-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cpp
120 lines (97 loc) · 3.87 KB
/
Log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "PirateDotty.h"
#include "Debug.h"
#include "Log.h"
#include <stdarg.h>
// --------------------------------------------------------------------
// GLOBAL VAR
#define KPRINTF_BUF_SIZE 256
// --------------------------------------------------------------------
#if defined(DEBUG) || defined(DEBUG_BT)
bool debug_on = true;
#else
bool debug_on = false;
#endif
int log_level = 3;
// --------------------------------------------------------------------
// LOCAL VAR
// --------------------------------------------------------------------
Stream* serialLine;
// --------------------------------------------------------------------
// FUNCTIONS
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// create a output function
// This works because Serial.write, although of
// type virtual, already exists.
//int uart_write (char c, FILE *stream)
//{
// if (serialLine != NULL) {
// serialLine->write(c) ;
// return 0;
// }
//}
void initLogging(Stream *stream) {
}
char buf[KPRINTF_BUF_SIZE];
/*
* like printf() but output goes to "Serial.print".
* If FREE_RTOS is defined, a mutex is used to protect from reentrancy.
* TODO: Success/fail checks for xSemaphoreCreateMutex() and xSemaphoreTake() timeout.
* Returns number of chars that did not fit into I/O port device buffer or 0.
* Note: Serial.print on the USB returns 0 in error, causing a flawed return value here.
*/
int kprintf(const char *format, ...) {
int n;
#ifdef _FREE_RTOS_
static xSemaphoreHandle kprintfLock = 0; // mutual exclusion for non-reentrant va_start
if (kprintfLock == 0) {
// first time only
kprintfLock = xSemaphoreCreateMutex();
xSemaphoreGive(kprintfLock);
}
if ((xSemaphoreTake(kprintfLock, configTICK_RATE_HZ / 4)) == pdPASS) // TODO if pdFAIL, do more than just discard the print data.
#endif
{
va_list args;
va_start (args, format);
vsnprintf(buf, sizeof(buf), format, args); // does not overrun sizeof(buf) including null terminator
va_end (args);
// the below assumes that the new data will fit into the I/O buffer. If not, Serial may drop it.
// if Serial had a get free buffer count, we could delay and retry. Such does exist at the device class level, but not at this level.
n = strlen(buf) - Serial.print(buf); // move chars to I/O buffer, freeing up local buf
#ifdef _FREE_RTOS_
xSemaphoreGive(kprintfLock);
#endif
}
return n; // number of chars unable to fit in device I/O buffer (see bug notice above)
}
void LOGi(const int loglevel, const char* fmt, ... )
{
// if (serialLine == NULL) return;
if (loglevel <= log_level) {
va_list argptr;
va_start(argptr, fmt);
// kprintf(fmt, ...);
vsnprintf(buf, sizeof(buf), fmt, argptr); // does not overrun sizeof(buf) including null terminator
va_end(argptr);
// the below assumes that the new data will fit into the I/O buffer. If not, Serial may drop it.
// if Serial had a get free buffer count, we could delay and retry. Such does exist at the device class level, but not at this level.
int n = strlen(buf) - Serial.print(buf); // move chars to I/O buffer, freeing up local buf
Serial.println(" ");
}
}
void LOGd(const int loglevel, const char* fmt, ... )
{
// if (serialLine == NULL) return;
if (debug_on && loglevel <= log_level) {
va_list argptr;
va_start(argptr, fmt);
// kprintf(fmt, argptr);
vsnprintf(buf, sizeof(buf), fmt, argptr); // does not overrun sizeof(buf) including null terminator
va_end(argptr);
// the below assumes that the new data will fit into the I/O buffer. If not, Serial may drop it.
// if Serial had a get free buffer count, we could delay and retry. Such does exist at the device class level, but not at this level.
int n = strlen(buf) - Serial.print(buf); // move chars to I/O buffer, freeing up local buf
Serial.println(" ");
}
}