From ae55a73b7a46338bcc6e3d18a78353eb7d1b881e Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Fri, 9 Apr 2021 12:30:02 -0700 Subject: [PATCH] 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 --- .../subsys/logging/dictionary/CMakeLists.txt | 8 +++ samples/subsys/logging/dictionary/README.rst | 71 +++++++++++++++++++ samples/subsys/logging/dictionary/prj.conf | 5 ++ samples/subsys/logging/dictionary/sample.yaml | 7 ++ samples/subsys/logging/dictionary/src/main.c | 71 +++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 samples/subsys/logging/dictionary/CMakeLists.txt create mode 100644 samples/subsys/logging/dictionary/README.rst create mode 100644 samples/subsys/logging/dictionary/prj.conf create mode 100644 samples/subsys/logging/dictionary/sample.yaml create mode 100644 samples/subsys/logging/dictionary/src/main.c diff --git a/samples/subsys/logging/dictionary/CMakeLists.txt b/samples/subsys/logging/dictionary/CMakeLists.txt new file mode 100644 index 00000000000..da573f4ae7f --- /dev/null +++ b/samples/subsys/logging/dictionary/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(logging_dictionary) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/subsys/logging/dictionary/README.rst b/samples/subsys/logging/dictionary/README.rst new file mode 100644 index 00000000000..be7176b9af4 --- /dev/null +++ b/samples/subsys/logging/dictionary/README.rst @@ -0,0 +1,71 @@ +.. _logging_dictionary_sample: + +Dictionary-based Logging Sample +############################### + +This is a sample app which utilizes dictionary based logging and +the UART backend. This is configured to output binary log data +in hexadecimal so it could be run in terminal. + + +Building and Running +******************** + +This application can be built and executed on QEMU as follows: + +.. zephyr-app-commands:: + :zephyr-app: samples/subsys/logging/dictionary + :host-os: unix + :board: qemu_x86 + :goals: run + :compact: + +To build for another board, change "qemu_x86" above to that board's name. + + +Sample Output +============= + + .. code-block:: none + + SeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr) + Booting from ROM..##ZLOGV1##000004000000000000ecd7000004000000f76a10001e6b10001d6b1000000003000000000000b3e30000030000004d6410004464100000080200000000000093e70000020000005e64100000200300000000000076eb0000030000006b641000dc60100000180200000000000043ef0000020000007c64100000200500000000000066f300000500000088641000dc60100001000000020000000020050000000000008cf7000005000000a2641000dc6010001000000011000000002005000000000000b3fb000005000000be641000dc60100020000000210000000020070000000000000600010007000000da641000dc60100040000000000000004100000000000000002004000000000000491f0100040000000e651000dc60100021000000002004000000000000e02801000400000025651000dc6010001a651000006007000000000000993501000401000032651000dc601000000000000364796e616d6963207374720000201b0000000000000c5701000a04000043651000dc601000000000003f651000000000003f651000000000003f651000000000000364796e616d696320737472000564796e616d6963207374720007616e6f746865722064796e616d6963207374720009616e6f746865722064796e616d6963207374720000600b000000000000736a01000801000066651000dc601000210000001a651000000000001a651000210000000564796e616d696320737472000020040000000000008c6e01000400000083651000dc601000da0310000020041a0000000000967a010004000000ba651000dc601000ad65100048455844554d50212048455844554d50402048455844554d5023 + + +Running the Log Parser +====================== + +1. Pipe the run output or copy the hexadecimal string into a temporary file ``/tmp/serial.log``: + + .. code-block:: none + + ##ZLOGV1##000004000000000000ecd7000004000000f76a10001e6b10001d6b1000000003000000000000b3e30000030000004d6410004464100000080200000000000093e70000020000005e64100000200300000000000076eb0000030000006b641000dc60100000180200000000000043ef0000020000007c64100000200500000000000066f300000500000088641000dc60100001000000020000000020050000000000008cf7000005000000a2641000dc6010001000000011000000002005000000000000b3fb000005000000be641000dc60100020000000210000000020070000000000000600010007000000da641000dc60100040000000000000004100000000000000002004000000000000491f0100040000000e651000dc60100021000000002004000000000000e02801000400000025651000dc6010001a651000006007000000000000993501000401000032651000dc601000000000000364796e616d6963207374720000201b0000000000000c5701000a04000043651000dc601000000000003f651000000000003f651000000000003f651000000000000364796e616d696320737472000564796e616d6963207374720007616e6f746865722064796e616d6963207374720009616e6f746865722064796e616d6963207374720000600b000000000000736a01000801000066651000dc601000210000001a651000000000001a651000210000000564796e616d696320737472000020040000000000008c6e01000400000083651000dc601000da0310000020041a0000000000967a010004000000ba651000dc601000ad65100048455844554d50212048455844554d50402048455844554d5023 + +2. Run the log parser to get the text output: + + .. code-block:: console + + ./scripts/logging/dictionary/log_parser.py build/zephyr/log_dictionary.json /tmp/serial.log --hex + + And here is the output: + + .. code-block:: none + + *** Booting Zephyr OS build zephyr-v2.5.0-2471-g09b9eaf333e6 *** + Hello World! qemu_x86 + [ 77820] hello_world: error string + [ 86332] hello_world: main: debug string + [ 96275] hello_world: info string + [ 104784] hello_world: main: int8_t 1, uint8_t 2 + [ 117609] hello_world: main: int16_t 16, uint16_t 17 + [ 130454] hello_world: main: int32_t 32, uint32_t 33 + [ 143299] hello_world: main: int64_t 64, uint64_t 65 + [ 166374] hello_world: main: char ! + [ 179452] hello_world: main: s str static str + [ 193395] hello_world: main: d str dynamic str + [ 217283] hello_world: main: mixed str dynamic str --- dynamic str --- another dynamic str --- another dynamic str + [ 266022] hello_world: main: mixed c/s ! static str dynamic str static str ! + [ 287888] hello_world: main: pointer 0x10651a + [ 301600] hello_world: main: For HeXdUmP! + 48 45 58 44 55 4d 50 21 20 48 45 58 44 55 4d 50 |HEXDUMP! HEXDUMP + 40 20 48 45 58 44 55 4d 50 23 |@ HEXDUM P# diff --git a/samples/subsys/logging/dictionary/prj.conf b/samples/subsys/logging/dictionary/prj.conf new file mode 100644 index 00000000000..f79592a503e --- /dev/null +++ b/samples/subsys/logging/dictionary/prj.conf @@ -0,0 +1,5 @@ +CONFIG_LOG=y +CONFIG_LOG_PRINTK=y +CONFIG_LOG_BACKEND_UART_OUTPUT_DICTIONARY_HEX=y +CONFIG_LOG_BACKEND_UART=y +CONFIG_LOG2_MODE_DEFERRED=y diff --git a/samples/subsys/logging/dictionary/sample.yaml b/samples/subsys/logging/dictionary/sample.yaml new file mode 100644 index 00000000000..936b4669893 --- /dev/null +++ b/samples/subsys/logging/dictionary/sample.yaml @@ -0,0 +1,7 @@ +sample: + description: Dictionary-based Logging Sample Application + name: logging_dictionary +tests: + sample.logger.basic.dictionary: + build_only: true + tags: logging diff --git a/samples/subsys/logging/dictionary/src/main.c b/samples/subsys/logging/dictionary/src/main.c new file mode 100644 index 00000000000..bdd819bbc07 --- /dev/null +++ b/samples/subsys/logging/dictionary/src/main.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2012-2014 Wind River Systems, Inc. + * Copyright (c) 2021 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(hello_world, 4); + +static const char *hexdump_msg = "HEXDUMP! HEXDUMP@ HEXDUMP#"; + +void main(void) +{ + int8_t i8 = 1; + uint8_t u8 = 2; + int16_t i16 = 16; + uint16_t u16 = 17; + int32_t i32 = 32; + uint32_t u32 = 33; + int64_t i64 = 64; + uint64_t u64 = 65; + char c = '!'; + char *s = "static str"; + char vs0[32]; + char vs1[32]; + void *p = s; + +#ifdef CONFIG_FPU + float f = 66.67; + double d = 68.69; + + long double ld = 70.71; +#endif + + printk("Hello World! %s\n", CONFIG_BOARD); + + LOG_ERR("error string"); + LOG_DBG("debug string"); + LOG_INF("info string"); + + LOG_DBG("int8_t %" PRId8 ", uint8_t %" PRIu8, i8, u8); + LOG_DBG("int16_t %" PRId16 ", uint16_t %" PRIu16, i16, u16); + LOG_DBG("int32_t %" PRId32 ", uint32_t %" PRIu32, i32, u32); + LOG_DBG("int64_t %" PRId64 ", uint64_t %" PRIu64, i64, u64); + + memset(vs0, 0, sizeof(vs0)); + snprintk(&vs0[0], sizeof(vs0), "%s", "dynamic str"); + + memset(vs1, 0, sizeof(vs1)); + snprintk(&vs1[0], sizeof(vs1), "%s", "another dynamic str"); + + LOG_DBG("char %c", c); + LOG_DBG("s str %s", s); + LOG_DBG("d str %s", vs0); + LOG_DBG("mixed str %s %s %s %s %s %s %s", vs0, "---", vs0, "---", vs1, "---", vs1); + LOG_DBG("mixed c/s %c %s %s %s %c", c, s, vs0, s, c); + + LOG_DBG("pointer %p", p); + + LOG_HEXDUMP_DBG(hexdump_msg, strlen(hexdump_msg), "For HeXdUmP!"); + +#ifdef CONFIG_FPU + LOG_DBG("float %f, double %f, long double %Lf", f, d, ld); +#endif +}