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