Debug library

Dependents:   NetworkingCoreLib LwIPNetworking yeswecancoap lwip

Committer:
donatien
Date:
Thu May 24 15:33:14 2012 +0000
Revision:
0:3ac250c92185
Child:
1:bd1844de60f1
Child:
2:719e79a2291e
Initial Commit

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 0:3ac250c92185 38 void debug_init()
donatien 0:3ac250c92185 39 {
donatien 0:3ac250c92185 40 debug_pc.baud(115200);
donatien 0:3ac250c92185 41 debug_printf_mutex = new Mutex();
donatien 0:3ac250c92185 42 printf("[START]\n");
donatien 0:3ac250c92185 43 fflush(stdout);
donatien 0:3ac250c92185 44 }
donatien 0:3ac250c92185 45
donatien 0:3ac250c92185 46 void debug(int level, const char* module, int line, const char* fmt, ...)
donatien 0:3ac250c92185 47 {
donatien 0:3ac250c92185 48 osStatus ret = debug_printf_mutex->lock();
donatien 0:3ac250c92185 49 if(ret != osOK)
donatien 0:3ac250c92185 50 {
donatien 0:3ac250c92185 51 /*
donatien 0:3ac250c92185 52 printf("[FATAL] DBG Mutex failed!! (ret code = %02x - Mutex is @ %p)\n", ret, &debug_printf_mutex);
donatien 0:3ac250c92185 53 error("");
donatien 0:3ac250c92185 54 while(1); //FATAL
donatien 0:3ac250c92185 55 */
donatien 0:3ac250c92185 56 }
donatien 0:3ac250c92185 57 switch(level)
donatien 0:3ac250c92185 58 {
donatien 0:3ac250c92185 59 default:
donatien 0:3ac250c92185 60 case 1:
donatien 0:3ac250c92185 61 printf("[ERROR]");
donatien 0:3ac250c92185 62 break;
donatien 0:3ac250c92185 63 case 2:
donatien 0:3ac250c92185 64 printf("[WARN]");
donatien 0:3ac250c92185 65 break;
donatien 0:3ac250c92185 66 case 3:
donatien 0:3ac250c92185 67 printf("[INFO]");
donatien 0:3ac250c92185 68 break;
donatien 0:3ac250c92185 69 case 4:
donatien 0:3ac250c92185 70 printf("[DBG]");
donatien 0:3ac250c92185 71 break;
donatien 0:3ac250c92185 72 }
donatien 0:3ac250c92185 73
donatien 0:3ac250c92185 74 printf(" Module %s - Line %d: ", module, line);
donatien 0:3ac250c92185 75
donatien 0:3ac250c92185 76 va_list argp;
donatien 0:3ac250c92185 77
donatien 0:3ac250c92185 78 va_start(argp, fmt);
donatien 0:3ac250c92185 79 vprintf(fmt, argp);
donatien 0:3ac250c92185 80 va_end(argp);
donatien 0:3ac250c92185 81
donatien 0:3ac250c92185 82 printf("\n");
donatien 0:3ac250c92185 83
donatien 0:3ac250c92185 84 fflush(stdout);
donatien 0:3ac250c92185 85
donatien 0:3ac250c92185 86 if(ret == osOK)
donatien 0:3ac250c92185 87 {
donatien 0:3ac250c92185 88 debug_printf_mutex->unlock();
donatien 0:3ac250c92185 89 }
donatien 0:3ac250c92185 90
donatien 0:3ac250c92185 91 }
donatien 0:3ac250c92185 92
donatien 0:3ac250c92185 93 void debug_error(const char* module, int line, int ret)
donatien 0:3ac250c92185 94 {
donatien 0:3ac250c92185 95 debug_printf_mutex->lock();
donatien 0:3ac250c92185 96 printf("[RC] Module %s - Line %d : Error %d\n", module, line, ret);
donatien 0:3ac250c92185 97 fflush(stdout);
donatien 0:3ac250c92185 98 debug_printf_mutex->unlock();
donatien 0:3ac250c92185 99 }
donatien 0:3ac250c92185 100
donatien 0:3ac250c92185 101