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