/
syslog.cpp
220 lines (180 loc) · 4.69 KB
/
syslog.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* syslog.c
*
* Created on: Sep 14, 2011
* Author: trampas.stern
*
* Misfit Tech invests time and resources providing this open source code,
* please support Misfit Tech and open-source hardware by purchasing
* products from Misfit Tech, www.misifittech.net!
*
* Written by Trampas Stern for Misfit Tech.
* BSD license, check license.txt for more information
* All text above, must be included in any redistribution
*********************************************************************/
#include "syslog.h"
#include <Arduino.h>
#define ANSI_WHITE "\033[37m"
#define ANSI_NORMAL "\033[0m"
#define ANSI_BLINK "\033[5m"
#define ANSI_BLUE "\033[34m"
#define ANSI_MAGENTA "\033[35m"
#define ANSI_CYAN "\033[36m"
#define ANSI_WHITE "\033[37m"
#define ANSI_RED "\033[31m"
#define ANSI_GREEN "\033[32m"
#define ANSI_PINK "\033[35m\033[1m"
#define ANSI_BROWN "\033[33m"
#define ANSI_YELLOW "\033[33m\033[1m"
#define ANSI_BLACK "\033[30m"
#define ANSI_BELL_AND_RED "\a\033[31m"
#define NEW_LINE "\n\r"
Uart *ptrSerial=NULL;
eLogLevel SyslogLevelToWrite;
static char buffer[SYSLOG_BUFFER_SIZE];
static unsigned int BufIndex=0;
static int SysLog_Enabled=1;
int SysLogDisable(void)
{
SysLog_Enabled=0;
return 0;
}
int SysLogEnable(void)
{
SysLog_Enabled=1;
return 0;
}
int SysLogIsEnabled(void)
{
return SysLog_Enabled;
}
void SysLogPuts(const char *ptrStr)
{
if (!SysLog_Enabled)
return;
if (NULL == ptrSerial)
{
while(*ptrStr)
{
SYSLOG_PUTC(*ptrStr);
ptrStr++;
}
} else
{
ptrSerial->write(ptrStr);
}
}
int SysLogInitDone=0;
void SysLogInit(Uart *ptrSerialObj, eLogLevel LevelToWrite)
{
ptrSerial=ptrSerialObj;
SyslogLevelToWrite=LevelToWrite;
SysLogInitDone=1;
BufIndex=0;
memset(buffer,0,SYSLOG_BUFFER_SIZE);
}
int SysLogProcessing=0; // this is used such that syslog can be reentrent
int SysLogMissed=0;
void SysLog(eLogLevel priorty, const char *fmt, ...)
{
//UINT32 ret;
int previousState=SysLog_Enabled;
char vastr[MAX_SYSLOG_STRING]={0};
//char outstr[MAX_SYSLOG_STRING]={0};
va_list ap;
if (SysLogProcessing)
{
//we have a syslog from a syslog call thus return as not much we can do...
//memset(buffer,0,SYSLOG_BUFFER_SIZE);
va_start(ap,fmt);
vsnprintf(&buffer[BufIndex],SYSLOG_BUFFER_SIZE-BufIndex,(char *)fmt,ap);
BufIndex=strlen(buffer);
snprintf(&buffer[BufIndex],SYSLOG_BUFFER_SIZE-BufIndex,NEW_LINE);
BufIndex=strlen(buffer);
SysLogMissed++; //set flag that we missed a call
return;
}
SysLogProcessing=1;
//stop the watch dog will doing a SysLog print
Sys_WDogHoldOn();
if(!SysLogInitDone)
{
SysLogInit(NULL, LOG_WARNING); //not sure who is reseting serial port but before we print set it up
WARNING("You should init SysLog");
//SysLogInitDone=0;
}
//Send out a * that we missed a SysLog Message before this current message
if (SysLogMissed)
{
//SysLogPuts(ANSI_RED);
SysLogPuts("*** Reentrant Log call possible loss of message(s):");
SysLogPuts(NEW_LINE);
if (BufIndex>0)
{
SysLogPuts(buffer);
memset(buffer,0,SYSLOG_BUFFER_SIZE);
BufIndex=0;
}
//SysLogPuts(ANSI_RED);
SysLogPuts("***********");
SysLogPuts(NEW_LINE);
SysLogMissed=0;
}
memset(vastr,0,MAX_SYSLOG_STRING);
va_start(ap,fmt);
//#ifndef PGM_P
#if 1
vsnprintf(vastr,MAX_SYSLOG_STRING,(char *)fmt,ap);
#else
vsprintf_P(vastr,(const char *)fmt,ap);
#endif
//get time and store in datetimestr if desired
//sprintf(outstr, "[%s] %s\r\n", datetimestr, vastr);
if (priorty<=LOG_ERROR)
{
SysLog_Enabled=1;
SysLogPuts(ANSI_RED);
}else if (priorty==LOG_DEBUG)
{
SysLogPuts(ANSI_WHITE);
}else if (priorty==LOG_WARNING)
{
SysLogPuts(ANSI_BLUE);
}
#ifdef RTC_H_
#ifdef TIME_H_
{
struct tm tp;
RTC_Time_s tm;
time_t secs;
char datetimestr[MAX_SYSLOG_STRING]={0};
RTC_ReadTime(&tm);
secs=tm.seconds;
convertFlexNetTime((time_t *)&secs, &tp);
time_str(datetimestr,&tp);
SysLogPuts(datetimestr);
if (priorty<=SyslogLevelToWrite && SysLogWriteFunc!=NULL)
{
SysLogWriteFunc(datetimestr,strlen(datetimestr));
}
}
#endif
#endif
SysLogPuts(vastr);
//
// if (priorty<=SyslogLevelToWrite && SysLogWriteFunc!=NULL)
// {
// SysLogWriteFunc(vastr,strlen(vastr));
// SysLogWriteFunc(NEW_LINE,strlen(NEW_LINE));
// }
SysLogPuts(ANSI_NORMAL);
SysLogPuts(NEW_LINE);
if (priorty == LOG_EMERG) {
//you can reboot processor here
}
//start the watch dog where left off..
Sys_WDogHoldOff();
SysLogProcessing=0;
SysLog_Enabled=previousState;
return;
}