From 53a0261193692a4e9938450e9c486c9cdab59dd7 Mon Sep 17 00:00:00 2001 From: fengzero Date: Wed, 17 May 2023 08:23:15 +0000 Subject: [PATCH 1/2] chore: log type & value --- include/neuron/type.h | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/include/neuron/type.h b/include/neuron/type.h index 81f6ac59..f946129b 100644 --- a/include/neuron/type.h +++ b/include/neuron/type.h @@ -112,6 +112,84 @@ typedef union { uint8_t bytes[NEU_VALUE_SIZE]; } neu_value_u; +static inline char *neu_value_str(neu_type_e type, neu_value_u value) +{ + static __thread char str[32] = { 0 }; + + switch (type) { + case NEU_TYPE_INT8: + snprintf(str, sizeof(str), "type: %s, value: %i", neu_type_string(type), + value.i8); + break; + case NEU_TYPE_UINT8: + snprintf(str, sizeof(str), "type: %s, value: %u", neu_type_string(type), + value.u8); + break; + case NEU_TYPE_INT16: + snprintf(str, sizeof(str), "type: %s, value: %i", neu_type_string(type), + value.i16); + break; + case NEU_TYPE_WORD: + case NEU_TYPE_UINT16: + snprintf(str, sizeof(str), "type: %s, value: %u", neu_type_string(type), + value.u16); + break; + case NEU_TYPE_INT32: + snprintf(str, sizeof(str), "type: %s, value: %i", neu_type_string(type), + value.i32); + break; + case NEU_TYPE_DWORD: + case NEU_TYPE_UINT32: + snprintf(str, sizeof(str), "type: %s, value: %u", neu_type_string(type), + value.u32); + break; + case NEU_TYPE_INT64: +#if __WORDSIZE == 64 + snprintf(str, sizeof(str), "type: %s, value: %li", + neu_type_string(type), value.i64); +#else + snprintf(str, sizeof(str), "type: %s, value: %lli", + neu_type_string(type), value.i64); +#endif + break; + case NEU_TYPE_LWORD: + case NEU_TYPE_UINT64: +#if __WORDSIZE == 64 + snprintf(str, sizeof(str), "type: %s, value: %lu", + neu_type_string(type), value.u64); +#else + snprintf(str, sizeof(str), "type: %s, value: %llu", + neu_type_string(type), value.u64); +#endif + break; + case NEU_TYPE_FLOAT: + snprintf(str, sizeof(str), "type: %s, value: %f", neu_type_string(type), + value.f32); + break; + case NEU_TYPE_DOUBLE: + snprintf(str, sizeof(str), "type: %s, value: %f", neu_type_string(type), + value.d64); + break; + case NEU_TYPE_BIT: + snprintf(str, sizeof(str), "type: %s, value: %u", neu_type_string(type), + value.u8); + break; + case NEU_TYPE_BOOL: + snprintf(str, sizeof(str), "type: %s, value: %u", neu_type_string(type), + value.boolean); + break; + case NEU_TYPE_STRING: { + snprintf(str, sizeof(str), "type: %s, value: %s", neu_type_string(type), + value.str); + break; + } + default: + break; + } + + return str; +} + typedef struct { neu_type_e type; neu_value_u value; From 12da5c41e4986a152991c0f68bba22c79b494607 Mon Sep 17 00:00:00 2001 From: fengzero Date: Wed, 17 May 2023 10:01:45 +0000 Subject: [PATCH 2/2] chore(log): limit string length --- include/neuron/type.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/neuron/type.h b/include/neuron/type.h index f946129b..cbe5bb04 100644 --- a/include/neuron/type.h +++ b/include/neuron/type.h @@ -179,8 +179,9 @@ static inline char *neu_value_str(neu_type_e type, neu_value_u value) value.boolean); break; case NEU_TYPE_STRING: { - snprintf(str, sizeof(str), "type: %s, value: %s", neu_type_string(type), - value.str); + snprintf(str, sizeof(str), "type: %s, value: %c%c%c", + neu_type_string(type), value.str[0], value.str[1], + value.str[2]); break; } default: