Christina Quast | b123d74 | 2014-12-23 13:03:36 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2008, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * \file |
| 32 | * |
| 33 | * \par Purpose |
| 34 | * |
| 35 | * Standard output methods for reporting debug information, warnings and |
| 36 | * errors, which can be easily be turned on/off. |
| 37 | * |
| 38 | * \par Usage |
| 39 | * -# Initialize the debug message port in application, for stdio printf(). |
| 40 | * -# Uses the TRACE_DEBUG(), TRACE_INFO(), TRACE_WARNING(), TRACE_ERROR() |
| 41 | * TRACE_FATAL() macros to output traces throughout the program. |
| 42 | * -# Each type of trace has a level : Debug 5, Info 4, Warning 3, Error 2 |
| 43 | * and Fatal 1. Disable a group of traces by changing the value of |
| 44 | * TRACE_LEVEL during compilation; traces with a level bigger than TRACE_LEVEL |
| 45 | * are not generated. To generate no trace, use the reserved value 0. |
| 46 | * -# Trace disabling can be static or dynamic. If dynamic disabling is selected |
| 47 | * the trace level can be modified in runtime. If static disabling is selected |
| 48 | * the disabled traces are not compiled. |
| 49 | * |
| 50 | * \par traceLevels Trace level description |
| 51 | * -# TRACE_DEBUG (5): Traces whose only purpose is for debugging the program, |
| 52 | * and which do not produce meaningful information otherwise. |
| 53 | * -# TRACE_INFO (4): Informational trace about the program execution. Should |
| 54 | * enable the user to see the execution flow. |
| 55 | * -# TRACE_WARNING (3): Indicates that a minor error has happened. In most case |
| 56 | * it can be discarded safely; it may even be expected. |
| 57 | * -# TRACE_ERROR (2): Indicates an error which may not stop the program execution, |
| 58 | * but which indicates there is a problem with the code. |
| 59 | * -# TRACE_FATAL (1): Indicates a major error which prevents the program from going |
| 60 | * any further. |
| 61 | */ |
| 62 | |
| 63 | #ifndef _USBLIB_TRACE_H |
| 64 | #define _USBLIB_TRACE_H |
| 65 | |
| 66 | /* |
| 67 | * Headers |
| 68 | */ |
| 69 | |
| 70 | #include <stdio.h> |
| 71 | |
| 72 | /* |
| 73 | * Global Definitions |
| 74 | */ |
| 75 | |
| 76 | /** Softpack Version */ |
| 77 | #define USBLIB_VERSION "0.1" |
| 78 | |
| 79 | #define TRACE_LEVEL_DEBUG 5 |
| 80 | #define TRACE_LEVEL_INFO 4 |
| 81 | #define TRACE_LEVEL_WARNING 3 |
| 82 | #define TRACE_LEVEL_ERROR 2 |
| 83 | #define TRACE_LEVEL_FATAL 1 |
| 84 | #define TRACE_LEVEL_NO_TRACE 0 |
| 85 | |
| 86 | /* By default, all traces are output except the debug one. */ |
| 87 | #if !defined(TRACE_LEVEL) |
| 88 | #define TRACE_LEVEL TRACE_LEVEL_INFO |
| 89 | #endif |
| 90 | |
| 91 | /* By default, trace level is static (not dynamic) */ |
| 92 | #if !defined(DYN_TRACES) |
| 93 | #define DYN_TRACES 0 |
| 94 | #endif |
| 95 | |
| 96 | #if defined(NOTRACE) |
| 97 | #error "Error: NOTRACE has to be not defined !" |
| 98 | #endif |
| 99 | |
| 100 | #undef NOTRACE |
| 101 | #if (DYN_TRACES==0) |
| 102 | #if (TRACE_LEVEL == TRACE_LEVEL_NO_TRACE) |
| 103 | #define NOTRACE |
| 104 | #endif |
| 105 | #endif |
| 106 | |
| 107 | |
| 108 | |
| 109 | /* ------------------------------------------------------------------------------ |
| 110 | * Global Macros |
| 111 | * ------------------------------------------------------------------------------ |
| 112 | */ |
| 113 | |
| 114 | #ifndef DYNTRACE |
| 115 | #define DYNTRACE 0 |
| 116 | #endif |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * Outputs a formatted string using 'printf' if the log level is high |
| 121 | * enough. Can be disabled by defining TRACE_LEVEL=0 during compilation. |
| 122 | * \param ... Additional parameters depending on formatted string. |
| 123 | */ |
| 124 | #if defined(NOTRACE) |
| 125 | |
| 126 | /* Empty macro */ |
| 127 | #define TRACE_DEBUG(...) { } |
| 128 | #define TRACE_INFO(...) { } |
| 129 | #define TRACE_WARNING(...) { } |
| 130 | #define TRACE_ERROR(...) { } |
| 131 | #define TRACE_FATAL(...) { while(1); } |
| 132 | |
| 133 | #define TRACE_DEBUG_WP(...) { } |
| 134 | #define TRACE_INFO_WP(...) { } |
| 135 | #define TRACE_WARNING_WP(...) { } |
| 136 | #define TRACE_ERROR_WP(...) { } |
| 137 | #define TRACE_FATAL_WP(...) { while(1); } |
| 138 | |
| 139 | #elif (DYN_TRACES == 1) |
| 140 | |
| 141 | /* Trace output depends on dwTraceLevel value */ |
| 142 | #define TRACE_DEBUG(...) { if (dwTraceLevel >= TRACE_LEVEL_DEBUG) { printf("-D- " __VA_ARGS__); } } |
| 143 | #define TRACE_INFO(...) { if (dwTraceLevel >= TRACE_LEVEL_INFO) { printf("-I- " __VA_ARGS__); } } |
| 144 | #define TRACE_WARNING(...) { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf("-W- " __VA_ARGS__); } } |
| 145 | #define TRACE_ERROR(...) { if (dwTraceLevel >= TRACE_LEVEL_ERROR) { printf("-E- " __VA_ARGS__); } } |
| 146 | #define TRACE_FATAL(...) { if (dwTraceLevel >= TRACE_LEVEL_FATAL) { printf("-F- " __VA_ARGS__); while(1); } } |
| 147 | |
| 148 | #define TRACE_DEBUG_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_DEBUG) { printf(__VA_ARGS__); } } |
| 149 | #define TRACE_INFO_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_INFO) { printf(__VA_ARGS__); } } |
| 150 | #define TRACE_WARNING_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_WARNING) { printf(__VA_ARGS__); } } |
| 151 | #define TRACE_ERROR_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_ERROR) { printf(__VA_ARGS__); } } |
| 152 | #define TRACE_FATAL_WP(...) { if (dwTraceLevel >= TRACE_LEVEL_FATAL) { printf(__VA_ARGS__); while(1); } } |
| 153 | |
| 154 | #else |
| 155 | |
| 156 | /* Trace compilation depends on TRACE_LEVEL value */ |
| 157 | #if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG) |
| 158 | #define TRACE_DEBUG(...) { printf("-D- " __VA_ARGS__); } |
| 159 | #define TRACE_DEBUG_WP(...) { printf(__VA_ARGS__); } |
| 160 | #else |
| 161 | #define TRACE_DEBUG(...) { } |
| 162 | #define TRACE_DEBUG_WP(...) { } |
| 163 | #endif |
| 164 | |
| 165 | #if (TRACE_LEVEL >= TRACE_LEVEL_INFO) |
| 166 | #define TRACE_INFO(...) { printf("-I- " __VA_ARGS__); } |
| 167 | #define TRACE_INFO_WP(...) { printf(__VA_ARGS__); } |
| 168 | #else |
| 169 | #define TRACE_INFO(...) { } |
| 170 | #define TRACE_INFO_WP(...) { } |
| 171 | #endif |
| 172 | |
| 173 | #if (TRACE_LEVEL >= TRACE_LEVEL_WARNING) |
| 174 | #define TRACE_WARNING(...) { printf("-W- " __VA_ARGS__); } |
| 175 | #define TRACE_WARNING_WP(...) { printf(__VA_ARGS__); } |
| 176 | #else |
| 177 | #define TRACE_WARNING(...) { } |
| 178 | #define TRACE_WARNING_WP(...) { } |
| 179 | #endif |
| 180 | |
| 181 | #if (TRACE_LEVEL >= TRACE_LEVEL_ERROR) |
| 182 | #define TRACE_ERROR(...) { printf("-E- " __VA_ARGS__); } |
| 183 | #define TRACE_ERROR_WP(...) { printf(__VA_ARGS__); } |
| 184 | #else |
| 185 | #define TRACE_ERROR(...) { } |
| 186 | #define TRACE_ERROR_WP(...) { } |
| 187 | #endif |
| 188 | |
| 189 | #if (TRACE_LEVEL >= TRACE_LEVEL_FATAL) |
| 190 | #define TRACE_FATAL(...) { printf("-F- " __VA_ARGS__); while(1); } |
| 191 | #define TRACE_FATAL_WP(...) { printf(__VA_ARGS__); while(1); } |
| 192 | #else |
| 193 | #define TRACE_FATAL(...) { while(1); } |
| 194 | #define TRACE_FATAL_WP(...) { while(1); } |
| 195 | #endif |
| 196 | |
| 197 | #endif |
| 198 | |
Christina Quast | 391dc2e | 2015-03-13 12:02:31 +0100 | [diff] [blame] | 199 | #if defined(USB_NO_DEBUG) |
| 200 | #undef TRACE_DEBUG_WP |
| 201 | #undef TRACE_INFO_WP |
| 202 | #endif |
Christina Quast | b123d74 | 2014-12-23 13:03:36 +0100 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * Exported variables |
| 206 | */ |
| 207 | /** Depending on DYN_TRACES, dwTraceLevel is a modifable runtime variable or a define */ |
| 208 | #if !defined(NOTRACE) && (DYN_TRACES == 1) |
| 209 | extern uint32_t dwTraceLevel ; |
| 210 | #endif |
| 211 | |
| 212 | #endif /* #ifndef _USBLIB_TRACE_H */ |
| 213 | |