Debug library

Dependents:   NetworkingCoreLib LwIPNetworking yeswecancoap lwip

Committer:
donatien
Date:
Thu May 31 15:09:00 2012 +0000
Revision:
1:bd1844de60f1
Parent:
0:3ac250c92185
Child:
3:53f6e0430d8e
Newline characters can be setup

Who changed what in which revision?

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