Debug library for instrumentation a.k.a printf debugging
Dependents: pyrocommander Projektni_zadatak_Analogni_sat ProjetOctopode
A debug library for instrumentation a.k.a printf debugging.
The aims of the library are:
- Provide functions for instrumentation debugging that are simple and practical
- Avoid people reinventing the wheel. Without a standard approach to debugging, each projects invents their own DBG, xprintf, dprintf type macros
- And they are all slightly different, and possibly non-composable/compatible - consistency is good!
- Provide support for statically selective debugging, such as enabling debug in a module or class (vs. all or nothing)
- Allow debug to be included as part of library code bases, and only enabled when needed (rather than used during development, then commented out/removed)
- Eliminate at compile time the calls/message strings of disabled debug, so instrumentation can be left in at zero cost
- Allow options for dynamic debugging (can be programatically enabled)
- Use stderr as debug stream, and ensure it can be re-routed to different stream devices
Usage
Basic debug
#include "mbed.h" #include "debug.h" int main() { debug("Hello debug world"); int v = 5; debug("Hello debug world, v = %d", v); }
Conditional debug (static)
#include "mbed.h" #include "debug.h" #define DEBUG_MAIN 1 int main() { debug(DEBUG_MAIN, "Hello debug world"); int v = 5; debug(DEBUG_MAIN, "Hello debug world, v = %d", v); }
- Recommended define format: DEBUG_<module> or DEBUG_<module>_<aspect>
- e.g. DEBUG_LCD, DEBUG_LCD_REDRAW
Conditional debug (dynamic)
#include "mbed.h" #include "debug.h" int main() { bool debug_main = 0; debug(debug_main, "Hello debug world"); debug_main = 1; int v = 5; debug(debug_main, "Hello debug world, v = %d", v); for(int i=0; i<10; i++) { debug(i > 7, "Debug something"); } }
Redirecting stderr
#include "mbed.h" #include "debug.h" LocalFileSystem local("local"); int main() { freopen("/local/debug.txt", "w", stderr); debug("Hello debug file"); fclose(stderr); }
debug.h@0:aa1c8e69d98f, 2012-08-04 (annotated)
- Committer:
- simon
- Date:
- Sat Aug 04 21:04:21 2012 +0000
- Revision:
- 0:aa1c8e69d98f
- Child:
- 1:12ff10369961
Initial draft of instrumentation debug library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:aa1c8e69d98f | 1 | /* Copyright (c) 2012 mbed.org, MIT License |
simon | 0:aa1c8e69d98f | 2 | * |
simon | 0:aa1c8e69d98f | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
simon | 0:aa1c8e69d98f | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
simon | 0:aa1c8e69d98f | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
simon | 0:aa1c8e69d98f | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
simon | 0:aa1c8e69d98f | 7 | * furnished to do so, subject to the following conditions: |
simon | 0:aa1c8e69d98f | 8 | * |
simon | 0:aa1c8e69d98f | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
simon | 0:aa1c8e69d98f | 10 | * substantial portions of the Software. |
simon | 0:aa1c8e69d98f | 11 | * |
simon | 0:aa1c8e69d98f | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
simon | 0:aa1c8e69d98f | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
simon | 0:aa1c8e69d98f | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
simon | 0:aa1c8e69d98f | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
simon | 0:aa1c8e69d98f | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
simon | 0:aa1c8e69d98f | 17 | */ |
simon | 0:aa1c8e69d98f | 18 | |
simon | 0:aa1c8e69d98f | 19 | #ifndef DEBUG_H |
simon | 0:aa1c8e69d98f | 20 | #define DEBUG_H |
simon | 0:aa1c8e69d98f | 21 | |
simon | 0:aa1c8e69d98f | 22 | #ifndef NDEBUG |
simon | 0:aa1c8e69d98f | 23 | |
simon | 0:aa1c8e69d98f | 24 | #include <stdarg.h> |
simon | 0:aa1c8e69d98f | 25 | #include <stdio.h> |
simon | 0:aa1c8e69d98f | 26 | |
simon | 0:aa1c8e69d98f | 27 | static inline void debug(const char *format, ...) { |
simon | 0:aa1c8e69d98f | 28 | va_list args; |
simon | 0:aa1c8e69d98f | 29 | va_start(args, format); |
simon | 0:aa1c8e69d98f | 30 | vfprintf(stderr, format, args); |
simon | 0:aa1c8e69d98f | 31 | va_end(args); |
simon | 0:aa1c8e69d98f | 32 | } |
simon | 0:aa1c8e69d98f | 33 | |
simon | 0:aa1c8e69d98f | 34 | static inline void debug(bool condition, const char *format, ...) { |
simon | 0:aa1c8e69d98f | 35 | if(condition) { |
simon | 0:aa1c8e69d98f | 36 | va_list args; |
simon | 0:aa1c8e69d98f | 37 | va_start(args, format); |
simon | 0:aa1c8e69d98f | 38 | vfprintf(stderr, format, args); |
simon | 0:aa1c8e69d98f | 39 | va_end(args); |
simon | 0:aa1c8e69d98f | 40 | } |
simon | 0:aa1c8e69d98f | 41 | } |
simon | 0:aa1c8e69d98f | 42 | |
simon | 0:aa1c8e69d98f | 43 | #else |
simon | 0:aa1c8e69d98f | 44 | |
simon | 0:aa1c8e69d98f | 45 | static inline void debug(const char *format, ...) {} |
simon | 0:aa1c8e69d98f | 46 | static inline void debug(bool condition, const char *format, ...) {} |
simon | 0:aa1c8e69d98f | 47 | |
simon | 0:aa1c8e69d98f | 48 | #endif |
simon | 0:aa1c8e69d98f | 49 | |
simon | 0:aa1c8e69d98f | 50 | #endif |