Base library for cellular modem implementations

Dependencies:   Socket lwip-sys lwip

Dependents:   CellularUSBModem CellularUSBModem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dbg.cpp Source File

dbg.cpp

00001 /* dbg.cpp */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 #include "dbg.h"
00021 
00022 #include "mbed.h"
00023 #include "rtos.h"
00024 
00025 #include <cstdio>
00026 #include <cstdarg>
00027 
00028 using namespace std;
00029 
00030 static Serial debug_pc(USBTX, USBRX);
00031 
00032 static char debug_newline[3];
00033 
00034 static void debug_lock(bool set)
00035 {
00036   static Mutex* mtx = new Mutex(); //Singleton runtime initialisation to avoid static initialisation chaos problems
00037   static bool init = false;
00038   if(set)
00039   {
00040     mtx->lock();
00041     if(!init)
00042     {
00043       strncpy( debug_newline, "\n", 2 );
00044       printf("[START]\n");
00045       fflush(stdout);
00046       init = true;
00047     }
00048   }
00049   else
00050   {
00051     mtx->unlock();
00052   }
00053 }
00054 
00055 void debug_init()
00056 {
00057   debug_lock(true); //Force init
00058   debug_lock(false);
00059 }
00060 
00061 void debug_set_newline(const char* newline)
00062 {
00063   debug_lock(true);
00064   strncpy( debug_newline, newline, 2 );
00065   debug_newline[2] = '\0';
00066   debug_lock(false);
00067 }
00068 
00069 void debug_set_speed(int speed)
00070 {
00071   debug_pc.baud(speed);
00072 }
00073 
00074 void debug(int level, const char* module, int line, const char* fmt, ...)
00075 {
00076   debug_lock(true);
00077   switch(level)
00078   {
00079   default:
00080   case 1:
00081     printf("[ERROR]");
00082     break;
00083   case 2:
00084     printf("[WARN]");
00085     break;
00086   case 3:
00087     printf("[INFO]");
00088     break;
00089   case 4:
00090     printf("[DBG]");
00091     break;
00092   }
00093 
00094   printf(" Module %s - Line %d: ", module, line);
00095 
00096   va_list argp;
00097 
00098   va_start(argp, fmt);
00099   vprintf(fmt, argp);
00100   va_end(argp);
00101 
00102   printf(debug_newline);
00103 
00104   fflush(stdout);
00105 
00106   debug_lock(false);
00107 
00108 }
00109 
00110 void debug_error(const char* module, int line, int ret)
00111 {
00112   debug_lock(true);
00113   printf("[RC] Module %s - Line %d : Error %d\n", module, line, ret);
00114   fflush(stdout);
00115   debug_lock(false);
00116 }
00117 
00118 void debug_exact(const char* fmt, ...)
00119 {
00120   debug_lock(true);
00121   va_list argp;
00122 
00123   va_start(argp, fmt);
00124   vprintf(fmt, argp);
00125   va_end(argp);
00126   debug_lock(false);
00127 }