This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:f7c60d3e7b8a 1 /*
maclobdell 0:f7c60d3e7b8a 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
maclobdell 0:f7c60d3e7b8a 3 * SPDX-License-Identifier: Apache-2.0
maclobdell 0:f7c60d3e7b8a 4 * Licensed under the Apache License, Version 2.0 (the License); you may
maclobdell 0:f7c60d3e7b8a 5 * not use this file except in compliance with the License.
maclobdell 0:f7c60d3e7b8a 6 * You may obtain a copy of the License at
maclobdell 0:f7c60d3e7b8a 7 *
maclobdell 0:f7c60d3e7b8a 8 * http://www.apache.org/licenses/LICENSE-2.0
maclobdell 0:f7c60d3e7b8a 9 *
maclobdell 0:f7c60d3e7b8a 10 * Unless required by applicable law or agreed to in writing, software
maclobdell 0:f7c60d3e7b8a 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
maclobdell 0:f7c60d3e7b8a 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maclobdell 0:f7c60d3e7b8a 13 * See the License for the specific language governing permissions and
maclobdell 0:f7c60d3e7b8a 14 * limitations under the License.
maclobdell 0:f7c60d3e7b8a 15 */
maclobdell 0:f7c60d3e7b8a 16 #include <stdio.h>
maclobdell 0:f7c60d3e7b8a 17 #include <string.h>
maclobdell 0:f7c60d3e7b8a 18 #include <stdarg.h>
maclobdell 0:f7c60d3e7b8a 19 #include <stdlib.h>
maclobdell 0:f7c60d3e7b8a 20
maclobdell 0:f7c60d3e7b8a 21 #define HAVE_DEBUG 1
maclobdell 0:f7c60d3e7b8a 22
maclobdell 0:f7c60d3e7b8a 23 #include "ns_trace.h"
maclobdell 0:f7c60d3e7b8a 24 #include "ip6string.h"
maclobdell 0:f7c60d3e7b8a 25 #include "common_functions.h"
maclobdell 0:f7c60d3e7b8a 26
maclobdell 0:f7c60d3e7b8a 27 #if defined(_WIN32) || defined(__unix__) || defined(__unix) || defined(unix) || (defined(YOTTA_CFG) && !defined(NDEBUG))
maclobdell 0:f7c60d3e7b8a 28 //NOTE! It is not allowed to use this MEM_ALLOC/MEM_FREE from interrupt context.
maclobdell 0:f7c60d3e7b8a 29 #ifndef MEM_ALLOC
maclobdell 0:f7c60d3e7b8a 30 #define MEM_ALLOC malloc
maclobdell 0:f7c60d3e7b8a 31 #endif
maclobdell 0:f7c60d3e7b8a 32 #ifndef MEM_FREE
maclobdell 0:f7c60d3e7b8a 33 #define MEM_FREE free
maclobdell 0:f7c60d3e7b8a 34 #endif
maclobdell 0:f7c60d3e7b8a 35 #else
maclobdell 0:f7c60d3e7b8a 36 #include "nsdynmemLIB.h"
maclobdell 0:f7c60d3e7b8a 37 #ifndef MEM_ALLOC
maclobdell 0:f7c60d3e7b8a 38 #define MEM_ALLOC ns_dyn_mem_alloc
maclobdell 0:f7c60d3e7b8a 39 #endif
maclobdell 0:f7c60d3e7b8a 40 #ifndef MEM_FREE
maclobdell 0:f7c60d3e7b8a 41 #define MEM_FREE ns_dyn_mem_free
maclobdell 0:f7c60d3e7b8a 42 #endif
maclobdell 0:f7c60d3e7b8a 43 #endif
maclobdell 0:f7c60d3e7b8a 44
maclobdell 0:f7c60d3e7b8a 45 #define VT100_COLOR_ERROR "\x1b[31m"
maclobdell 0:f7c60d3e7b8a 46 #define VT100_COLOR_WARN "\x1b[33m"
maclobdell 0:f7c60d3e7b8a 47 #define VT100_COLOR_INFO "\x1b[39m"
maclobdell 0:f7c60d3e7b8a 48 #define VT100_COLOR_DEBUG "\x1b[90m"
maclobdell 0:f7c60d3e7b8a 49
maclobdell 0:f7c60d3e7b8a 50 /** default max trace line size in bytes */
maclobdell 0:f7c60d3e7b8a 51 #define DEFAULT_TRACE_LINE_LENGTH 1024
maclobdell 0:f7c60d3e7b8a 52 /** default max temporary buffer size in bytes, used in
maclobdell 0:f7c60d3e7b8a 53 trace_ipv6, trace_array and trace_strn */
maclobdell 0:f7c60d3e7b8a 54 #define DEFAULT_TRACE_TMP_LINE_LEN 128
maclobdell 0:f7c60d3e7b8a 55 /** default max filters (include/exclude) length in bytes */
maclobdell 0:f7c60d3e7b8a 56 #define DEFAULT_TRACE_FILTER_LENGTH 24
maclobdell 0:f7c60d3e7b8a 57
maclobdell 0:f7c60d3e7b8a 58 /** default print function, just redirect str to printf */
maclobdell 0:f7c60d3e7b8a 59 static void default_print(const char *str);
maclobdell 0:f7c60d3e7b8a 60
maclobdell 0:f7c60d3e7b8a 61 typedef struct {
maclobdell 0:f7c60d3e7b8a 62 /** trace configuration bits */
maclobdell 0:f7c60d3e7b8a 63 uint8_t trace_config;
maclobdell 0:f7c60d3e7b8a 64 /** exclude filters list, related group name */
maclobdell 0:f7c60d3e7b8a 65 char *filters_exclude;
maclobdell 0:f7c60d3e7b8a 66 /** include filters list, related group name */
maclobdell 0:f7c60d3e7b8a 67 char *filters_include;
maclobdell 0:f7c60d3e7b8a 68 /** Filters length */
maclobdell 0:f7c60d3e7b8a 69 int filters_length;
maclobdell 0:f7c60d3e7b8a 70 /** trace line */
maclobdell 0:f7c60d3e7b8a 71 char *line;
maclobdell 0:f7c60d3e7b8a 72 /** trace line length */
maclobdell 0:f7c60d3e7b8a 73 int line_length;
maclobdell 0:f7c60d3e7b8a 74 /** temporary data */
maclobdell 0:f7c60d3e7b8a 75 char *tmp_data;
maclobdell 0:f7c60d3e7b8a 76 /** temporary data array length */
maclobdell 0:f7c60d3e7b8a 77 int tmp_data_length;
maclobdell 0:f7c60d3e7b8a 78 /** temporary data pointer */
maclobdell 0:f7c60d3e7b8a 79 char *tmp_data_ptr;
maclobdell 0:f7c60d3e7b8a 80
maclobdell 0:f7c60d3e7b8a 81 /** prefix function, which can be used to put time to the trace line */
maclobdell 0:f7c60d3e7b8a 82 char *(*prefix_f)(size_t);
maclobdell 0:f7c60d3e7b8a 83 /** suffix function, which can be used to some string to the end of trace line */
maclobdell 0:f7c60d3e7b8a 84 char *(*suffix_f)(void);
maclobdell 0:f7c60d3e7b8a 85 /** print out function. Can be redirect to flash for example. */
maclobdell 0:f7c60d3e7b8a 86 void (*printf)(const char *);
maclobdell 0:f7c60d3e7b8a 87 /** print out function for TRACE_LEVEL_CMD */
maclobdell 0:f7c60d3e7b8a 88 void (*cmd_printf)(const char *);
maclobdell 0:f7c60d3e7b8a 89 } trace_s;
maclobdell 0:f7c60d3e7b8a 90
maclobdell 0:f7c60d3e7b8a 91 static trace_s m_trace = {
maclobdell 0:f7c60d3e7b8a 92 .filters_exclude = 0,
maclobdell 0:f7c60d3e7b8a 93 .filters_include = 0,
maclobdell 0:f7c60d3e7b8a 94 .line = 0,
maclobdell 0:f7c60d3e7b8a 95 .tmp_data = 0,
maclobdell 0:f7c60d3e7b8a 96 .prefix_f = 0,
maclobdell 0:f7c60d3e7b8a 97 .suffix_f = 0,
maclobdell 0:f7c60d3e7b8a 98 .printf = 0,
maclobdell 0:f7c60d3e7b8a 99 .cmd_printf = 0
maclobdell 0:f7c60d3e7b8a 100 };
maclobdell 0:f7c60d3e7b8a 101
maclobdell 0:f7c60d3e7b8a 102 int trace_init(void)
maclobdell 0:f7c60d3e7b8a 103 {
maclobdell 0:f7c60d3e7b8a 104 m_trace.trace_config = TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN;
maclobdell 0:f7c60d3e7b8a 105 m_trace.line_length = DEFAULT_TRACE_LINE_LENGTH;
maclobdell 0:f7c60d3e7b8a 106 if (m_trace.line == NULL) {
maclobdell 0:f7c60d3e7b8a 107 m_trace.line = MEM_ALLOC(m_trace.line_length);
maclobdell 0:f7c60d3e7b8a 108 }
maclobdell 0:f7c60d3e7b8a 109 m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;
maclobdell 0:f7c60d3e7b8a 110 if (m_trace.tmp_data == NULL) {
maclobdell 0:f7c60d3e7b8a 111 m_trace.tmp_data = MEM_ALLOC(m_trace.tmp_data_length);
maclobdell 0:f7c60d3e7b8a 112 }
maclobdell 0:f7c60d3e7b8a 113 m_trace.tmp_data_ptr = m_trace.tmp_data;
maclobdell 0:f7c60d3e7b8a 114 m_trace.filters_length = DEFAULT_TRACE_FILTER_LENGTH;
maclobdell 0:f7c60d3e7b8a 115 if (m_trace.filters_exclude == NULL) {
maclobdell 0:f7c60d3e7b8a 116 m_trace.filters_exclude = MEM_ALLOC(m_trace.filters_length);
maclobdell 0:f7c60d3e7b8a 117 }
maclobdell 0:f7c60d3e7b8a 118 if (m_trace.filters_include == NULL) {
maclobdell 0:f7c60d3e7b8a 119 m_trace.filters_include = MEM_ALLOC(m_trace.filters_length);
maclobdell 0:f7c60d3e7b8a 120 }
maclobdell 0:f7c60d3e7b8a 121
maclobdell 0:f7c60d3e7b8a 122 if (m_trace.line == NULL ||
maclobdell 0:f7c60d3e7b8a 123 m_trace.tmp_data == NULL ||
maclobdell 0:f7c60d3e7b8a 124 m_trace.filters_exclude == NULL ||
maclobdell 0:f7c60d3e7b8a 125 m_trace.filters_include == NULL) {
maclobdell 0:f7c60d3e7b8a 126 //memory allocation fail
maclobdell 0:f7c60d3e7b8a 127 trace_free();
maclobdell 0:f7c60d3e7b8a 128 return -1;
maclobdell 0:f7c60d3e7b8a 129 }
maclobdell 0:f7c60d3e7b8a 130 memset(m_trace.tmp_data, 0, m_trace.tmp_data_length);
maclobdell 0:f7c60d3e7b8a 131 memset(m_trace.filters_exclude, 0, m_trace.filters_length);
maclobdell 0:f7c60d3e7b8a 132 memset(m_trace.filters_include, 0, m_trace.filters_length);
maclobdell 0:f7c60d3e7b8a 133 memset(m_trace.line, 0, m_trace.line_length);
maclobdell 0:f7c60d3e7b8a 134
maclobdell 0:f7c60d3e7b8a 135 m_trace.prefix_f = 0;
maclobdell 0:f7c60d3e7b8a 136 m_trace.suffix_f = 0;
maclobdell 0:f7c60d3e7b8a 137 m_trace.printf = default_print;
maclobdell 0:f7c60d3e7b8a 138 m_trace.cmd_printf = 0;
maclobdell 0:f7c60d3e7b8a 139
maclobdell 0:f7c60d3e7b8a 140 return 0;
maclobdell 0:f7c60d3e7b8a 141 }
maclobdell 0:f7c60d3e7b8a 142 void trace_free(void)
maclobdell 0:f7c60d3e7b8a 143 {
maclobdell 0:f7c60d3e7b8a 144 MEM_FREE(m_trace.line);
maclobdell 0:f7c60d3e7b8a 145 m_trace.line_length = 0;
maclobdell 0:f7c60d3e7b8a 146 m_trace.line = 0;
maclobdell 0:f7c60d3e7b8a 147 MEM_FREE(m_trace.tmp_data);
maclobdell 0:f7c60d3e7b8a 148 m_trace.tmp_data = 0;
maclobdell 0:f7c60d3e7b8a 149 m_trace.tmp_data_ptr = 0;
maclobdell 0:f7c60d3e7b8a 150 MEM_FREE(m_trace.filters_exclude);
maclobdell 0:f7c60d3e7b8a 151 m_trace.filters_exclude = 0;
maclobdell 0:f7c60d3e7b8a 152 MEM_FREE(m_trace.filters_include);
maclobdell 0:f7c60d3e7b8a 153 m_trace.filters_include = 0;
maclobdell 0:f7c60d3e7b8a 154 m_trace.filters_length = 0;
maclobdell 0:f7c60d3e7b8a 155 }
maclobdell 0:f7c60d3e7b8a 156 /** @TODO do we need dynamically change trace buffer sizes ?
maclobdell 0:f7c60d3e7b8a 157 // reconfigure trace buffer sizes
maclobdell 0:f7c60d3e7b8a 158 void set_trace_buffer_sizes(int lineLength, int tmpLength)
maclobdell 0:f7c60d3e7b8a 159 {
maclobdell 0:f7c60d3e7b8a 160 REALLOC( m_trace.line, dataLength );
maclobdell 0:f7c60d3e7b8a 161 REALLOC( m_trace.tmp_data, tmpLength);
maclobdell 0:f7c60d3e7b8a 162 m_trace.tmp_data_length = tmpLength;
maclobdell 0:f7c60d3e7b8a 163 }
maclobdell 0:f7c60d3e7b8a 164 */
maclobdell 0:f7c60d3e7b8a 165 void set_trace_config(uint8_t config)
maclobdell 0:f7c60d3e7b8a 166 {
maclobdell 0:f7c60d3e7b8a 167 m_trace.trace_config = config;
maclobdell 0:f7c60d3e7b8a 168 }
maclobdell 0:f7c60d3e7b8a 169 uint8_t get_trace_config(void)
maclobdell 0:f7c60d3e7b8a 170 {
maclobdell 0:f7c60d3e7b8a 171 return m_trace.trace_config;
maclobdell 0:f7c60d3e7b8a 172 }
maclobdell 0:f7c60d3e7b8a 173 void set_trace_prefix_function(char *(*pref_f)(size_t))
maclobdell 0:f7c60d3e7b8a 174 {
maclobdell 0:f7c60d3e7b8a 175 m_trace.prefix_f = pref_f;
maclobdell 0:f7c60d3e7b8a 176 }
maclobdell 0:f7c60d3e7b8a 177 void set_trace_suffix_function(char *(*suffix_f)(void))
maclobdell 0:f7c60d3e7b8a 178 {
maclobdell 0:f7c60d3e7b8a 179 m_trace.suffix_f = suffix_f;
maclobdell 0:f7c60d3e7b8a 180 }
maclobdell 0:f7c60d3e7b8a 181 void set_trace_print_function(void (*printf)(const char *))
maclobdell 0:f7c60d3e7b8a 182 {
maclobdell 0:f7c60d3e7b8a 183 m_trace.printf = printf;
maclobdell 0:f7c60d3e7b8a 184 }
maclobdell 0:f7c60d3e7b8a 185 void set_trace_cmdprint_function(void (*printf)(const char *))
maclobdell 0:f7c60d3e7b8a 186 {
maclobdell 0:f7c60d3e7b8a 187 m_trace.cmd_printf = printf;
maclobdell 0:f7c60d3e7b8a 188 }
maclobdell 0:f7c60d3e7b8a 189 void set_trace_exclude_filters(char *filters)
maclobdell 0:f7c60d3e7b8a 190 {
maclobdell 0:f7c60d3e7b8a 191 if (filters) {
maclobdell 0:f7c60d3e7b8a 192 (void)strncpy(m_trace.filters_exclude, filters, m_trace.filters_length);
maclobdell 0:f7c60d3e7b8a 193 } else {
maclobdell 0:f7c60d3e7b8a 194 m_trace.filters_exclude[0] = 0;
maclobdell 0:f7c60d3e7b8a 195 }
maclobdell 0:f7c60d3e7b8a 196 }
maclobdell 0:f7c60d3e7b8a 197 const char *get_trace_exclude_filters(void)
maclobdell 0:f7c60d3e7b8a 198 {
maclobdell 0:f7c60d3e7b8a 199 return m_trace.filters_exclude;
maclobdell 0:f7c60d3e7b8a 200 }
maclobdell 0:f7c60d3e7b8a 201 const char *get_trace_include_filters(void)
maclobdell 0:f7c60d3e7b8a 202 {
maclobdell 0:f7c60d3e7b8a 203 return m_trace.filters_include;
maclobdell 0:f7c60d3e7b8a 204 }
maclobdell 0:f7c60d3e7b8a 205 void set_trace_include_filters(char *filters)
maclobdell 0:f7c60d3e7b8a 206 {
maclobdell 0:f7c60d3e7b8a 207 if (filters) {
maclobdell 0:f7c60d3e7b8a 208 (void)strncpy(m_trace.filters_include, filters, m_trace.filters_length);
maclobdell 0:f7c60d3e7b8a 209 } else {
maclobdell 0:f7c60d3e7b8a 210 m_trace.filters_include[0] = 0;
maclobdell 0:f7c60d3e7b8a 211 }
maclobdell 0:f7c60d3e7b8a 212 }
maclobdell 0:f7c60d3e7b8a 213 static int8_t trace_skip(int8_t dlevel, const char *grp)
maclobdell 0:f7c60d3e7b8a 214 {
maclobdell 0:f7c60d3e7b8a 215 if (dlevel >= 0 && grp != 0) {
maclobdell 0:f7c60d3e7b8a 216 // filter debug prints only when dlevel is >0 and grp is given
maclobdell 0:f7c60d3e7b8a 217
maclobdell 0:f7c60d3e7b8a 218 /// @TODO this could be much better..
maclobdell 0:f7c60d3e7b8a 219 if (m_trace.filters_exclude[0] != '\0' &&
maclobdell 0:f7c60d3e7b8a 220 strstr(m_trace.filters_exclude, grp) != 0) {
maclobdell 0:f7c60d3e7b8a 221 //grp was in exclude list
maclobdell 0:f7c60d3e7b8a 222 return 1;
maclobdell 0:f7c60d3e7b8a 223 }
maclobdell 0:f7c60d3e7b8a 224 if (m_trace.filters_include[0] != '\0' &&
maclobdell 0:f7c60d3e7b8a 225 strstr(m_trace.filters_include, grp) == 0) {
maclobdell 0:f7c60d3e7b8a 226 //grp was in include list
maclobdell 0:f7c60d3e7b8a 227 return 1;
maclobdell 0:f7c60d3e7b8a 228 }
maclobdell 0:f7c60d3e7b8a 229 }
maclobdell 0:f7c60d3e7b8a 230 return 0;
maclobdell 0:f7c60d3e7b8a 231 }
maclobdell 0:f7c60d3e7b8a 232 static void default_print(const char *str)
maclobdell 0:f7c60d3e7b8a 233 {
maclobdell 0:f7c60d3e7b8a 234 puts(str);
maclobdell 0:f7c60d3e7b8a 235 }
maclobdell 0:f7c60d3e7b8a 236 void tracef(uint8_t dlevel, const char *grp, const char *fmt, ...)
maclobdell 0:f7c60d3e7b8a 237 {
maclobdell 0:f7c60d3e7b8a 238 if (m_trace.line == NULL) {
maclobdell 0:f7c60d3e7b8a 239 // Quite likely the trace_init() has not been called yet,
maclobdell 0:f7c60d3e7b8a 240 // but it is better to just shut up instead of crashing with
maclobdell 0:f7c60d3e7b8a 241 // null pointer dereference.
maclobdell 0:f7c60d3e7b8a 242 return;
maclobdell 0:f7c60d3e7b8a 243 }
maclobdell 0:f7c60d3e7b8a 244
maclobdell 0:f7c60d3e7b8a 245 m_trace.line[0] = 0; //by default trace is empty
maclobdell 0:f7c60d3e7b8a 246 if (trace_skip(dlevel, grp) || fmt == 0 || grp == 0) {
maclobdell 0:f7c60d3e7b8a 247 return;
maclobdell 0:f7c60d3e7b8a 248 }
maclobdell 0:f7c60d3e7b8a 249 if ((m_trace.trace_config & TRACE_MASK_LEVEL) & dlevel) {
maclobdell 0:f7c60d3e7b8a 250 bool color = (m_trace.trace_config & TRACE_MODE_COLOR) != 0;
maclobdell 0:f7c60d3e7b8a 251 bool plain = (m_trace.trace_config & TRACE_MODE_PLAIN) != 0;
maclobdell 0:f7c60d3e7b8a 252 bool cr = (m_trace.trace_config & TRACE_CARRIAGE_RETURN) != 0;
maclobdell 0:f7c60d3e7b8a 253 //printf("CFG: 0x%02x, plain: %i, color: %i, cr: %i\n", m_trace.trace_config, plain, color, cr);
maclobdell 0:f7c60d3e7b8a 254
maclobdell 0:f7c60d3e7b8a 255 int retval = 0, bLeft = m_trace.line_length;
maclobdell 0:f7c60d3e7b8a 256 char *ptr = m_trace.line;
maclobdell 0:f7c60d3e7b8a 257 if (plain == true || dlevel == TRACE_LEVEL_CMD) {
maclobdell 0:f7c60d3e7b8a 258 va_list ap;
maclobdell 0:f7c60d3e7b8a 259 va_start(ap, fmt);
maclobdell 0:f7c60d3e7b8a 260 //add trace data
maclobdell 0:f7c60d3e7b8a 261 retval = vsnprintf(ptr, bLeft, fmt, ap);
maclobdell 0:f7c60d3e7b8a 262 va_end(ap);
maclobdell 0:f7c60d3e7b8a 263 if (dlevel == TRACE_LEVEL_CMD && m_trace.cmd_printf) {
maclobdell 0:f7c60d3e7b8a 264 m_trace.cmd_printf(m_trace.line);
maclobdell 0:f7c60d3e7b8a 265 m_trace.cmd_printf("\n");
maclobdell 0:f7c60d3e7b8a 266 } else {
maclobdell 0:f7c60d3e7b8a 267 //print out whole data
maclobdell 0:f7c60d3e7b8a 268 m_trace.printf(m_trace.line);
maclobdell 0:f7c60d3e7b8a 269 }
maclobdell 0:f7c60d3e7b8a 270 } else {
maclobdell 0:f7c60d3e7b8a 271 if (color) {
maclobdell 0:f7c60d3e7b8a 272 if (cr) {
maclobdell 0:f7c60d3e7b8a 273 retval = snprintf(ptr, bLeft, "\r\x1b[2K");
maclobdell 0:f7c60d3e7b8a 274 if (retval >= bLeft) {
maclobdell 0:f7c60d3e7b8a 275 retval = 0;
maclobdell 0:f7c60d3e7b8a 276 }
maclobdell 0:f7c60d3e7b8a 277 if (retval > 0) {
maclobdell 0:f7c60d3e7b8a 278 ptr += retval;
maclobdell 0:f7c60d3e7b8a 279 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 280 }
maclobdell 0:f7c60d3e7b8a 281 }
maclobdell 0:f7c60d3e7b8a 282 if (bLeft > 0) {
maclobdell 0:f7c60d3e7b8a 283 //include color in ANSI/VT100 escape code
maclobdell 0:f7c60d3e7b8a 284 switch (dlevel) {
maclobdell 0:f7c60d3e7b8a 285 case (TRACE_LEVEL_ERROR):
maclobdell 0:f7c60d3e7b8a 286 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_ERROR);
maclobdell 0:f7c60d3e7b8a 287 break;
maclobdell 0:f7c60d3e7b8a 288 case (TRACE_LEVEL_WARN):
maclobdell 0:f7c60d3e7b8a 289 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_WARN);
maclobdell 0:f7c60d3e7b8a 290 break;
maclobdell 0:f7c60d3e7b8a 291 case (TRACE_LEVEL_INFO):
maclobdell 0:f7c60d3e7b8a 292 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_INFO);
maclobdell 0:f7c60d3e7b8a 293 break;
maclobdell 0:f7c60d3e7b8a 294 case (TRACE_LEVEL_DEBUG):
maclobdell 0:f7c60d3e7b8a 295 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_DEBUG);
maclobdell 0:f7c60d3e7b8a 296 break;
maclobdell 0:f7c60d3e7b8a 297 default:
maclobdell 0:f7c60d3e7b8a 298 color = 0; //avoid unneeded color-terminate code
maclobdell 0:f7c60d3e7b8a 299 retval = 0;
maclobdell 0:f7c60d3e7b8a 300 break;
maclobdell 0:f7c60d3e7b8a 301 }
maclobdell 0:f7c60d3e7b8a 302 if (retval >= bLeft) {
maclobdell 0:f7c60d3e7b8a 303 retval = 0;
maclobdell 0:f7c60d3e7b8a 304 }
maclobdell 0:f7c60d3e7b8a 305 if (retval > 0 && color) {
maclobdell 0:f7c60d3e7b8a 306 ptr += retval;
maclobdell 0:f7c60d3e7b8a 307 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 308 }
maclobdell 0:f7c60d3e7b8a 309 }
maclobdell 0:f7c60d3e7b8a 310
maclobdell 0:f7c60d3e7b8a 311 }
maclobdell 0:f7c60d3e7b8a 312 if (bLeft > 0 && m_trace.prefix_f) {
maclobdell 0:f7c60d3e7b8a 313 va_list ap;
maclobdell 0:f7c60d3e7b8a 314 va_start(ap, fmt);
maclobdell 0:f7c60d3e7b8a 315 //find out length of body
maclobdell 0:f7c60d3e7b8a 316 size_t sz = 0;
maclobdell 0:f7c60d3e7b8a 317 sz = vsnprintf(NULL, 0, fmt, ap) + retval + (retval ? 4 : 0);
maclobdell 0:f7c60d3e7b8a 318 //add prefix string
maclobdell 0:f7c60d3e7b8a 319 retval = snprintf(ptr, bLeft, "%s", m_trace.prefix_f(sz));
maclobdell 0:f7c60d3e7b8a 320 if (retval >= bLeft) {
maclobdell 0:f7c60d3e7b8a 321 retval = 0;
maclobdell 0:f7c60d3e7b8a 322 }
maclobdell 0:f7c60d3e7b8a 323 if (retval > 0) {
maclobdell 0:f7c60d3e7b8a 324 ptr += retval;
maclobdell 0:f7c60d3e7b8a 325 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 326 }
maclobdell 0:f7c60d3e7b8a 327 va_end(ap);
maclobdell 0:f7c60d3e7b8a 328 }
maclobdell 0:f7c60d3e7b8a 329 if (bLeft > 0) {
maclobdell 0:f7c60d3e7b8a 330 //add group tag
maclobdell 0:f7c60d3e7b8a 331 switch (dlevel) {
maclobdell 0:f7c60d3e7b8a 332 case (TRACE_LEVEL_ERROR):
maclobdell 0:f7c60d3e7b8a 333 retval = snprintf(ptr, bLeft, "[ERR ][%-4s]: ", grp);
maclobdell 0:f7c60d3e7b8a 334 break;
maclobdell 0:f7c60d3e7b8a 335 case (TRACE_LEVEL_WARN):
maclobdell 0:f7c60d3e7b8a 336 retval = snprintf(ptr, bLeft, "[WARN][%-4s]: ", grp);
maclobdell 0:f7c60d3e7b8a 337 break;
maclobdell 0:f7c60d3e7b8a 338 case (TRACE_LEVEL_INFO):
maclobdell 0:f7c60d3e7b8a 339 retval = snprintf(ptr, bLeft, "[INFO][%-4s]: ", grp);
maclobdell 0:f7c60d3e7b8a 340 break;
maclobdell 0:f7c60d3e7b8a 341 case (TRACE_LEVEL_DEBUG):
maclobdell 0:f7c60d3e7b8a 342 retval = snprintf(ptr, bLeft, "[DBG ][%-4s]: ", grp);
maclobdell 0:f7c60d3e7b8a 343 break;
maclobdell 0:f7c60d3e7b8a 344 default:
maclobdell 0:f7c60d3e7b8a 345 retval = snprintf(ptr, bLeft, " ");
maclobdell 0:f7c60d3e7b8a 346 break;
maclobdell 0:f7c60d3e7b8a 347 }
maclobdell 0:f7c60d3e7b8a 348 if (retval >= bLeft) {
maclobdell 0:f7c60d3e7b8a 349 retval = 0;
maclobdell 0:f7c60d3e7b8a 350 }
maclobdell 0:f7c60d3e7b8a 351 if (retval > 0) {
maclobdell 0:f7c60d3e7b8a 352 ptr += retval;
maclobdell 0:f7c60d3e7b8a 353 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 354 }
maclobdell 0:f7c60d3e7b8a 355 }
maclobdell 0:f7c60d3e7b8a 356 if (retval > 0 && bLeft > 0) {
maclobdell 0:f7c60d3e7b8a 357 va_list ap;
maclobdell 0:f7c60d3e7b8a 358 va_start(ap, fmt);
maclobdell 0:f7c60d3e7b8a 359 //add trace text
maclobdell 0:f7c60d3e7b8a 360 retval = vsnprintf(ptr, bLeft, fmt, ap);
maclobdell 0:f7c60d3e7b8a 361 if (retval >= bLeft) {
maclobdell 0:f7c60d3e7b8a 362 retval = 0;
maclobdell 0:f7c60d3e7b8a 363 }
maclobdell 0:f7c60d3e7b8a 364 if (retval > 0) {
maclobdell 0:f7c60d3e7b8a 365 ptr += retval;
maclobdell 0:f7c60d3e7b8a 366 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 367 }
maclobdell 0:f7c60d3e7b8a 368 va_end(ap);
maclobdell 0:f7c60d3e7b8a 369 }
maclobdell 0:f7c60d3e7b8a 370
maclobdell 0:f7c60d3e7b8a 371 if (retval > 0 && bLeft > 0 && m_trace.suffix_f) {
maclobdell 0:f7c60d3e7b8a 372 //add suffix string
maclobdell 0:f7c60d3e7b8a 373 retval = snprintf(ptr, bLeft, "%s", m_trace.suffix_f());
maclobdell 0:f7c60d3e7b8a 374 if (retval >= bLeft) {
maclobdell 0:f7c60d3e7b8a 375 retval = 0;
maclobdell 0:f7c60d3e7b8a 376 }
maclobdell 0:f7c60d3e7b8a 377 if (retval > 0) {
maclobdell 0:f7c60d3e7b8a 378 ptr += retval;
maclobdell 0:f7c60d3e7b8a 379 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 380 }
maclobdell 0:f7c60d3e7b8a 381 }
maclobdell 0:f7c60d3e7b8a 382
maclobdell 0:f7c60d3e7b8a 383 if (retval > 0 && bLeft > 0 && color) {
maclobdell 0:f7c60d3e7b8a 384 //add zero color VT100 when color mode
maclobdell 0:f7c60d3e7b8a 385 retval = snprintf(ptr, bLeft, "\x1b[0m");
maclobdell 0:f7c60d3e7b8a 386 if (retval >= bLeft) {
maclobdell 0:f7c60d3e7b8a 387 retval = 0;
maclobdell 0:f7c60d3e7b8a 388 }
maclobdell 0:f7c60d3e7b8a 389 if (retval > 0) {
maclobdell 0:f7c60d3e7b8a 390 ptr += retval;
maclobdell 0:f7c60d3e7b8a 391 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 392 }
maclobdell 0:f7c60d3e7b8a 393 }
maclobdell 0:f7c60d3e7b8a 394 //print out whole data
maclobdell 0:f7c60d3e7b8a 395 m_trace.printf(m_trace.line);
maclobdell 0:f7c60d3e7b8a 396 }
maclobdell 0:f7c60d3e7b8a 397 //return tmp data pointer back to the beginning
maclobdell 0:f7c60d3e7b8a 398 m_trace.tmp_data_ptr = m_trace.tmp_data;
maclobdell 0:f7c60d3e7b8a 399 }
maclobdell 0:f7c60d3e7b8a 400 }
maclobdell 0:f7c60d3e7b8a 401 const char *trace_last(void)
maclobdell 0:f7c60d3e7b8a 402 {
maclobdell 0:f7c60d3e7b8a 403 return m_trace.line;
maclobdell 0:f7c60d3e7b8a 404 }
maclobdell 0:f7c60d3e7b8a 405 /* Helping functions */
maclobdell 0:f7c60d3e7b8a 406 #define tmp_data_left() m_trace.tmp_data_length-(m_trace.tmp_data_ptr-m_trace.tmp_data)
maclobdell 0:f7c60d3e7b8a 407 char *trace_ipv6(const void *addr_ptr)
maclobdell 0:f7c60d3e7b8a 408 {
maclobdell 0:f7c60d3e7b8a 409 char *str = m_trace.tmp_data_ptr;
maclobdell 0:f7c60d3e7b8a 410 if (str == NULL) {
maclobdell 0:f7c60d3e7b8a 411 return "";
maclobdell 0:f7c60d3e7b8a 412 }
maclobdell 0:f7c60d3e7b8a 413 if (tmp_data_left() < 41) {
maclobdell 0:f7c60d3e7b8a 414 return "";
maclobdell 0:f7c60d3e7b8a 415 }
maclobdell 0:f7c60d3e7b8a 416 if (addr_ptr == NULL) {
maclobdell 0:f7c60d3e7b8a 417 return "<null>";
maclobdell 0:f7c60d3e7b8a 418 }
maclobdell 0:f7c60d3e7b8a 419 str[0] = 0;
maclobdell 0:f7c60d3e7b8a 420 ip6tos(addr_ptr, str);
maclobdell 0:f7c60d3e7b8a 421 m_trace.tmp_data_ptr += strlen(str) + 1;
maclobdell 0:f7c60d3e7b8a 422 return str;
maclobdell 0:f7c60d3e7b8a 423 }
maclobdell 0:f7c60d3e7b8a 424 char *trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
maclobdell 0:f7c60d3e7b8a 425 {
maclobdell 0:f7c60d3e7b8a 426 char *str = m_trace.tmp_data_ptr;
maclobdell 0:f7c60d3e7b8a 427 int retval, bLeft = tmp_data_left();
maclobdell 0:f7c60d3e7b8a 428 char tmp[40];
maclobdell 0:f7c60d3e7b8a 429 uint8_t addr[16] = {0};
maclobdell 0:f7c60d3e7b8a 430
maclobdell 0:f7c60d3e7b8a 431 if (str == NULL) {
maclobdell 0:f7c60d3e7b8a 432 return "";
maclobdell 0:f7c60d3e7b8a 433 }
maclobdell 0:f7c60d3e7b8a 434 if (bLeft < 43) {
maclobdell 0:f7c60d3e7b8a 435 return "";
maclobdell 0:f7c60d3e7b8a 436 }
maclobdell 0:f7c60d3e7b8a 437
maclobdell 0:f7c60d3e7b8a 438 if (prefix_len != 0) {
maclobdell 0:f7c60d3e7b8a 439 if (prefix == NULL || prefix_len > 128) {
maclobdell 0:f7c60d3e7b8a 440 return "<err>";
maclobdell 0:f7c60d3e7b8a 441 }
maclobdell 0:f7c60d3e7b8a 442 bitcopy(addr, prefix, prefix_len);
maclobdell 0:f7c60d3e7b8a 443 }
maclobdell 0:f7c60d3e7b8a 444
maclobdell 0:f7c60d3e7b8a 445 ip6tos(addr, tmp);
maclobdell 0:f7c60d3e7b8a 446 retval = snprintf(str, bLeft, "%s/%u", tmp, prefix_len);
maclobdell 0:f7c60d3e7b8a 447 if (retval <= 0 || retval > bLeft) {
maclobdell 0:f7c60d3e7b8a 448 return "";
maclobdell 0:f7c60d3e7b8a 449 }
maclobdell 0:f7c60d3e7b8a 450
maclobdell 0:f7c60d3e7b8a 451 m_trace.tmp_data_ptr += retval + 1;
maclobdell 0:f7c60d3e7b8a 452 return str;
maclobdell 0:f7c60d3e7b8a 453 }
maclobdell 0:f7c60d3e7b8a 454 char *trace_array(const uint8_t *buf, uint16_t len)
maclobdell 0:f7c60d3e7b8a 455 {
maclobdell 0:f7c60d3e7b8a 456 int i, retval, bLeft = tmp_data_left();
maclobdell 0:f7c60d3e7b8a 457 char *str, *wptr;
maclobdell 0:f7c60d3e7b8a 458 str = m_trace.tmp_data_ptr;
maclobdell 0:f7c60d3e7b8a 459 if (str == NULL) {
maclobdell 0:f7c60d3e7b8a 460 return "";
maclobdell 0:f7c60d3e7b8a 461 }
maclobdell 0:f7c60d3e7b8a 462 if (buf == NULL) {
maclobdell 0:f7c60d3e7b8a 463 return "<null>";
maclobdell 0:f7c60d3e7b8a 464 }
maclobdell 0:f7c60d3e7b8a 465 wptr = str;
maclobdell 0:f7c60d3e7b8a 466 wptr[0] = 0;
maclobdell 0:f7c60d3e7b8a 467 const uint8_t *ptr = buf;
maclobdell 0:f7c60d3e7b8a 468 for (i = 0; i < len; i++) {
maclobdell 0:f7c60d3e7b8a 469 if (bLeft <= 0) {
maclobdell 0:f7c60d3e7b8a 470 break;
maclobdell 0:f7c60d3e7b8a 471 }
maclobdell 0:f7c60d3e7b8a 472 retval = snprintf(wptr, bLeft, "%02x:", *ptr++);
maclobdell 0:f7c60d3e7b8a 473 if (retval <= 0 || retval > bLeft) {
maclobdell 0:f7c60d3e7b8a 474 break;
maclobdell 0:f7c60d3e7b8a 475 }
maclobdell 0:f7c60d3e7b8a 476 bLeft -= retval;
maclobdell 0:f7c60d3e7b8a 477 wptr += retval;
maclobdell 0:f7c60d3e7b8a 478 }
maclobdell 0:f7c60d3e7b8a 479 if (wptr > str) {
maclobdell 0:f7c60d3e7b8a 480 *(wptr - 1) = 0; //null to replace last ':' character
maclobdell 0:f7c60d3e7b8a 481 }
maclobdell 0:f7c60d3e7b8a 482 m_trace.tmp_data_ptr = wptr;
maclobdell 0:f7c60d3e7b8a 483 return str;
maclobdell 0:f7c60d3e7b8a 484 }
maclobdell 0:f7c60d3e7b8a 485
maclobdell 0:f7c60d3e7b8a 486 // rest of debug print functions will be obsolete and will be overridden with new trace interface..
maclobdell 0:f7c60d3e7b8a 487 void debugf(const char *fmt, ...)
maclobdell 0:f7c60d3e7b8a 488 {
maclobdell 0:f7c60d3e7b8a 489 va_list ap;
maclobdell 0:f7c60d3e7b8a 490 va_start(ap, fmt);
maclobdell 0:f7c60d3e7b8a 491 vprintf(fmt, ap);
maclobdell 0:f7c60d3e7b8a 492 va_end(ap);
maclobdell 0:f7c60d3e7b8a 493 }
maclobdell 0:f7c60d3e7b8a 494
maclobdell 0:f7c60d3e7b8a 495 void debug(const char *s)
maclobdell 0:f7c60d3e7b8a 496 {
maclobdell 0:f7c60d3e7b8a 497 fputs(s, stdout);
maclobdell 0:f7c60d3e7b8a 498 }
maclobdell 0:f7c60d3e7b8a 499
maclobdell 0:f7c60d3e7b8a 500 void debug_put(char c)
maclobdell 0:f7c60d3e7b8a 501 {
maclobdell 0:f7c60d3e7b8a 502 putchar(c);
maclobdell 0:f7c60d3e7b8a 503 }
maclobdell 0:f7c60d3e7b8a 504
maclobdell 0:f7c60d3e7b8a 505 void debug_hex(uint8_t x)
maclobdell 0:f7c60d3e7b8a 506 {
maclobdell 0:f7c60d3e7b8a 507 printf("%02x", x);
maclobdell 0:f7c60d3e7b8a 508 }
maclobdell 0:f7c60d3e7b8a 509 void debug_int(int i)
maclobdell 0:f7c60d3e7b8a 510 {
maclobdell 0:f7c60d3e7b8a 511 printf("%d", i);
maclobdell 0:f7c60d3e7b8a 512 }
maclobdell 0:f7c60d3e7b8a 513 void printf_array(const void *buf , uint16_t len)
maclobdell 0:f7c60d3e7b8a 514 {
maclobdell 0:f7c60d3e7b8a 515 int i;
maclobdell 0:f7c60d3e7b8a 516 const uint8_t *ptr = buf;
maclobdell 0:f7c60d3e7b8a 517 for (i = 0; i < len; i++) {
maclobdell 0:f7c60d3e7b8a 518 if (i && (0 == i % 16)) {
maclobdell 0:f7c60d3e7b8a 519 putchar('\n');
maclobdell 0:f7c60d3e7b8a 520 }
maclobdell 0:f7c60d3e7b8a 521 printf("%02x:", *ptr++);
maclobdell 0:f7c60d3e7b8a 522
maclobdell 0:f7c60d3e7b8a 523 }
maclobdell 0:f7c60d3e7b8a 524 putchar('\n');
maclobdell 0:f7c60d3e7b8a 525 }
maclobdell 0:f7c60d3e7b8a 526 void printf_ipv6_address(const void *addr_ptr)
maclobdell 0:f7c60d3e7b8a 527 {
maclobdell 0:f7c60d3e7b8a 528 char buf[40] = {0};
maclobdell 0:f7c60d3e7b8a 529 ip6tos(addr_ptr, buf);
maclobdell 0:f7c60d3e7b8a 530 printf("%s\n", buf);
maclobdell 0:f7c60d3e7b8a 531 }
maclobdell 0:f7c60d3e7b8a 532 void printf_string(const void *ptr, uint16_t len)
maclobdell 0:f7c60d3e7b8a 533 {
maclobdell 0:f7c60d3e7b8a 534 printf("%.*s\n", len, (const char *)ptr);
maclobdell 0:f7c60d3e7b8a 535 }