From 30579ef578a58ddc0194cbcf8749b896e4a5c574 Mon Sep 17 00:00:00 2001 From: fengzero Date: Wed, 17 May 2023 08:23:15 +0000 Subject: [PATCH] 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 b253ed67..9f27cc72 100644 --- a/include/neuron/type.h +++ b/include/neuron/type.h @@ -112,6 +112,84 @@ typedef union { uint8_t bytes[NEU_VALUE_SIZE]; } __attribute__((packed)) 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;