Debug library
Dependents: NetworkingCoreLib LwIPNetworking yeswecancoap lwip
dbg.cpp@3:53f6e0430d8e, 2012-06-22 (annotated)
- Committer:
- donatien
- Date:
- Fri Jun 22 16:17:38 2012 +0000
- Revision:
- 3:53f6e0430d8e
- Parent:
- 1:bd1844de60f1
- Parent:
- 2:719e79a2291e
Merge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:3ac250c92185 | 1 | /* dbg.cpp */ |
donatien | 2:719e79a2291e | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 2:719e79a2291e | 3 | * |
donatien | 2:719e79a2291e | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 2:719e79a2291e | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 2:719e79a2291e | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 2:719e79a2291e | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 2:719e79a2291e | 8 | * furnished to do so, subject to the following conditions: |
donatien | 2:719e79a2291e | 9 | * |
donatien | 2:719e79a2291e | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 2:719e79a2291e | 11 | * substantial portions of the Software. |
donatien | 2:719e79a2291e | 12 | * |
donatien | 2:719e79a2291e | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 2:719e79a2291e | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 2:719e79a2291e | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 2:719e79a2291e | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 2:719e79a2291e | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 2:719e79a2291e | 18 | */ |
donatien | 0:3ac250c92185 | 19 | |
donatien | 0:3ac250c92185 | 20 | #include "dbg.h" |
donatien | 0:3ac250c92185 | 21 | |
donatien | 0:3ac250c92185 | 22 | #include "mbed.h" |
donatien | 0:3ac250c92185 | 23 | #include "rtos.h" |
donatien | 0:3ac250c92185 | 24 | |
donatien | 0:3ac250c92185 | 25 | #include <cstdio> |
donatien | 0:3ac250c92185 | 26 | #include <cstdarg> |
donatien | 0:3ac250c92185 | 27 | |
donatien | 0:3ac250c92185 | 28 | using namespace std; |
donatien | 0:3ac250c92185 | 29 | |
donatien | 0:3ac250c92185 | 30 | static Serial debug_pc(USBTX, USBRX); |
donatien | 0:3ac250c92185 | 31 | |
donatien | 0:3ac250c92185 | 32 | Mutex* debug_printf_mutex; //Singleton runtime initialisation to avoid static initialisation chaos problems |
donatien | 0:3ac250c92185 | 33 | |
donatien | 1:bd1844de60f1 | 34 | static char debug_newline[3]; |
donatien | 1:bd1844de60f1 | 35 | |
donatien | 0:3ac250c92185 | 36 | void debug_init() |
donatien | 0:3ac250c92185 | 37 | { |
donatien | 1:bd1844de60f1 | 38 | debug_set_newline("\n"); |
donatien | 0:3ac250c92185 | 39 | debug_pc.baud(115200); |
donatien | 0:3ac250c92185 | 40 | debug_printf_mutex = new Mutex(); |
donatien | 0:3ac250c92185 | 41 | printf("[START]\n"); |
donatien | 0:3ac250c92185 | 42 | fflush(stdout); |
donatien | 0:3ac250c92185 | 43 | } |
donatien | 0:3ac250c92185 | 44 | |
donatien | 1:bd1844de60f1 | 45 | void debug_set_newline(const char* newline) |
donatien | 1:bd1844de60f1 | 46 | { |
donatien | 1:bd1844de60f1 | 47 | strncpy( debug_newline, newline, 2 ); |
donatien | 1:bd1844de60f1 | 48 | debug_newline[2] = '\0'; |
donatien | 1:bd1844de60f1 | 49 | } |
donatien | 1:bd1844de60f1 | 50 | |
donatien | 0:3ac250c92185 | 51 | void debug(int level, const char* module, int line, const char* fmt, ...) |
donatien | 0:3ac250c92185 | 52 | { |
donatien | 0:3ac250c92185 | 53 | osStatus ret = debug_printf_mutex->lock(); |
donatien | 0:3ac250c92185 | 54 | if(ret != osOK) |
donatien | 0:3ac250c92185 | 55 | { |
donatien | 0:3ac250c92185 | 56 | /* |
donatien | 0:3ac250c92185 | 57 | printf("[FATAL] DBG Mutex failed!! (ret code = %02x - Mutex is @ %p)\n", ret, &debug_printf_mutex); |
donatien | 0:3ac250c92185 | 58 | error(""); |
donatien | 0:3ac250c92185 | 59 | while(1); //FATAL |
donatien | 0:3ac250c92185 | 60 | */ |
donatien | 0:3ac250c92185 | 61 | } |
donatien | 0:3ac250c92185 | 62 | switch(level) |
donatien | 0:3ac250c92185 | 63 | { |
donatien | 0:3ac250c92185 | 64 | default: |
donatien | 0:3ac250c92185 | 65 | case 1: |
donatien | 0:3ac250c92185 | 66 | printf("[ERROR]"); |
donatien | 0:3ac250c92185 | 67 | break; |
donatien | 0:3ac250c92185 | 68 | case 2: |
donatien | 0:3ac250c92185 | 69 | printf("[WARN]"); |
donatien | 0:3ac250c92185 | 70 | break; |
donatien | 0:3ac250c92185 | 71 | case 3: |
donatien | 0:3ac250c92185 | 72 | printf("[INFO]"); |
donatien | 0:3ac250c92185 | 73 | break; |
donatien | 0:3ac250c92185 | 74 | case 4: |
donatien | 0:3ac250c92185 | 75 | printf("[DBG]"); |
donatien | 0:3ac250c92185 | 76 | break; |
donatien | 0:3ac250c92185 | 77 | } |
donatien | 0:3ac250c92185 | 78 | |
donatien | 0:3ac250c92185 | 79 | printf(" Module %s - Line %d: ", module, line); |
donatien | 0:3ac250c92185 | 80 | |
donatien | 0:3ac250c92185 | 81 | va_list argp; |
donatien | 0:3ac250c92185 | 82 | |
donatien | 0:3ac250c92185 | 83 | va_start(argp, fmt); |
donatien | 0:3ac250c92185 | 84 | vprintf(fmt, argp); |
donatien | 0:3ac250c92185 | 85 | va_end(argp); |
donatien | 0:3ac250c92185 | 86 | |
donatien | 1:bd1844de60f1 | 87 | printf(debug_newline); |
donatien | 0:3ac250c92185 | 88 | |
donatien | 0:3ac250c92185 | 89 | fflush(stdout); |
donatien | 0:3ac250c92185 | 90 | |
donatien | 0:3ac250c92185 | 91 | if(ret == osOK) |
donatien | 0:3ac250c92185 | 92 | { |
donatien | 0:3ac250c92185 | 93 | debug_printf_mutex->unlock(); |
donatien | 0:3ac250c92185 | 94 | } |
donatien | 0:3ac250c92185 | 95 | |
donatien | 0:3ac250c92185 | 96 | } |
donatien | 0:3ac250c92185 | 97 | |
donatien | 0:3ac250c92185 | 98 | void debug_error(const char* module, int line, int ret) |
donatien | 0:3ac250c92185 | 99 | { |
donatien | 0:3ac250c92185 | 100 | debug_printf_mutex->lock(); |
donatien | 0:3ac250c92185 | 101 | printf("[RC] Module %s - Line %d : Error %d\n", module, line, ret); |
donatien | 0:3ac250c92185 | 102 | fflush(stdout); |
donatien | 0:3ac250c92185 | 103 | debug_printf_mutex->unlock(); |
donatien | 0:3ac250c92185 | 104 | } |
donatien | 0:3ac250c92185 | 105 | |
donatien | 1:bd1844de60f1 | 106 | void debug_exact(const char* fmt, ...) |
donatien | 1:bd1844de60f1 | 107 | { |
donatien | 1:bd1844de60f1 | 108 | osStatus ret = debug_printf_mutex->lock(); |
donatien | 1:bd1844de60f1 | 109 | if(ret != osOK) |
donatien | 1:bd1844de60f1 | 110 | { |
donatien | 1:bd1844de60f1 | 111 | /* |
donatien | 1:bd1844de60f1 | 112 | printf("[FATAL] DBG Mutex failed!! (ret code = %02x - Mutex is @ %p)\n", ret, &debug_printf_mutex); |
donatien | 1:bd1844de60f1 | 113 | error(""); |
donatien | 1:bd1844de60f1 | 114 | while(1); //FATAL |
donatien | 1:bd1844de60f1 | 115 | */ |
donatien | 1:bd1844de60f1 | 116 | } |
donatien | 1:bd1844de60f1 | 117 | |
donatien | 1:bd1844de60f1 | 118 | va_list argp; |
donatien | 0:3ac250c92185 | 119 | |
donatien | 1:bd1844de60f1 | 120 | va_start(argp, fmt); |
donatien | 1:bd1844de60f1 | 121 | vprintf(fmt, argp); |
donatien | 1:bd1844de60f1 | 122 | va_end(argp); |
donatien | 1:bd1844de60f1 | 123 | |
donatien | 1:bd1844de60f1 | 124 | if(ret == osOK) |
donatien | 1:bd1844de60f1 | 125 | { |
donatien | 1:bd1844de60f1 | 126 | debug_printf_mutex->unlock(); |
donatien | 1:bd1844de60f1 | 127 | } |
donatien | 1:bd1844de60f1 | 128 | } |