From 62358668670bf223baa73b4644580611d051ea5a Mon Sep 17 00:00:00 2001 From: benbenshi Date: Fri, 21 Dec 2018 02:29:34 -0500 Subject: [PATCH] =?UTF-8?q?feature:=E5=A2=9E=E5=8A=A0hexdump=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、移植ulog的hexdump功能,验证ok,main.c编译后hexdump输出如下: D/HEX test: 0000-0010: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................ D/HEX test: 0010-0020: 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ................ D/HEX test: 0020-0030: 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&'()*+,-./ D/HEX test: 0030-0040: 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 0123456789:;<=>? D/HEX test: 0040-0050: 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFGHIJKLMNO D/HEX test: 0050-0060: 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVWXYZ[\]^_ D/HEX test: 0060-0070: 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefghijklmno D/HEX test: 0070-0080: 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvwxyz{|}~. D/HEX test: 0080-0090: 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ................ D/HEX test: 0090-00A0: 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ................ D/HEX test: 00A0-00B0: A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ................ D/HEX test: 00B0-00C0: B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF ................ D/HEX test: 00C0-00D0: C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ................ D/HEX test: 00D0-00E0: D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ................ D/HEX test: 00E0-00F0: E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ................ D/HEX test: 00F0-0100: F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ................ --- demo/os/linux/main.c | 8 ++++ easylogger/inc/elog.h | 1 + easylogger/src/elog.c | 90 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/demo/os/linux/main.c b/demo/os/linux/main.c index f3eb6ac..4fcd0ce 100644 --- a/demo/os/linux/main.c +++ b/demo/os/linux/main.c @@ -72,6 +72,13 @@ int main(void) { * EasyLogger demo */ void test_elog(void) { + uint8_t buf[256]= {0}; + int i = 0; + + for (i = 0; i < sizeof(buf); i++) + { + buf[i] = i; + } while(true) { /* test log output for all level */ log_a("Hello EasyLogger!"); @@ -81,6 +88,7 @@ void test_elog(void) { log_d("Hello EasyLogger!"); log_v("Hello EasyLogger!"); // elog_raw("Hello EasyLogger!"); + elog_hexdump("test", 16, buf, sizeof(buf)); sleep(5); } } diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index 1bbed0b..caa28f4 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -182,6 +182,7 @@ extern void (*elog_assert_hook)(const char* expr, const char* func, size_t line) void elog_assert_set_hook(void (*hook)(const char* expr, const char* func, size_t line)); int8_t elog_find_lvl(const char *log); const char *elog_find_tag(const char *log, uint8_t lvl, size_t *tag_len); +void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size); #define elog_a(tag, ...) elog_assert(tag, __VA_ARGS__) #define elog_e(tag, ...) elog_error(tag, __VA_ARGS__) diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index b7c7ac5..c35db57 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -404,7 +404,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f if (level > elog.filter.level) { return; } else if (!strstr(tag, elog.filter.tag)) { /* tag filter */ - //TODO ԿDzKMPģʽƥַ + //TODO ���Կ��Dz���KMP������ģʽƥ���ַ������������� return; } /* args point to the first variable parameter */ @@ -478,7 +478,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f } /* package thread info */ if (get_fmt_enabled(level, ELOG_FMT_LINE)) { - //TODO snprintfԴռÿܽϸߣŻ + //TODO snprintf��Դռ�ÿ��ܽϸߣ����Ż� snprintf(line_num, ELOG_LINE_NUM_MAX_LEN, "%ld", line); log_len += elog_strcpy(log_len, log_buf + log_len, line_num); } @@ -667,3 +667,89 @@ const char *elog_find_tag(const char *log, uint8_t lvl, size_t *tag_len) { return tag; } + +/** + * dump the hex format data to log + * + * @param name name for hex object, it will show on log header + * @param width hex number for every line, such as: 16, 32 + * @param buf hex buffer + * @param size buffer size + */ +void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size) +{ +#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ') + + uint16_t i, j; + uint16_t log_len = 0; + char dump_string[8] = {0}; + int fmt_result; + + if (!elog.output_enabled) { + return; + } + + /* lock output */ + elog_output_lock(); + + for (i = 0; i < size; i += width) + { + /* package header */ + fmt_result = snprintf(log_buf, ELOG_LINE_BUF_SIZE, "D/HEX %s: %04X-%04X: ", name, i, i + width); + /* calculate log length */ + if ((fmt_result > -1) && (fmt_result <= ELOG_LINE_BUF_SIZE)) + { + log_len = fmt_result; + } + else + { + log_len = ELOG_LINE_BUF_SIZE; + } + /* dump hex */ + for (j = 0; j < width; j++) + { + if (i + j < size) + { + snprintf(dump_string, sizeof(dump_string), "%02X ", buf[i + j]); + } + else + { + strncpy(dump_string, " ", sizeof(dump_string)); + } + log_len += elog_strcpy(log_len, log_buf + log_len, dump_string); + if ((j + 1) % 8 == 0) + { + log_len += elog_strcpy(log_len, log_buf + log_len, " "); + } + } + log_len += elog_strcpy(log_len, log_buf + log_len, " "); + /* dump char for hex */ + for (j = 0; j < width; j++) + { + if (i + j < size) + { + snprintf(dump_string, sizeof(dump_string), "%c", __is_print(buf[i + j]) ? buf[i + j] : '.'); + log_len += elog_strcpy(log_len, log_buf + log_len, dump_string); + } + } + /* overflow check and reserve some space for newline sign */ + if (log_len + strlen(ELOG_NEWLINE_SIGN) > ELOG_LINE_BUF_SIZE) + { + log_len = ELOG_LINE_BUF_SIZE - strlen(ELOG_NEWLINE_SIGN); + } + /* package newline sign */ + log_len += elog_strcpy(log_len, log_buf + log_len, ELOG_NEWLINE_SIGN); + /* do log output */ +#if defined(ELOG_ASYNC_OUTPUT_ENABLE) + extern void elog_async_output(uint8_t level, const char *log, size_t size); + elog_async_output(ELOG_LVL_DEBUG, log_buf, log_len); +#elif defined(ELOG_BUF_OUTPUT_ENABLE) + extern void elog_buf_output(const char *log, size_t size); + elog_buf_output(log_buf, log_len); +#else + elog_port_output(log_buf, log_len); +#endif + } + /* unlock output */ + elog_output_unlock(); +}