-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleLog.cpp
169 lines (128 loc) · 3.24 KB
/
SimpleLog.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
/*
* based on ideas from
*
*/
#include "SimpleLog.h"
/* NEW:
* Log hierarchy: (not bit combinations any more)
*
*
*
* QUIET 0 -lowest
* ERROR 1
* WARNING 2
* INFO 3
* DEBUG 4 -highest
*
* lower level include always higher levels: e.g. DEBUG includes I, W, E
*
*/
const char * prefix [ LOGLEVEL_MAX ] = {"", "E:", "W:", "I:", "D:"};
int SimpleLog::begin(LOGLEVEL_t level, IPAddress syslogServer)
{
_syslogServer = syslogServer;
if ( _syslogServer )
// _udp.begin(2390); not necessary: cause not listening
_udp.beginPacket(syslogServer, 514);
begin(level);
return( SIMPLE_LOG_SUCCESS);
}
int SimpleLog::begin(LOGLEVEL_t level)
{
setLevel( level ); // errors must always be logged
return( SIMPLE_LOG_SUCCESS);
}
int SimpleLog::begin()
{
setLevel( LOGLEVEL_INFO); // default
return( SIMPLE_LOG_SUCCESS);
}
int SimpleLog::startSyslog (IPAddress syslogServer)
{
_syslogServer = syslogServer;
if ( _syslogServer ) {
// _udp.begin(2390); not necessary: cause not listening
_state = _udp.beginPacket(syslogServer, 514);
}
return( _state);
}
void SimpleLog::setSysLogPrefix(const char *pf) {
strncpy( _syslogPrefix, pf, sizeof (_syslogPrefix) -2);
strcat( _syslogPrefix, ":");
}
int SimpleLog::startSyslog (const char * hostname)
{
_state = _udp.beginPacket(hostname, 514);
return( _state);
}
int SimpleLog::stopSyslog () {
_state = 0;
_udp.stop();
return( _state);
}
int SimpleLog::setLevel(LOGLEVEL_t newLevel)
{
int retVal = SIMPLE_LOG_SUCCESS;
if( newLevel >= LOGLEVEL_QUIET && newLevel < LOGLEVEL_MAX)
_level = newLevel;
else
retVal = SIMPLE_LOG_LOGLEVEL_INVAL;
return( retVal );
}
void SimpleLog::logIntern(LOGLEVEL_t logLevel, const char* msg, va_list args)
{
strcpy (buf, prefix [logLevel]);
vsnprintf_P (&buf[2], sizeof(buf)-2 , msg, args );
Serial.printf ( buf );
sendUdpSyslog ( buf );
}
void SimpleLog::logIntern(LOGLEVEL_t logLevel, String msg)
{
msg = String(prefix [logLevel]) + msg ;
Serial.println(msg);
sendUdpSyslog (msg.c_str());
}
// ------------------------
void SimpleLog::debug (const char * msg, ...) {
if( _level >= LOGLEVEL_DEBUG ) {
va_list args;
va_start(args, msg); // store all argumenet after 'msg'
logIntern ( LOGLEVEL_DEBUG, msg, args);
va_end(args );
}
}
void SimpleLog::debug (String msg){
logIntern( LOGLEVEL_DEBUG, msg);
}
// --------------------------------------------------------------
void SimpleLog::info (const char * msg, ...) {
if( _level >= LOGLEVEL_INFO) {
va_list args;
va_start(args, msg); // store all argument after 'msg'
logIntern ( LOGLEVEL_INFO, msg, args);
va_end(args );
}
}
void SimpleLog::info (String msg){
logIntern( LOGLEVEL_INFO, msg);
}
// --------------------------------------------------------------------------
void SimpleLog::error (String msg){
logIntern( LOGLEVEL_ERROR, msg);
}
// error always
void SimpleLog::error (const char * msg, ...) {
va_list args;
va_start(args, msg); // store all argument after 'msg'
logIntern ( LOGLEVEL_ERROR, msg, args);
va_end(args );
}
// --------------------------------------------------------------
void SimpleLog::sendUdpSyslog(const char * msgtosend)
{
if (_state) { // != 0
_udp.write(_syslogPrefix);
_udp.write(msgtosend);
_udp.endPacket();
}
}