Skip to content

Commit 1c109d2

Browse files
committed
samples: logging: add a sample app for dictionary-based logging
This adds a sample application on dictionary-based logging. The README file includes instruction on how to run the log parser to generate human readable log messages. Signed-off-by: Daniel Leung <[email protected]>
1 parent e06a014 commit 1c109d2

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(logging_dictionary)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _logging_dictionary_sample:
2+
3+
Dictionary-based Logging Sample
4+
###############################
5+
6+
This is a sample app which utilizes dictionary based logging and
7+
the UART backend. This is configured to output binary log data
8+
in hexadecimal so it could be run in terminal.
9+
10+
11+
Building and Running
12+
********************
13+
14+
This application can be built and executed on QEMU as follows:
15+
16+
.. zephyr-app-commands::
17+
:zephyr-app: samples/subsys/logging/dictionary
18+
:host-os: unix
19+
:board: qemu_x86
20+
:goals: run
21+
:compact:
22+
23+
To build for another board, change "qemu_x86" above to that board's name.
24+
25+
26+
Sample Output
27+
=============
28+
29+
.. code-block:: none
30+
31+
SeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
32+
Booting from ROM..##ZLOGV1##000004000000000000ecd7000004000000f76a10001e6b10001d6b1000000003000000000000b3e30000030000004d6410004464100000080200000000000093e70000020000005e64100000200300000000000076eb0000030000006b641000dc60100000180200000000000043ef0000020000007c64100000200500000000000066f300000500000088641000dc60100001000000020000000020050000000000008cf7000005000000a2641000dc6010001000000011000000002005000000000000b3fb000005000000be641000dc60100020000000210000000020070000000000000600010007000000da641000dc60100040000000000000004100000000000000002004000000000000491f0100040000000e651000dc60100021000000002004000000000000e02801000400000025651000dc6010001a651000006007000000000000993501000401000032651000dc601000000000000364796e616d6963207374720000201b0000000000000c5701000a04000043651000dc601000000000003f651000000000003f651000000000003f651000000000000364796e616d696320737472000564796e616d6963207374720007616e6f746865722064796e616d6963207374720009616e6f746865722064796e616d6963207374720000600b000000000000736a01000801000066651000dc601000210000001a651000000000001a651000210000000564796e616d696320737472000020040000000000008c6e01000400000083651000dc601000da0310000020041a0000000000967a010004000000ba651000dc601000ad65100048455844554d50212048455844554d50402048455844554d5023
33+
34+
35+
Running the Log Parser
36+
======================
37+
38+
1. Pipe the run output or copy the hexadecimal string into a temporary file ``/tmp/serial.log``:
39+
40+
.. code-block:: none
41+
42+
##ZLOGV1##000004000000000000ecd7000004000000f76a10001e6b10001d6b1000000003000000000000b3e30000030000004d6410004464100000080200000000000093e70000020000005e64100000200300000000000076eb0000030000006b641000dc60100000180200000000000043ef0000020000007c64100000200500000000000066f300000500000088641000dc60100001000000020000000020050000000000008cf7000005000000a2641000dc6010001000000011000000002005000000000000b3fb000005000000be641000dc60100020000000210000000020070000000000000600010007000000da641000dc60100040000000000000004100000000000000002004000000000000491f0100040000000e651000dc60100021000000002004000000000000e02801000400000025651000dc6010001a651000006007000000000000993501000401000032651000dc601000000000000364796e616d6963207374720000201b0000000000000c5701000a04000043651000dc601000000000003f651000000000003f651000000000003f651000000000000364796e616d696320737472000564796e616d6963207374720007616e6f746865722064796e616d6963207374720009616e6f746865722064796e616d6963207374720000600b000000000000736a01000801000066651000dc601000210000001a651000000000001a651000210000000564796e616d696320737472000020040000000000008c6e01000400000083651000dc601000da0310000020041a0000000000967a010004000000ba651000dc601000ad65100048455844554d50212048455844554d50402048455844554d5023
43+
44+
2. Run the log parser to get the text output:
45+
46+
.. code-block:: console
47+
48+
./scripts/logging/dictionary/log_parser.py build/zephyr/log_dictionary.json /tmp/serial.log --hex
49+
50+
And here is the output:
51+
52+
.. code-block:: none
53+
54+
*** Booting Zephyr OS build zephyr-v2.5.0-2471-g09b9eaf333e6 ***
55+
Hello World! qemu_x86
56+
[ 77820] <err> hello_world: error string
57+
[ 86332] <dbg> hello_world: main: debug string
58+
[ 96275] <inf> hello_world: info string
59+
[ 104784] <dbg> hello_world: main: int8_t 1, uint8_t 2
60+
[ 117609] <dbg> hello_world: main: int16_t 16, uint16_t 17
61+
[ 130454] <dbg> hello_world: main: int32_t 32, uint32_t 33
62+
[ 143299] <dbg> hello_world: main: int64_t 64, uint64_t 65
63+
[ 166374] <dbg> hello_world: main: char !
64+
[ 179452] <dbg> hello_world: main: s str static str
65+
[ 193395] <dbg> hello_world: main: d str dynamic str
66+
[ 217283] <dbg> hello_world: main: mixed str dynamic str --- dynamic str --- another dynamic str --- another dynamic str
67+
[ 266022] <dbg> hello_world: main: mixed c/s ! static str dynamic str static str !
68+
[ 287888] <dbg> hello_world: main: pointer 0x10651a
69+
[ 301600] <dbg> hello_world: main: For HeXdUmP!
70+
48 45 58 44 55 4d 50 21 20 48 45 58 44 55 4d 50 |HEXDUMP! HEXDUMP
71+
40 20 48 45 58 44 55 4d 50 23 |@ HEXDUM P#
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_LOG=y
2+
CONFIG_LOG_PRINTK=y
3+
CONFIG_LOG_BACKEND_UART_OUTPUT_DICTIONARY_HEX=y
4+
CONFIG_LOG_BACKEND_UART=y
5+
CONFIG_LOG2_MODE_DEFERRED=y
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sample:
2+
description: Dictionary-based Logging Sample Application
3+
name: logging_dictionary
4+
tests:
5+
sample.logger.basic.dictionary:
6+
build_only: true
7+
tags: logging
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2012-2014 Wind River Systems, Inc.
3+
* Copyright (c) 2021 Intel Corporation
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <inttypes.h>
9+
#include <zephyr.h>
10+
#include <string.h>
11+
#include <sys/printk.h>
12+
#include <logging/log.h>
13+
14+
LOG_MODULE_REGISTER(hello_world, 4);
15+
16+
static const char *hexdump_msg = "HEXDUMP! HEXDUMP@ HEXDUMP#";
17+
18+
void main(void)
19+
{
20+
int8_t i8 = 1;
21+
uint8_t u8 = 2;
22+
int16_t i16 = 16;
23+
uint16_t u16 = 17;
24+
int32_t i32 = 32;
25+
uint32_t u32 = 33;
26+
int64_t i64 = 64;
27+
uint64_t u64 = 65;
28+
char c = '!';
29+
char *s = "static str";
30+
char vs0[32];
31+
char vs1[32];
32+
void *p = s;
33+
34+
#ifdef CONFIG_FPU
35+
float f = 66.67;
36+
double d = 68.69;
37+
38+
long double ld = 70.71;
39+
#endif
40+
41+
printk("Hello World! %s\n", CONFIG_BOARD);
42+
43+
LOG_ERR("error string");
44+
LOG_DBG("debug string");
45+
LOG_INF("info string");
46+
47+
LOG_DBG("int8_t %" PRId8 ", uint8_t %" PRIu8, i8, u8);
48+
LOG_DBG("int16_t %" PRId16 ", uint16_t %" PRIu16, i16, u16);
49+
LOG_DBG("int32_t %" PRId32 ", uint32_t %" PRIu32, i32, u32);
50+
LOG_DBG("int64_t %" PRId64 ", uint64_t %" PRIu64, i64, u64);
51+
52+
memset(vs0, 0, sizeof(vs0));
53+
snprintk(&vs0[0], sizeof(vs0), "%s", "dynamic str");
54+
55+
memset(vs1, 0, sizeof(vs1));
56+
snprintk(&vs1[0], sizeof(vs1), "%s", "another dynamic str");
57+
58+
LOG_DBG("char %c", c);
59+
LOG_DBG("s str %s", s);
60+
LOG_DBG("d str %s", vs0);
61+
LOG_DBG("mixed str %s %s %s %s %s %s %s", vs0, "---", vs0, "---", vs1, "---", vs1);
62+
LOG_DBG("mixed c/s %c %s %s %s %c", c, s, vs0, s, c);
63+
64+
LOG_DBG("pointer %p", p);
65+
66+
LOG_HEXDUMP_DBG(hexdump_msg, strlen(hexdump_msg), "For HeXdUmP!");
67+
68+
#ifdef CONFIG_FPU
69+
LOG_DBG("float %f, double %f, long double %Lf", f, d, ld);
70+
#endif
71+
}

0 commit comments

Comments
 (0)