Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maygup01 0:11cc2b7889af 1 /*
maygup01 0:11cc2b7889af 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
maygup01 0:11cc2b7889af 3 * SPDX-License-Identifier: Apache-2.0
maygup01 0:11cc2b7889af 4 * Licensed under the Apache License, Version 2.0 (the License); you may
maygup01 0:11cc2b7889af 5 * not use this file except in compliance with the License.
maygup01 0:11cc2b7889af 6 * You may obtain a copy of the License at
maygup01 0:11cc2b7889af 7 *
maygup01 0:11cc2b7889af 8 * http://www.apache.org/licenses/LICENSE-2.0
maygup01 0:11cc2b7889af 9 *
maygup01 0:11cc2b7889af 10 * Unless required by applicable law or agreed to in writing, software
maygup01 0:11cc2b7889af 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
maygup01 0:11cc2b7889af 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maygup01 0:11cc2b7889af 13 * See the License for the specific language governing permissions and
maygup01 0:11cc2b7889af 14 * limitations under the License.
maygup01 0:11cc2b7889af 15 */
maygup01 0:11cc2b7889af 16 #include <stdio.h>
maygup01 0:11cc2b7889af 17 #include <string.h>
maygup01 0:11cc2b7889af 18 #include <stdarg.h>
maygup01 0:11cc2b7889af 19
maygup01 0:11cc2b7889af 20 #ifdef MBED_CONF_MBED_TRACE_ENABLE
maygup01 0:11cc2b7889af 21 #undef MBED_CONF_MBED_TRACE_ENABLE
maygup01 0:11cc2b7889af 22 #endif
maygup01 0:11cc2b7889af 23 #define MBED_CONF_MBED_TRACE_ENABLE 1
maygup01 0:11cc2b7889af 24 #ifndef MBED_CONF_MBED_TRACE_FEA_IPV6
maygup01 0:11cc2b7889af 25 #define MBED_CONF_MBED_TRACE_FEA_IPV6 1
maygup01 0:11cc2b7889af 26 #endif
maygup01 0:11cc2b7889af 27
maygup01 0:11cc2b7889af 28 #include "mbed-trace/mbed_trace.h"
maygup01 0:11cc2b7889af 29 #if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
maygup01 0:11cc2b7889af 30 #include "mbed-client-libservice/ip6string.h"
maygup01 0:11cc2b7889af 31 #include "mbed-client-libservice/common_functions.h"
maygup01 0:11cc2b7889af 32 #endif
maygup01 0:11cc2b7889af 33
maygup01 0:11cc2b7889af 34 #if defined(YOTTA_CFG_MBED_TRACE_MEM)
maygup01 0:11cc2b7889af 35 #define MBED_TRACE_MEM_INCLUDE YOTTA_CFG_MBED_TRACE_MEM_INCLUDE
maygup01 0:11cc2b7889af 36 #define MBED_TRACE_MEM_ALLOC YOTTA_CFG_MBED_TRACE_MEM_ALLOC
maygup01 0:11cc2b7889af 37 #define MBED_TRACE_MEM_FREE YOTTA_CFG_MBED_TRACE_MEM_FREE
maygup01 0:11cc2b7889af 38 #else /* YOTTA_CFG_MEMLIB */
maygup01 0:11cc2b7889af 39 // Default options
maygup01 0:11cc2b7889af 40 #ifndef MBED_TRACE_MEM_INCLUDE
maygup01 0:11cc2b7889af 41 #define MBED_TRACE_MEM_INCLUDE <stdlib.h>
maygup01 0:11cc2b7889af 42 #endif
maygup01 0:11cc2b7889af 43 #include MBED_TRACE_MEM_INCLUDE
maygup01 0:11cc2b7889af 44 #ifndef MBED_TRACE_MEM_ALLOC
maygup01 0:11cc2b7889af 45 #define MBED_TRACE_MEM_ALLOC malloc
maygup01 0:11cc2b7889af 46 #endif
maygup01 0:11cc2b7889af 47 #ifndef MBED_TRACE_MEM_FREE
maygup01 0:11cc2b7889af 48 #define MBED_TRACE_MEM_FREE free
maygup01 0:11cc2b7889af 49 #endif
maygup01 0:11cc2b7889af 50 #endif /* YOTTA_CFG_MEMLIB */
maygup01 0:11cc2b7889af 51
maygup01 0:11cc2b7889af 52 #define VT100_COLOR_ERROR "\x1b[31m"
maygup01 0:11cc2b7889af 53 #define VT100_COLOR_WARN "\x1b[33m"
maygup01 0:11cc2b7889af 54 #define VT100_COLOR_INFO "\x1b[39m"
maygup01 0:11cc2b7889af 55 #define VT100_COLOR_DEBUG "\x1b[90m"
maygup01 0:11cc2b7889af 56
maygup01 0:11cc2b7889af 57 /** default max trace line size in bytes */
maygup01 0:11cc2b7889af 58 #ifdef MBED_TRACE_LINE_LENGTH
maygup01 0:11cc2b7889af 59 #define DEFAULT_TRACE_LINE_LENGTH MBED_TRACE_LINE_LENGTH
maygup01 0:11cc2b7889af 60 #elif defined YOTTA_CFG_MBED_TRACE_LINE_LENGTH
maygup01 0:11cc2b7889af 61 #warning YOTTA_CFG_MBED_TRACE_LINE_LENGTH is deprecated and will be removed in the future! Use MBED_TRACE_LINE_LENGTH instead.
maygup01 0:11cc2b7889af 62 #define DEFAULT_TRACE_LINE_LENGTH YOTTA_CFG_MBED_TRACE_LINE_LENGTH
maygup01 0:11cc2b7889af 63 #else
maygup01 0:11cc2b7889af 64 #define DEFAULT_TRACE_LINE_LENGTH 1024
maygup01 0:11cc2b7889af 65 #endif
maygup01 0:11cc2b7889af 66
maygup01 0:11cc2b7889af 67 /** default max temporary buffer size in bytes, used in
maygup01 0:11cc2b7889af 68 trace_ipv6, trace_ipv6_prefix and trace_array */
maygup01 0:11cc2b7889af 69 #ifdef MBED_TRACE_TMP_LINE_LENGTH
maygup01 0:11cc2b7889af 70 #define DEFAULT_TRACE_TMP_LINE_LEN MBED_TRACE_TMP_LINE_LENGTH
maygup01 0:11cc2b7889af 71 #elif defined YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
maygup01 0:11cc2b7889af 72 #warning The YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN flag is deprecated and will be removed in the future! Use MBED_TRACE_TMP_LINE_LENGTH instead.
maygup01 0:11cc2b7889af 73 #define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
maygup01 0:11cc2b7889af 74 #elif defined YOTTA_CFG_MTRACE_TMP_LINE_LEN
maygup01 0:11cc2b7889af 75 #warning The YOTTA_CFG_MTRACE_TMP_LINE_LEN flag is deprecated and will be removed in the future! Use MBED_TRACE_TMP_LINE_LENGTH instead.
maygup01 0:11cc2b7889af 76 #define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MTRACE_TMP_LINE_LEN
maygup01 0:11cc2b7889af 77 #else
maygup01 0:11cc2b7889af 78 #define DEFAULT_TRACE_TMP_LINE_LEN 128
maygup01 0:11cc2b7889af 79 #endif
maygup01 0:11cc2b7889af 80
maygup01 0:11cc2b7889af 81 /** default max filters (include/exclude) length in bytes */
maygup01 0:11cc2b7889af 82 #ifdef MBED_TRACE_FILTER_LENGTH
maygup01 0:11cc2b7889af 83 #define DEFAULT_TRACE_FILTER_LENGTH MBED_TRACE_FILTER_LENGTH
maygup01 0:11cc2b7889af 84 #else
maygup01 0:11cc2b7889af 85 #define DEFAULT_TRACE_FILTER_LENGTH 24
maygup01 0:11cc2b7889af 86 #endif
maygup01 0:11cc2b7889af 87
maygup01 0:11cc2b7889af 88 /** default trace configuration bitmask */
maygup01 0:11cc2b7889af 89 #ifdef MBED_TRACE_CONFIG
maygup01 0:11cc2b7889af 90 #define DEFAULT_TRACE_CONFIG MBED_TRACE_CONFIG
maygup01 0:11cc2b7889af 91 #else
maygup01 0:11cc2b7889af 92 #define DEFAULT_TRACE_CONFIG TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN
maygup01 0:11cc2b7889af 93 #endif
maygup01 0:11cc2b7889af 94
maygup01 0:11cc2b7889af 95 /** default print function, just redirect str to printf */
maygup01 0:11cc2b7889af 96 static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length);
maygup01 0:11cc2b7889af 97 static void mbed_trace_default_print(const char *str);
maygup01 0:11cc2b7889af 98 static void mbed_trace_reset_tmp(void);
maygup01 0:11cc2b7889af 99
maygup01 0:11cc2b7889af 100 typedef struct trace_s {
maygup01 0:11cc2b7889af 101 /** trace configuration bits */
maygup01 0:11cc2b7889af 102 uint8_t trace_config;
maygup01 0:11cc2b7889af 103 /** exclude filters list, related group name */
maygup01 0:11cc2b7889af 104 char *filters_exclude;
maygup01 0:11cc2b7889af 105 /** include filters list, related group name */
maygup01 0:11cc2b7889af 106 char *filters_include;
maygup01 0:11cc2b7889af 107 /** Filters length */
maygup01 0:11cc2b7889af 108 int filters_length;
maygup01 0:11cc2b7889af 109 /** trace line */
maygup01 0:11cc2b7889af 110 char *line;
maygup01 0:11cc2b7889af 111 /** trace line length */
maygup01 0:11cc2b7889af 112 int line_length;
maygup01 0:11cc2b7889af 113 /** temporary data */
maygup01 0:11cc2b7889af 114 char *tmp_data;
maygup01 0:11cc2b7889af 115 /** temporary data array length */
maygup01 0:11cc2b7889af 116 int tmp_data_length;
maygup01 0:11cc2b7889af 117 /** temporary data pointer */
maygup01 0:11cc2b7889af 118 char *tmp_data_ptr;
maygup01 0:11cc2b7889af 119
maygup01 0:11cc2b7889af 120 /** prefix function, which can be used to put time to the trace line */
maygup01 0:11cc2b7889af 121 char *(*prefix_f)(size_t);
maygup01 0:11cc2b7889af 122 /** suffix function, which can be used to some string to the end of trace line */
maygup01 0:11cc2b7889af 123 char *(*suffix_f)(void);
maygup01 0:11cc2b7889af 124 /** print out function. Can be redirect to flash for example. */
maygup01 0:11cc2b7889af 125 void (*printf)(const char *);
maygup01 0:11cc2b7889af 126 /** print out function for TRACE_LEVEL_CMD */
maygup01 0:11cc2b7889af 127 void (*cmd_printf)(const char *);
maygup01 0:11cc2b7889af 128 /** mutex wait function which can be called to lock against a mutex. */
maygup01 0:11cc2b7889af 129 void (*mutex_wait_f)(void);
maygup01 0:11cc2b7889af 130 /** mutex release function which must be used to release the mutex locked by mutex_wait_f. */
maygup01 0:11cc2b7889af 131 void (*mutex_release_f)(void);
maygup01 0:11cc2b7889af 132 /** number of times the mutex has been locked */
maygup01 0:11cc2b7889af 133 int mutex_lock_count;
maygup01 0:11cc2b7889af 134 } trace_t;
maygup01 0:11cc2b7889af 135
maygup01 0:11cc2b7889af 136 static trace_t m_trace = {
maygup01 0:11cc2b7889af 137 .trace_config = DEFAULT_TRACE_CONFIG,
maygup01 0:11cc2b7889af 138 .filters_exclude = 0,
maygup01 0:11cc2b7889af 139 .filters_include = 0,
maygup01 0:11cc2b7889af 140 .filters_length = DEFAULT_TRACE_FILTER_LENGTH,
maygup01 0:11cc2b7889af 141 .line = 0,
maygup01 0:11cc2b7889af 142 .line_length = DEFAULT_TRACE_LINE_LENGTH,
maygup01 0:11cc2b7889af 143 .tmp_data = 0,
maygup01 0:11cc2b7889af 144 .tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN,
maygup01 0:11cc2b7889af 145 .prefix_f = 0,
maygup01 0:11cc2b7889af 146 .suffix_f = 0,
maygup01 0:11cc2b7889af 147 .printf = mbed_trace_default_print,
maygup01 0:11cc2b7889af 148 .cmd_printf = 0,
maygup01 0:11cc2b7889af 149 .mutex_wait_f = 0,
maygup01 0:11cc2b7889af 150 .mutex_release_f = 0,
maygup01 0:11cc2b7889af 151 .mutex_lock_count = 0
maygup01 0:11cc2b7889af 152 };
maygup01 0:11cc2b7889af 153
maygup01 0:11cc2b7889af 154 int mbed_trace_init(void)
maygup01 0:11cc2b7889af 155 {
maygup01 0:11cc2b7889af 156 if (m_trace.line == NULL) {
maygup01 0:11cc2b7889af 157 m_trace.line = MBED_TRACE_MEM_ALLOC(m_trace.line_length);
maygup01 0:11cc2b7889af 158 }
maygup01 0:11cc2b7889af 159
maygup01 0:11cc2b7889af 160 if (m_trace.tmp_data == NULL) {
maygup01 0:11cc2b7889af 161 m_trace.tmp_data = MBED_TRACE_MEM_ALLOC(m_trace.tmp_data_length);
maygup01 0:11cc2b7889af 162 }
maygup01 0:11cc2b7889af 163 m_trace.tmp_data_ptr = m_trace.tmp_data;
maygup01 0:11cc2b7889af 164
maygup01 0:11cc2b7889af 165 if (m_trace.filters_exclude == NULL) {
maygup01 0:11cc2b7889af 166 m_trace.filters_exclude = MBED_TRACE_MEM_ALLOC(m_trace.filters_length);
maygup01 0:11cc2b7889af 167 }
maygup01 0:11cc2b7889af 168 if (m_trace.filters_include == NULL) {
maygup01 0:11cc2b7889af 169 m_trace.filters_include = MBED_TRACE_MEM_ALLOC(m_trace.filters_length);
maygup01 0:11cc2b7889af 170 }
maygup01 0:11cc2b7889af 171
maygup01 0:11cc2b7889af 172 if (m_trace.line == NULL ||
maygup01 0:11cc2b7889af 173 m_trace.tmp_data == NULL ||
maygup01 0:11cc2b7889af 174 m_trace.filters_exclude == NULL ||
maygup01 0:11cc2b7889af 175 m_trace.filters_include == NULL) {
maygup01 0:11cc2b7889af 176 //memory allocation fail
maygup01 0:11cc2b7889af 177 mbed_trace_free();
maygup01 0:11cc2b7889af 178 return -1;
maygup01 0:11cc2b7889af 179 }
maygup01 0:11cc2b7889af 180 memset(m_trace.tmp_data, 0, m_trace.tmp_data_length);
maygup01 0:11cc2b7889af 181 memset(m_trace.filters_exclude, 0, m_trace.filters_length);
maygup01 0:11cc2b7889af 182 memset(m_trace.filters_include, 0, m_trace.filters_length);
maygup01 0:11cc2b7889af 183 memset(m_trace.line, 0, m_trace.line_length);
maygup01 0:11cc2b7889af 184
maygup01 0:11cc2b7889af 185 return 0;
maygup01 0:11cc2b7889af 186 }
maygup01 0:11cc2b7889af 187 void mbed_trace_free(void)
maygup01 0:11cc2b7889af 188 {
maygup01 0:11cc2b7889af 189 // release memory
maygup01 0:11cc2b7889af 190 MBED_TRACE_MEM_FREE(m_trace.line);
maygup01 0:11cc2b7889af 191 MBED_TRACE_MEM_FREE(m_trace.tmp_data);
maygup01 0:11cc2b7889af 192 MBED_TRACE_MEM_FREE(m_trace.filters_exclude);
maygup01 0:11cc2b7889af 193 MBED_TRACE_MEM_FREE(m_trace.filters_include);
maygup01 0:11cc2b7889af 194
maygup01 0:11cc2b7889af 195 // reset to default values
maygup01 0:11cc2b7889af 196 m_trace.trace_config = DEFAULT_TRACE_CONFIG;
maygup01 0:11cc2b7889af 197 m_trace.filters_exclude = 0;
maygup01 0:11cc2b7889af 198 m_trace.filters_include = 0;
maygup01 0:11cc2b7889af 199 m_trace.filters_length = DEFAULT_TRACE_FILTER_LENGTH;
maygup01 0:11cc2b7889af 200 m_trace.line = 0;
maygup01 0:11cc2b7889af 201 m_trace.line_length = DEFAULT_TRACE_LINE_LENGTH;
maygup01 0:11cc2b7889af 202 m_trace.tmp_data = 0;
maygup01 0:11cc2b7889af 203 m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;
maygup01 0:11cc2b7889af 204 m_trace.prefix_f = 0;
maygup01 0:11cc2b7889af 205 m_trace.suffix_f = 0;
maygup01 0:11cc2b7889af 206 m_trace.printf = mbed_trace_default_print;
maygup01 0:11cc2b7889af 207 m_trace.cmd_printf = 0;
maygup01 0:11cc2b7889af 208 m_trace.mutex_wait_f = 0;
maygup01 0:11cc2b7889af 209 m_trace.mutex_release_f = 0;
maygup01 0:11cc2b7889af 210 m_trace.mutex_lock_count = 0;
maygup01 0:11cc2b7889af 211 }
maygup01 0:11cc2b7889af 212 static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length)
maygup01 0:11cc2b7889af 213 {
maygup01 0:11cc2b7889af 214 MBED_TRACE_MEM_FREE(*buffer);
maygup01 0:11cc2b7889af 215 *buffer = MBED_TRACE_MEM_ALLOC(new_length);
maygup01 0:11cc2b7889af 216 *length_ptr = new_length;
maygup01 0:11cc2b7889af 217 }
maygup01 0:11cc2b7889af 218 void mbed_trace_buffer_sizes(int lineLength, int tmpLength)
maygup01 0:11cc2b7889af 219 {
maygup01 0:11cc2b7889af 220 if( lineLength > 0 ) {
maygup01 0:11cc2b7889af 221 mbed_trace_realloc( &(m_trace.line), &m_trace.line_length, lineLength );
maygup01 0:11cc2b7889af 222 }
maygup01 0:11cc2b7889af 223 if( tmpLength > 0 ) {
maygup01 0:11cc2b7889af 224 mbed_trace_realloc( &(m_trace.tmp_data), &m_trace.tmp_data_length, tmpLength);
maygup01 0:11cc2b7889af 225 mbed_trace_reset_tmp();
maygup01 0:11cc2b7889af 226 }
maygup01 0:11cc2b7889af 227 }
maygup01 0:11cc2b7889af 228 void mbed_trace_config_set(uint8_t config)
maygup01 0:11cc2b7889af 229 {
maygup01 0:11cc2b7889af 230 m_trace.trace_config = config;
maygup01 0:11cc2b7889af 231 }
maygup01 0:11cc2b7889af 232 uint8_t mbed_trace_config_get(void)
maygup01 0:11cc2b7889af 233 {
maygup01 0:11cc2b7889af 234 return m_trace.trace_config;
maygup01 0:11cc2b7889af 235 }
maygup01 0:11cc2b7889af 236 void mbed_trace_prefix_function_set(char *(*pref_f)(size_t))
maygup01 0:11cc2b7889af 237 {
maygup01 0:11cc2b7889af 238 m_trace.prefix_f = pref_f;
maygup01 0:11cc2b7889af 239 }
maygup01 0:11cc2b7889af 240 void mbed_trace_suffix_function_set(char *(*suffix_f)(void))
maygup01 0:11cc2b7889af 241 {
maygup01 0:11cc2b7889af 242 m_trace.suffix_f = suffix_f;
maygup01 0:11cc2b7889af 243 }
maygup01 0:11cc2b7889af 244 void mbed_trace_print_function_set(void (*printf)(const char *))
maygup01 0:11cc2b7889af 245 {
maygup01 0:11cc2b7889af 246 m_trace.printf = printf;
maygup01 0:11cc2b7889af 247 }
maygup01 0:11cc2b7889af 248 void mbed_trace_cmdprint_function_set(void (*printf)(const char *))
maygup01 0:11cc2b7889af 249 {
maygup01 0:11cc2b7889af 250 m_trace.cmd_printf = printf;
maygup01 0:11cc2b7889af 251 }
maygup01 0:11cc2b7889af 252 void mbed_trace_mutex_wait_function_set(void (*mutex_wait_f)(void))
maygup01 0:11cc2b7889af 253 {
maygup01 0:11cc2b7889af 254 m_trace.mutex_wait_f = mutex_wait_f;
maygup01 0:11cc2b7889af 255 }
maygup01 0:11cc2b7889af 256 void mbed_trace_mutex_release_function_set(void (*mutex_release_f)(void))
maygup01 0:11cc2b7889af 257 {
maygup01 0:11cc2b7889af 258 m_trace.mutex_release_f = mutex_release_f;
maygup01 0:11cc2b7889af 259 }
maygup01 0:11cc2b7889af 260 void mbed_trace_exclude_filters_set(char *filters)
maygup01 0:11cc2b7889af 261 {
maygup01 0:11cc2b7889af 262 if (filters) {
maygup01 0:11cc2b7889af 263 (void)strncpy(m_trace.filters_exclude, filters, m_trace.filters_length);
maygup01 0:11cc2b7889af 264 } else {
maygup01 0:11cc2b7889af 265 m_trace.filters_exclude[0] = 0;
maygup01 0:11cc2b7889af 266 }
maygup01 0:11cc2b7889af 267 }
maygup01 0:11cc2b7889af 268 const char *mbed_trace_exclude_filters_get(void)
maygup01 0:11cc2b7889af 269 {
maygup01 0:11cc2b7889af 270 return m_trace.filters_exclude;
maygup01 0:11cc2b7889af 271 }
maygup01 0:11cc2b7889af 272 const char *mbed_trace_include_filters_get(void)
maygup01 0:11cc2b7889af 273 {
maygup01 0:11cc2b7889af 274 return m_trace.filters_include;
maygup01 0:11cc2b7889af 275 }
maygup01 0:11cc2b7889af 276 void mbed_trace_include_filters_set(char *filters)
maygup01 0:11cc2b7889af 277 {
maygup01 0:11cc2b7889af 278 if (filters) {
maygup01 0:11cc2b7889af 279 (void)strncpy(m_trace.filters_include, filters, m_trace.filters_length);
maygup01 0:11cc2b7889af 280 } else {
maygup01 0:11cc2b7889af 281 m_trace.filters_include[0] = 0;
maygup01 0:11cc2b7889af 282 }
maygup01 0:11cc2b7889af 283 }
maygup01 0:11cc2b7889af 284 static int8_t mbed_trace_skip(int8_t dlevel, const char *grp)
maygup01 0:11cc2b7889af 285 {
maygup01 0:11cc2b7889af 286 if (dlevel >= 0 && grp != 0) {
maygup01 0:11cc2b7889af 287 // filter debug prints only when dlevel is >0 and grp is given
maygup01 0:11cc2b7889af 288
maygup01 0:11cc2b7889af 289 /// @TODO this could be much better..
maygup01 0:11cc2b7889af 290 if (m_trace.filters_exclude[0] != '\0' &&
maygup01 0:11cc2b7889af 291 strstr(m_trace.filters_exclude, grp) != 0) {
maygup01 0:11cc2b7889af 292 //grp was in exclude list
maygup01 0:11cc2b7889af 293 return 1;
maygup01 0:11cc2b7889af 294 }
maygup01 0:11cc2b7889af 295 if (m_trace.filters_include[0] != '\0' &&
maygup01 0:11cc2b7889af 296 strstr(m_trace.filters_include, grp) == 0) {
maygup01 0:11cc2b7889af 297 //grp was in include list
maygup01 0:11cc2b7889af 298 return 1;
maygup01 0:11cc2b7889af 299 }
maygup01 0:11cc2b7889af 300 }
maygup01 0:11cc2b7889af 301 return 0;
maygup01 0:11cc2b7889af 302 }
maygup01 0:11cc2b7889af 303 static void mbed_trace_default_print(const char *str)
maygup01 0:11cc2b7889af 304 {
maygup01 0:11cc2b7889af 305 puts(str);
maygup01 0:11cc2b7889af 306 }
maygup01 0:11cc2b7889af 307 void mbed_tracef(uint8_t dlevel, const char *grp, const char *fmt, ...)
maygup01 0:11cc2b7889af 308 {
maygup01 0:11cc2b7889af 309 va_list ap;
maygup01 0:11cc2b7889af 310 va_start(ap, fmt);
maygup01 0:11cc2b7889af 311 mbed_vtracef(dlevel, grp, fmt, ap);
maygup01 0:11cc2b7889af 312 va_end(ap);
maygup01 0:11cc2b7889af 313 }
maygup01 0:11cc2b7889af 314 void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
maygup01 0:11cc2b7889af 315 {
maygup01 0:11cc2b7889af 316 if ( m_trace.mutex_wait_f ) {
maygup01 0:11cc2b7889af 317 m_trace.mutex_wait_f();
maygup01 0:11cc2b7889af 318 m_trace.mutex_lock_count++;
maygup01 0:11cc2b7889af 319 }
maygup01 0:11cc2b7889af 320
maygup01 0:11cc2b7889af 321 if (NULL == m_trace.line) {
maygup01 0:11cc2b7889af 322 goto end;
maygup01 0:11cc2b7889af 323 }
maygup01 0:11cc2b7889af 324
maygup01 0:11cc2b7889af 325 m_trace.line[0] = 0; //by default trace is empty
maygup01 0:11cc2b7889af 326
maygup01 0:11cc2b7889af 327 if (mbed_trace_skip(dlevel, grp) || fmt == 0 || grp == 0 || !m_trace.printf) {
maygup01 0:11cc2b7889af 328 //return tmp data pointer back to the beginning
maygup01 0:11cc2b7889af 329 mbed_trace_reset_tmp();
maygup01 0:11cc2b7889af 330 goto end;
maygup01 0:11cc2b7889af 331 }
maygup01 0:11cc2b7889af 332 if ((m_trace.trace_config & TRACE_MASK_LEVEL) & dlevel) {
maygup01 0:11cc2b7889af 333 bool color = (m_trace.trace_config & TRACE_MODE_COLOR) != 0;
maygup01 0:11cc2b7889af 334 bool plain = (m_trace.trace_config & TRACE_MODE_PLAIN) != 0;
maygup01 0:11cc2b7889af 335 bool cr = (m_trace.trace_config & TRACE_CARRIAGE_RETURN) != 0;
maygup01 0:11cc2b7889af 336
maygup01 0:11cc2b7889af 337 int retval = 0, bLeft = m_trace.line_length;
maygup01 0:11cc2b7889af 338 char *ptr = m_trace.line;
maygup01 0:11cc2b7889af 339 if (plain == true || dlevel == TRACE_LEVEL_CMD) {
maygup01 0:11cc2b7889af 340 //add trace data
maygup01 0:11cc2b7889af 341 retval = vsnprintf(ptr, bLeft, fmt, ap);
maygup01 0:11cc2b7889af 342 if (dlevel == TRACE_LEVEL_CMD && m_trace.cmd_printf) {
maygup01 0:11cc2b7889af 343 m_trace.cmd_printf(m_trace.line);
maygup01 0:11cc2b7889af 344 m_trace.cmd_printf("\n");
maygup01 0:11cc2b7889af 345 } else {
maygup01 0:11cc2b7889af 346 //print out whole data
maygup01 0:11cc2b7889af 347 m_trace.printf(m_trace.line);
maygup01 0:11cc2b7889af 348 }
maygup01 0:11cc2b7889af 349 } else {
maygup01 0:11cc2b7889af 350 if (color) {
maygup01 0:11cc2b7889af 351 if (cr) {
maygup01 0:11cc2b7889af 352 retval = snprintf(ptr, bLeft, "\r\x1b[2K");
maygup01 0:11cc2b7889af 353 if (retval >= bLeft) {
maygup01 0:11cc2b7889af 354 retval = 0;
maygup01 0:11cc2b7889af 355 }
maygup01 0:11cc2b7889af 356 if (retval > 0) {
maygup01 0:11cc2b7889af 357 ptr += retval;
maygup01 0:11cc2b7889af 358 bLeft -= retval;
maygup01 0:11cc2b7889af 359 }
maygup01 0:11cc2b7889af 360 }
maygup01 0:11cc2b7889af 361 if (bLeft > 0) {
maygup01 0:11cc2b7889af 362 //include color in ANSI/VT100 escape code
maygup01 0:11cc2b7889af 363 switch (dlevel) {
maygup01 0:11cc2b7889af 364 case (TRACE_LEVEL_ERROR):
maygup01 0:11cc2b7889af 365 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_ERROR);
maygup01 0:11cc2b7889af 366 break;
maygup01 0:11cc2b7889af 367 case (TRACE_LEVEL_WARN):
maygup01 0:11cc2b7889af 368 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_WARN);
maygup01 0:11cc2b7889af 369 break;
maygup01 0:11cc2b7889af 370 case (TRACE_LEVEL_INFO):
maygup01 0:11cc2b7889af 371 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_INFO);
maygup01 0:11cc2b7889af 372 break;
maygup01 0:11cc2b7889af 373 case (TRACE_LEVEL_DEBUG):
maygup01 0:11cc2b7889af 374 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_DEBUG);
maygup01 0:11cc2b7889af 375 break;
maygup01 0:11cc2b7889af 376 default:
maygup01 0:11cc2b7889af 377 color = 0; //avoid unneeded color-terminate code
maygup01 0:11cc2b7889af 378 retval = 0;
maygup01 0:11cc2b7889af 379 break;
maygup01 0:11cc2b7889af 380 }
maygup01 0:11cc2b7889af 381 if (retval >= bLeft) {
maygup01 0:11cc2b7889af 382 retval = 0;
maygup01 0:11cc2b7889af 383 }
maygup01 0:11cc2b7889af 384 if (retval > 0 && color) {
maygup01 0:11cc2b7889af 385 ptr += retval;
maygup01 0:11cc2b7889af 386 bLeft -= retval;
maygup01 0:11cc2b7889af 387 }
maygup01 0:11cc2b7889af 388 }
maygup01 0:11cc2b7889af 389
maygup01 0:11cc2b7889af 390 }
maygup01 0:11cc2b7889af 391 if (bLeft > 0 && m_trace.prefix_f) {
maygup01 0:11cc2b7889af 392 //find out length of body
maygup01 0:11cc2b7889af 393 size_t sz = 0;
maygup01 0:11cc2b7889af 394 va_list ap2;
maygup01 0:11cc2b7889af 395 va_copy(ap2, ap);
maygup01 0:11cc2b7889af 396 sz = vsnprintf(NULL, 0, fmt, ap2) + retval + (retval ? 4 : 0);
maygup01 0:11cc2b7889af 397 va_end(ap2);
maygup01 0:11cc2b7889af 398 //add prefix string
maygup01 0:11cc2b7889af 399 retval = snprintf(ptr, bLeft, "%s", m_trace.prefix_f(sz));
maygup01 0:11cc2b7889af 400 if (retval >= bLeft) {
maygup01 0:11cc2b7889af 401 retval = 0;
maygup01 0:11cc2b7889af 402 }
maygup01 0:11cc2b7889af 403 if (retval > 0) {
maygup01 0:11cc2b7889af 404 ptr += retval;
maygup01 0:11cc2b7889af 405 bLeft -= retval;
maygup01 0:11cc2b7889af 406 }
maygup01 0:11cc2b7889af 407 }
maygup01 0:11cc2b7889af 408 if (bLeft > 0) {
maygup01 0:11cc2b7889af 409 //add group tag
maygup01 0:11cc2b7889af 410 switch (dlevel) {
maygup01 0:11cc2b7889af 411 case (TRACE_LEVEL_ERROR):
maygup01 0:11cc2b7889af 412 retval = snprintf(ptr, bLeft, "[ERR ][%-4s]: ", grp);
maygup01 0:11cc2b7889af 413 break;
maygup01 0:11cc2b7889af 414 case (TRACE_LEVEL_WARN):
maygup01 0:11cc2b7889af 415 retval = snprintf(ptr, bLeft, "[WARN][%-4s]: ", grp);
maygup01 0:11cc2b7889af 416 break;
maygup01 0:11cc2b7889af 417 case (TRACE_LEVEL_INFO):
maygup01 0:11cc2b7889af 418 retval = snprintf(ptr, bLeft, "[INFO][%-4s]: ", grp);
maygup01 0:11cc2b7889af 419 break;
maygup01 0:11cc2b7889af 420 case (TRACE_LEVEL_DEBUG):
maygup01 0:11cc2b7889af 421 retval = snprintf(ptr, bLeft, "[DBG ][%-4s]: ", grp);
maygup01 0:11cc2b7889af 422 break;
maygup01 0:11cc2b7889af 423 default:
maygup01 0:11cc2b7889af 424 retval = snprintf(ptr, bLeft, " ");
maygup01 0:11cc2b7889af 425 break;
maygup01 0:11cc2b7889af 426 }
maygup01 0:11cc2b7889af 427 if (retval >= bLeft) {
maygup01 0:11cc2b7889af 428 retval = 0;
maygup01 0:11cc2b7889af 429 }
maygup01 0:11cc2b7889af 430 if (retval > 0) {
maygup01 0:11cc2b7889af 431 ptr += retval;
maygup01 0:11cc2b7889af 432 bLeft -= retval;
maygup01 0:11cc2b7889af 433 }
maygup01 0:11cc2b7889af 434 }
maygup01 0:11cc2b7889af 435 if (retval > 0 && bLeft > 0) {
maygup01 0:11cc2b7889af 436 //add trace text
maygup01 0:11cc2b7889af 437 retval = vsnprintf(ptr, bLeft, fmt, ap);
maygup01 0:11cc2b7889af 438 if (retval >= bLeft) {
maygup01 0:11cc2b7889af 439 retval = 0;
maygup01 0:11cc2b7889af 440 }
maygup01 0:11cc2b7889af 441 if (retval > 0) {
maygup01 0:11cc2b7889af 442 ptr += retval;
maygup01 0:11cc2b7889af 443 bLeft -= retval;
maygup01 0:11cc2b7889af 444 }
maygup01 0:11cc2b7889af 445 }
maygup01 0:11cc2b7889af 446
maygup01 0:11cc2b7889af 447 if (retval > 0 && bLeft > 0 && m_trace.suffix_f) {
maygup01 0:11cc2b7889af 448 //add suffix string
maygup01 0:11cc2b7889af 449 retval = snprintf(ptr, bLeft, "%s", m_trace.suffix_f());
maygup01 0:11cc2b7889af 450 if (retval >= bLeft) {
maygup01 0:11cc2b7889af 451 retval = 0;
maygup01 0:11cc2b7889af 452 }
maygup01 0:11cc2b7889af 453 if (retval > 0) {
maygup01 0:11cc2b7889af 454 ptr += retval;
maygup01 0:11cc2b7889af 455 bLeft -= retval;
maygup01 0:11cc2b7889af 456 }
maygup01 0:11cc2b7889af 457 }
maygup01 0:11cc2b7889af 458
maygup01 0:11cc2b7889af 459 if (retval > 0 && bLeft > 0 && color) {
maygup01 0:11cc2b7889af 460 //add zero color VT100 when color mode
maygup01 0:11cc2b7889af 461 retval = snprintf(ptr, bLeft, "\x1b[0m");
maygup01 0:11cc2b7889af 462 if (retval >= bLeft) {
maygup01 0:11cc2b7889af 463 retval = 0;
maygup01 0:11cc2b7889af 464 }
maygup01 0:11cc2b7889af 465 if (retval > 0) {
maygup01 0:11cc2b7889af 466 // not used anymore
maygup01 0:11cc2b7889af 467 //ptr += retval;
maygup01 0:11cc2b7889af 468 //bLeft -= retval;
maygup01 0:11cc2b7889af 469 }
maygup01 0:11cc2b7889af 470 }
maygup01 0:11cc2b7889af 471 //print out whole data
maygup01 0:11cc2b7889af 472 m_trace.printf(m_trace.line);
maygup01 0:11cc2b7889af 473 }
maygup01 0:11cc2b7889af 474 //return tmp data pointer back to the beginning
maygup01 0:11cc2b7889af 475 mbed_trace_reset_tmp();
maygup01 0:11cc2b7889af 476 }
maygup01 0:11cc2b7889af 477
maygup01 0:11cc2b7889af 478 end:
maygup01 0:11cc2b7889af 479 if ( m_trace.mutex_release_f ) {
maygup01 0:11cc2b7889af 480 // Store the mutex lock count to temp variable so that it won't get
maygup01 0:11cc2b7889af 481 // clobbered during last loop iteration when mutex gets released
maygup01 0:11cc2b7889af 482 int count = m_trace.mutex_lock_count;
maygup01 0:11cc2b7889af 483 m_trace.mutex_lock_count = 0;
maygup01 0:11cc2b7889af 484 // Since the helper functions (eg. mbed_trace_array) are used like this:
maygup01 0:11cc2b7889af 485 // mbed_tracef(TRACE_LEVEL_INFO, "grp", "%s", mbed_trace_array(some_array))
maygup01 0:11cc2b7889af 486 // The helper function MUST acquire the mutex if it modifies any buffers. However
maygup01 0:11cc2b7889af 487 // it CANNOT unlock the mutex because that would allow another thread to acquire
maygup01 0:11cc2b7889af 488 // the mutex after helper function unlocks it and before mbed_tracef acquires it
maygup01 0:11cc2b7889af 489 // for itself. This means that here we have to unlock the mutex as many times
maygup01 0:11cc2b7889af 490 // as it was acquired by trace function and any possible helper functions.
maygup01 0:11cc2b7889af 491 do {
maygup01 0:11cc2b7889af 492 m_trace.mutex_release_f();
maygup01 0:11cc2b7889af 493 } while (--count > 0);
maygup01 0:11cc2b7889af 494 }
maygup01 0:11cc2b7889af 495 }
maygup01 0:11cc2b7889af 496 static void mbed_trace_reset_tmp(void)
maygup01 0:11cc2b7889af 497 {
maygup01 0:11cc2b7889af 498 m_trace.tmp_data_ptr = m_trace.tmp_data;
maygup01 0:11cc2b7889af 499 }
maygup01 0:11cc2b7889af 500 const char *mbed_trace_last(void)
maygup01 0:11cc2b7889af 501 {
maygup01 0:11cc2b7889af 502 return m_trace.line;
maygup01 0:11cc2b7889af 503 }
maygup01 0:11cc2b7889af 504 /* Helping functions */
maygup01 0:11cc2b7889af 505 #define tmp_data_left() m_trace.tmp_data_length-(m_trace.tmp_data_ptr-m_trace.tmp_data)
maygup01 0:11cc2b7889af 506 #if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
maygup01 0:11cc2b7889af 507 char *mbed_trace_ipv6(const void *addr_ptr)
maygup01 0:11cc2b7889af 508 {
maygup01 0:11cc2b7889af 509 /** Acquire mutex. It is released before returning from mbed_vtracef. */
maygup01 0:11cc2b7889af 510 if ( m_trace.mutex_wait_f ) {
maygup01 0:11cc2b7889af 511 m_trace.mutex_wait_f();
maygup01 0:11cc2b7889af 512 m_trace.mutex_lock_count++;
maygup01 0:11cc2b7889af 513 }
maygup01 0:11cc2b7889af 514 char *str = m_trace.tmp_data_ptr;
maygup01 0:11cc2b7889af 515 if (str == NULL) {
maygup01 0:11cc2b7889af 516 return "";
maygup01 0:11cc2b7889af 517 }
maygup01 0:11cc2b7889af 518 if (tmp_data_left() < 41) {
maygup01 0:11cc2b7889af 519 return "";
maygup01 0:11cc2b7889af 520 }
maygup01 0:11cc2b7889af 521 if (addr_ptr == NULL) {
maygup01 0:11cc2b7889af 522 return "<null>";
maygup01 0:11cc2b7889af 523 }
maygup01 0:11cc2b7889af 524 str[0] = 0;
maygup01 0:11cc2b7889af 525 m_trace.tmp_data_ptr += ip6tos(addr_ptr, str) + 1;
maygup01 0:11cc2b7889af 526 return str;
maygup01 0:11cc2b7889af 527 }
maygup01 0:11cc2b7889af 528 char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
maygup01 0:11cc2b7889af 529 {
maygup01 0:11cc2b7889af 530 /** Acquire mutex. It is released before returning from mbed_vtracef. */
maygup01 0:11cc2b7889af 531 if ( m_trace.mutex_wait_f ) {
maygup01 0:11cc2b7889af 532 m_trace.mutex_wait_f();
maygup01 0:11cc2b7889af 533 m_trace.mutex_lock_count++;
maygup01 0:11cc2b7889af 534 }
maygup01 0:11cc2b7889af 535 char *str = m_trace.tmp_data_ptr;
maygup01 0:11cc2b7889af 536 if (str == NULL) {
maygup01 0:11cc2b7889af 537 return "";
maygup01 0:11cc2b7889af 538 }
maygup01 0:11cc2b7889af 539 if (tmp_data_left() < 45) {
maygup01 0:11cc2b7889af 540 return "";
maygup01 0:11cc2b7889af 541 }
maygup01 0:11cc2b7889af 542
maygup01 0:11cc2b7889af 543 if ((prefix_len != 0 && prefix == NULL) || prefix_len > 128) {
maygup01 0:11cc2b7889af 544 return "<err>";
maygup01 0:11cc2b7889af 545 }
maygup01 0:11cc2b7889af 546
maygup01 0:11cc2b7889af 547 m_trace.tmp_data_ptr += ip6_prefix_tos(prefix, prefix_len, str) + 1;
maygup01 0:11cc2b7889af 548 return str;
maygup01 0:11cc2b7889af 549 }
maygup01 0:11cc2b7889af 550 #endif //MBED_CONF_MBED_TRACE_FEA_IPV6
maygup01 0:11cc2b7889af 551 char *mbed_trace_array(const uint8_t *buf, uint16_t len)
maygup01 0:11cc2b7889af 552 {
maygup01 0:11cc2b7889af 553 /** Acquire mutex. It is released before returning from mbed_vtracef. */
maygup01 0:11cc2b7889af 554 if ( m_trace.mutex_wait_f ) {
maygup01 0:11cc2b7889af 555 m_trace.mutex_wait_f();
maygup01 0:11cc2b7889af 556 m_trace.mutex_lock_count++;
maygup01 0:11cc2b7889af 557 }
maygup01 0:11cc2b7889af 558 int i, bLeft = tmp_data_left();
maygup01 0:11cc2b7889af 559 char *str, *wptr;
maygup01 0:11cc2b7889af 560 str = m_trace.tmp_data_ptr;
maygup01 0:11cc2b7889af 561 if (len == 0 || str == NULL || bLeft == 0) {
maygup01 0:11cc2b7889af 562 return "";
maygup01 0:11cc2b7889af 563 }
maygup01 0:11cc2b7889af 564 if (buf == NULL) {
maygup01 0:11cc2b7889af 565 return "<null>";
maygup01 0:11cc2b7889af 566 }
maygup01 0:11cc2b7889af 567 wptr = str;
maygup01 0:11cc2b7889af 568 wptr[0] = 0;
maygup01 0:11cc2b7889af 569 const uint8_t *ptr = buf;
maygup01 0:11cc2b7889af 570 char overflow = 0;
maygup01 0:11cc2b7889af 571 for (i = 0; i < len; i++) {
maygup01 0:11cc2b7889af 572 if (bLeft <= 3) {
maygup01 0:11cc2b7889af 573 overflow = 1;
maygup01 0:11cc2b7889af 574 break;
maygup01 0:11cc2b7889af 575 }
maygup01 0:11cc2b7889af 576 int retval = snprintf(wptr, bLeft, "%02x:", *ptr++);
maygup01 0:11cc2b7889af 577 if (retval <= 0 || retval > bLeft) {
maygup01 0:11cc2b7889af 578 break;
maygup01 0:11cc2b7889af 579 }
maygup01 0:11cc2b7889af 580 bLeft -= retval;
maygup01 0:11cc2b7889af 581 wptr += retval;
maygup01 0:11cc2b7889af 582 }
maygup01 0:11cc2b7889af 583 if (wptr > str) {
maygup01 0:11cc2b7889af 584 if( overflow ) {
maygup01 0:11cc2b7889af 585 // replace last character as 'star',
maygup01 0:11cc2b7889af 586 // which indicate buffer len is not enough
maygup01 0:11cc2b7889af 587 *(wptr - 1) = '*';
maygup01 0:11cc2b7889af 588 } else {
maygup01 0:11cc2b7889af 589 //null to replace last ':' character
maygup01 0:11cc2b7889af 590 *(wptr - 1) = 0;
maygup01 0:11cc2b7889af 591 }
maygup01 0:11cc2b7889af 592 }
maygup01 0:11cc2b7889af 593 m_trace.tmp_data_ptr = wptr;
maygup01 0:11cc2b7889af 594 return str;
maygup01 0:11cc2b7889af 595 }