From 1374a5e5e6775dd6db9533075f25b3112c984b22 Mon Sep 17 00:00:00 2001 From: Arto Kinnunen Date: Fri, 9 Nov 2018 15:59:35 +0200 Subject: [PATCH] Squashed 'features/frameworks/mbed-trace/' changes from 9eaf0d1..7a1bd34 7a1bd34 Use Mbed OS coding style (#86) git-subtree-dir: features/frameworks/mbed-trace git-subtree-split: 7a1bd34be547f3aa9badfbeb1c9993ff4a9e1ded --- mbed-trace/mbed_trace.h | 36 +-- source/mbed_trace.c | 26 +-- test/Test.cpp | 483 ++++++++++++++++++++-------------------- 3 files changed, 276 insertions(+), 269 deletions(-) diff --git a/mbed-trace/mbed_trace.h b/mbed-trace/mbed_trace.h index eb4055b0c0..0562e4a898 100644 --- a/mbed-trace/mbed_trace.h +++ b/mbed-trace/mbed_trace.h @@ -180,11 +180,11 @@ extern "C" { * Initialize trace functionality * @return 0 when all success, otherwise non zero */ -int mbed_trace_init( void ); +int mbed_trace_init(void); /** * Free trace memory */ -void mbed_trace_free( void ); +void mbed_trace_free(void); /** * Resize buffers (line / tmp ) sizes * @param lineLength new maximum length for trace line (0 = do no resize) @@ -226,7 +226,7 @@ uint8_t mbed_trace_config_get(void); * char* trace_time(){ return "rtc-time-in-string"; } * mbed_trace_prefix_function_set( &trace_time ); */ -void mbed_trace_prefix_function_set( char* (*pref_f)(size_t) ); +void mbed_trace_prefix_function_set(char *(*pref_f)(size_t)); /** * Set trace suffix function * suffix -function return string with null terminated @@ -235,18 +235,18 @@ void mbed_trace_prefix_function_set( char* (*pref_f)(size_t) ); * char* trace_suffix(){ return " END"; } * mbed_trace_suffix_function_set( &trace_suffix ); */ -void mbed_trace_suffix_function_set(char* (*suffix_f)(void) ); +void mbed_trace_suffix_function_set(char *(*suffix_f)(void)); /** * Set trace print function * By default, trace module print using printf() function, * but with this you can write own print function, * for e.g. to other IO device. */ -void mbed_trace_print_function_set( void (*print_f)(const char*) ); +void mbed_trace_print_function_set(void (*print_f)(const char *)); /** * Set trace print function for tr_cmdline() */ -void mbed_trace_cmdprint_function_set( void (*printf)(const char*) ); +void mbed_trace_cmdprint_function_set(void (*printf)(const char *)); /** * Set trace mutex wait function * By default, trace calls are not thread safe. @@ -270,10 +270,10 @@ void mbed_trace_mutex_release_function_set(void (*mutex_release_f)(void)); * mbed_trace_exclude_filters_set("mygr"); * mbed_tracef(TRACE_ACTIVE_LEVEL_DEBUG, "ougr", "This is not printed"); */ -void mbed_trace_exclude_filters_set(char* filters); +void mbed_trace_exclude_filters_set(char *filters); /** get trace exclude filters */ -const char* mbed_trace_exclude_filters_get(void); +const char *mbed_trace_exclude_filters_get(void); /** * When trace group contains text in filter, * trace will be printed. @@ -282,10 +282,10 @@ const char* mbed_trace_exclude_filters_get(void); * mbed_tracef(TRACE_ACTIVE_LEVEL_DEBUG, "mygr", "Hi There"); * mbed_tracef(TRACE_ACTIVE_LEVEL_DEBUG, "grp2", "This is not printed"); */ -void mbed_trace_include_filters_set(char* filters); +void mbed_trace_include_filters_set(char *filters); /** get trace include filters */ -const char* mbed_trace_include_filters_get(void); +const char *mbed_trace_include_filters_get(void); /** * General trace function * This should be used every time when user want to print out something important thing @@ -298,9 +298,9 @@ const char* mbed_trace_include_filters_get(void); * @param ... variable arguments related to fmt */ #if defined(__GNUC__) || defined(__CC_ARM) -void mbed_tracef(uint8_t dlevel, const char* grp, const char *fmt, ...) __attribute__ ((__format__(__printf__, 3, 4))); +void mbed_tracef(uint8_t dlevel, const char *grp, const char *fmt, ...) __attribute__((__format__(__printf__, 3, 4))); #else -void mbed_tracef(uint8_t dlevel, const char* grp, const char *fmt, ...); +void mbed_tracef(uint8_t dlevel, const char *grp, const char *fmt, ...); #endif /** * General trace function @@ -318,16 +318,16 @@ void mbed_tracef(uint8_t dlevel, const char* grp, const char *fmt, ...); * @param ap variable arguments list (like vprintf) */ #if defined(__GNUC__) || defined(__CC_ARM) -void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap) __attribute__ ((__format__(__printf__, 3, 0))); +void mbed_vtracef(uint8_t dlevel, const char *grp, const char *fmt, va_list ap) __attribute__((__format__(__printf__, 3, 0))); #else -void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap); +void mbed_vtracef(uint8_t dlevel, const char *grp, const char *fmt, va_list ap); #endif /** * Get last trace from buffer */ -const char* mbed_trace_last(void); +const char *mbed_trace_last(void); #if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1 /** * mbed_tracef helping function for convert ipv6 @@ -339,7 +339,7 @@ const char* mbed_trace_last(void); * @param add_ptr IPv6 Address pointer * @return temporary buffer where ipv6 is in string format */ -char* mbed_trace_ipv6(const void *addr_ptr); +char *mbed_trace_ipv6(const void *addr_ptr); /** * mbed_tracef helping function for print ipv6 prefix * usage e.g. @@ -350,7 +350,7 @@ char* mbed_trace_ipv6(const void *addr_ptr); * @param prefix_len prefix length * @return temporary buffer where ipv6 is in string format */ -char* mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len); +char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len); #endif /** * mbed_tracef helping function for convert hex-array to string. @@ -364,7 +364,7 @@ char* mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len); * if array as string not fit to temp buffer, this function write '*' as last character, * which indicate that buffer is too small for array. */ -char* mbed_trace_array(const uint8_t* buf, uint16_t len); +char *mbed_trace_array(const uint8_t *buf, uint16_t len); #ifdef __cplusplus } diff --git a/source/mbed_trace.c b/source/mbed_trace.c index 5fdffd52c3..04dacbfaaf 100644 --- a/source/mbed_trace.c +++ b/source/mbed_trace.c @@ -93,7 +93,7 @@ #endif /** default print function, just redirect str to printf */ -static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length); +static void mbed_trace_realloc(char **buffer, int *length_ptr, int new_length); static void mbed_trace_default_print(const char *str); static void mbed_trace_reset_tmp(void); @@ -209,7 +209,7 @@ void mbed_trace_free(void) m_trace.mutex_release_f = 0; m_trace.mutex_lock_count = 0; } -static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length) +static void mbed_trace_realloc(char **buffer, int *length_ptr, int new_length) { MBED_TRACE_MEM_FREE(*buffer); *buffer = MBED_TRACE_MEM_ALLOC(new_length); @@ -217,11 +217,11 @@ static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length) } void mbed_trace_buffer_sizes(int lineLength, int tmpLength) { - if( lineLength > 0 ) { - mbed_trace_realloc( &(m_trace.line), &m_trace.line_length, lineLength ); + if (lineLength > 0) { + mbed_trace_realloc(&(m_trace.line), &m_trace.line_length, lineLength); } - if( tmpLength > 0 ) { - mbed_trace_realloc( &(m_trace.tmp_data), &m_trace.tmp_data_length, tmpLength); + if (tmpLength > 0) { + mbed_trace_realloc(&(m_trace.tmp_data), &m_trace.tmp_data_length, tmpLength); mbed_trace_reset_tmp(); } } @@ -311,9 +311,9 @@ void mbed_tracef(uint8_t dlevel, const char *grp, const char *fmt, ...) mbed_vtracef(dlevel, grp, fmt, ap); va_end(ap); } -void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap) +void mbed_vtracef(uint8_t dlevel, const char *grp, const char *fmt, va_list ap) { - if ( m_trace.mutex_wait_f ) { + if (m_trace.mutex_wait_f) { m_trace.mutex_wait_f(); m_trace.mutex_lock_count++; } @@ -476,7 +476,7 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap) } end: - if ( m_trace.mutex_release_f ) { + if (m_trace.mutex_release_f) { // Store the mutex lock count to temp variable so that it won't get // clobbered during last loop iteration when mutex gets released int count = m_trace.mutex_lock_count; @@ -507,7 +507,7 @@ const char *mbed_trace_last(void) char *mbed_trace_ipv6(const void *addr_ptr) { /** Acquire mutex. It is released before returning from mbed_vtracef. */ - if ( m_trace.mutex_wait_f ) { + if (m_trace.mutex_wait_f) { m_trace.mutex_wait_f(); m_trace.mutex_lock_count++; } @@ -528,7 +528,7 @@ char *mbed_trace_ipv6(const void *addr_ptr) char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len) { /** Acquire mutex. It is released before returning from mbed_vtracef. */ - if ( m_trace.mutex_wait_f ) { + if (m_trace.mutex_wait_f) { m_trace.mutex_wait_f(); m_trace.mutex_lock_count++; } @@ -551,7 +551,7 @@ char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len) char *mbed_trace_array(const uint8_t *buf, uint16_t len) { /** Acquire mutex. It is released before returning from mbed_vtracef. */ - if ( m_trace.mutex_wait_f ) { + if (m_trace.mutex_wait_f) { m_trace.mutex_wait_f(); m_trace.mutex_lock_count++; } @@ -581,7 +581,7 @@ char *mbed_trace_array(const uint8_t *buf, uint16_t len) wptr += retval; } if (wptr > str) { - if( overflow ) { + if (overflow) { // replace last character as 'star', // which indicate buffer len is not enough *(wptr - 1) = '*'; diff --git a/test/Test.cpp b/test/Test.cpp index 845d9a49c6..4261d8172e 100644 --- a/test/Test.cpp +++ b/test/Test.cpp @@ -30,101 +30,107 @@ static int mutex_release_count = 0; static bool check_mutex_lock_status = true; void my_mutex_wait() { - mutex_wait_count++; + mutex_wait_count++; } void my_mutex_release() { - mutex_release_count++; + mutex_release_count++; } char buf[1024]; #include -void myprint(const char* str) +void myprint(const char *str) { - if ( check_mutex_lock_status ) { - CHECK( (mutex_wait_count - mutex_release_count) > 0 ); - } - strcpy(buf, str); + if (check_mutex_lock_status) { + CHECK((mutex_wait_count - mutex_release_count) > 0); + } + strcpy(buf, str); } TEST_GROUP(trace) { - void setup() - { + void setup() { - mbed_trace_init(); - mbed_trace_config_set(TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL); - mbed_trace_print_function_set( myprint ); - mbed_trace_mutex_wait_function_set( my_mutex_wait ); - mbed_trace_mutex_release_function_set( my_mutex_release ); - } - void teardown() - { - CHECK(mutex_wait_count == mutex_release_count); // Check the mutex count with every test - mbed_trace_free(); - } + mbed_trace_init(); + mbed_trace_config_set(TRACE_MODE_PLAIN | TRACE_ACTIVE_LEVEL_ALL); + mbed_trace_print_function_set(myprint); + mbed_trace_mutex_wait_function_set(my_mutex_wait); + mbed_trace_mutex_release_function_set(my_mutex_release); + } + void teardown() { + CHECK(mutex_wait_count == mutex_release_count); // Check the mutex count with every test + mbed_trace_free(); + } }; /* Unity test code starts */ TEST(trace, MutexNotSet) { - mbed_trace_mutex_wait_function_set( 0 ); - mbed_trace_mutex_release_function_set( 0 ); - int mutex_call_count_at_entry = mutex_wait_count; - check_mutex_lock_status = false; + mbed_trace_mutex_wait_function_set(0); + mbed_trace_mutex_release_function_set(0); + int mutex_call_count_at_entry = mutex_wait_count; + check_mutex_lock_status = false; - char expectedStr[] = "Hello hello!"; - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "Hello hello!"); - STRCMP_EQUAL(expectedStr, buf); + char expectedStr[] = "Hello hello!"; + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "Hello hello!"); + STRCMP_EQUAL(expectedStr, buf); - CHECK( mutex_call_count_at_entry == mutex_wait_count ); - CHECK( mutex_call_count_at_entry == mutex_release_count ); + CHECK(mutex_call_count_at_entry == mutex_wait_count); + CHECK(mutex_call_count_at_entry == mutex_release_count); - mbed_trace_mutex_wait_function_set( my_mutex_wait ); - mbed_trace_mutex_release_function_set( my_mutex_release ); - check_mutex_lock_status = true; + mbed_trace_mutex_wait_function_set(my_mutex_wait); + mbed_trace_mutex_release_function_set(my_mutex_release); + check_mutex_lock_status = true; } TEST(trace, Array) { - unsigned char longStr[200] = {0x66}; - for(int i=0;i<200;i++) {longStr[i] = 0x66; } - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(longStr, 200) ); + unsigned char longStr[200] = {0x66}; + for (int i = 0; i < 200; i++) { + longStr[i] = 0x66; + } + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(longStr, 200)); } TEST(trace, Null0Array) { - static const unsigned char array[2] = { 0x23, 0x45 }; - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(array, 2)); - STRCMP_EQUAL("23:45", buf); - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(array, 0)); - STRCMP_EQUAL("", buf); - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(NULL, 0)); - STRCMP_EQUAL("", buf); - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(NULL, 2)); - STRCMP_EQUAL("", buf); + static const unsigned char array[2] = { 0x23, 0x45 }; + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(array, 2)); + STRCMP_EQUAL("23:45", buf); + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(array, 0)); + STRCMP_EQUAL("", buf); + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(NULL, 0)); + STRCMP_EQUAL("", buf); + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", mbed_trace_array(NULL, 2)); + STRCMP_EQUAL("", buf); } TEST(trace, LongString) { - char longStr[1000] = {0x36}; - for(int i=0;i<999;i++) {longStr[i] = 0x36; } - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", longStr ); + char longStr[1000] = {0x36}; + for (int i = 0; i < 999; i++) { + longStr[i] = 0x36; + } + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "%s", longStr); } TEST(trace, TooLong) { - #define TOO_LONG_SIZE 9400 - #define TRACE_LINE_SIZE 1024 - char longStr[TOO_LONG_SIZE] = {0}; - for(int i=0;i][DBG ][mygr]: test 1 2", buf); - //TEST_ASSERT_EQUAL_INT(4, time_length); - - mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL|TRACE_MODE_PLAIN); - mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "test"); - STRCMP_EQUAL("test", buf); - - mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL|TRACE_MODE_COLOR); - mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "test"); - STRCMP_EQUAL("\x1b[31m[