-
Notifications
You must be signed in to change notification settings - Fork 4
/
traceLogDemo.cc
289 lines (250 loc) · 9.85 KB
/
traceLogDemo.cc
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*******************************************************************************
*
* Copyright (c) 2009, Ron Iovine, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ron Iovine nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Ron Iovine ''AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL Ron Iovine BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************/
/*******************************************************************************
*
* This is an example demo program that shows how to use the TraceLog feature as
* a stand-alone module without any dynamic trace filtering via the TraceFilter
* and PshellServer modules
*
*******************************************************************************/
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <stdlib.h>
#include <PshellServer.h>
#include <TraceLog.h>
#define DUMP_BUFFER_SIZE 256
/*
* create some user defined levels and macros
*/
/* must start user defined levels after TL_MAX */
#define TL_USER_LEVEL1 TL_MAX+1
#define TL_USER_LEVEL2 TL_MAX+2
#define TL_USER_LEVEL3 TL_MAX+3
/* define the string based names of the trace levels */
#define TL_USER_LEVEL1_STRING "USER-LEVEL1"
#define TL_USER_LEVEL2_STRING "USER-LEVEL2"
#define TL_USER_LEVEL3_STRING "USER-LEVEL3"
/* define some program specific trace macros */
#define TRACE_USER_LEVEL1(format, args...) __TRACE(TL_USER_LEVEL1, TL_USER_LEVEL1_STRING, format, ## args)
#define TRACE_USER_LEVEL2(format, args...) __TRACE(TL_USER_LEVEL2, TL_USER_LEVEL2_STRING, format, ## args)
#define TRACE_USER_LEVEL3(format, args...) __TRACE(TL_USER_LEVEL3, TL_USER_LEVEL3_STRING, format, ## args)
/* a couple of functions to show function name filtering */
/******************************************************************************/
/******************************************************************************/
void foo(void)
{
TRACE_ENTER("message 1");
sleep(1);
TRACE_EXIT("message 2");
}
/******************************************************************************/
/******************************************************************************/
void bar(void)
{
TRACE_ENTER("message 1");
sleep(1);
TRACE_EXIT("message 2");
}
/******************************************************************************/
/******************************************************************************/
void sampleOutputFunction(const char *outputString_)
{
/*
* this sample log output function is registered with the TraceLog
* 'trace_registerOutputFunction' call to show a client supplied
* log function, the string passed in is a fully formatted log
* string, it is up to the registering application to decide
* what to do with that string, i.e. write to stdout, write to
* a custom logfile, write to syslog etc
*/
/* write to stdout */
printf("CUSTOM OUTPUT - %s", outputString_);
/* write to syslog */
syslog(LOG_INFO, "CUSTOM OUTPUT - %s", outputString_);
}
/******************************************************************************/
/******************************************************************************/
void sampleFormatFunction(const char *name_,
const char *level_,
const char *file_,
const char *function_,
int line_,
const char *timestamp_,
const char *userMessage_,
char *outputString_)
{
/*
* this sample format function is registered with the TraceLog
* 'trace_registerFormatFunction' call to show a client supplied
* formatting function, the formating must be done into the outputString_
* variable, the user can check to see if the various formatting items
* are enabled, or they can just use a fixed format
*/
// standard output format, see what items are enabled, each item is separated by a '|'
// custom format is: 2014-08-17 23:11:00.114192 level [name:file:line] user-message
// add any timestamp if enabled
if (trace_isTimestampEnabled())
{
sprintf(&outputString_[strlen(outputString_)], "%s ", timestamp_);
}
// add the level
sprintf(&outputString_[strlen(outputString_)], "%s ", level_);
if (trace_isLogNameEnabled() && trace_isLocationEnabled())
{
sprintf(&outputString_[strlen(outputString_)], "[%s:%s:%d] ", name_, file_, line_);
}
else if (trace_isLogNameEnabled())
{
sprintf(&outputString_[strlen(outputString_)], "[%s] ", name_);
}
else if (trace_isLocationEnabled())
{
sprintf(&outputString_[strlen(outputString_)], "[%s:%d] ", file_, line_);
}
// add the user message
sprintf(&outputString_[strlen(outputString_)], "%s\n", userMessage_);
}
/******************************************************************************/
/******************************************************************************/
void showUsage(void)
{
printf("\n");
printf("Usage: traceLogDemo <level> [custom]\n");
printf("\n");
printf(" where:\n");
printf(" <level> - The desired log level value, 0-%d\n", TL_USER_LEVEL3);
printf(" custom - Use a custom log format\n");
printf("\n");
exit(0);
}
/******************************************************************************/
/******************************************************************************/
int main (int argc, char *argv[])
{
#ifndef TRACE_LOG_DISABLED
char dumpBuffer[DUMP_BUFFER_SIZE];
#endif
unsigned logLevel = 0;
TraceOutputFunction outputFunction = NULL;
TraceFormatFunction formatFunction = NULL;
const char *timestampFormat = NULL;
/* validate our command line arguments and get desired log level */
if ((argc == 2) || (argc == 3))
{
if (strcmp(argv[1], "-h") == 0)
{
showUsage();
}
if (argc == 3)
{
if (strcmp(argv[2], "custom") == 0)
{
/*
* register a custom client provided log function, this function will
* take a fully formatted log message string, it is up to the
* registering application to decide what to do with that string,
* i.e. write to stdout, write to custom logfile, write to syslog
* etc, this is optional, if no log function is registered, the
* trace logging service will just use 'printf' to output the
* log message
*/
/* open syslog with our program name */
openlog(argv[0], (LOG_CONS | LOG_PID | LOG_NDELAY), LOG_USER);
/* register our log output function */
outputFunction = sampleOutputFunction;
/* register our custom format function */
formatFunction = sampleFormatFunction;
/* set a custom timestamp format, add the date, the time must be last because the usec portion is appended to the time */
timestampFormat = "%Y-%m-%d %T";
}
else
{
showUsage();
}
}
logLevel = atoi(argv[1]);
}
else
{
showUsage();
}
#ifndef TRACE_LOG_DISABLED
/* initialize a sample memory hex dump buffer */
for (unsigned i = 0; i < DUMP_BUFFER_SIZE; i++)
{
dumpBuffer[i] = i;
}
#endif
/*
* register our program specific trace log levels with the trace log system
* this must be done before the trace_init(0 call so we can keep track of our
* max level name string length so our trace display can be formatted and aligned
* correctly
*
* format of call is "name", level
*/
trace_addUserLevel(TL_USER_LEVEL1_STRING, TL_USER_LEVEL1);
trace_addUserLevel(TL_USER_LEVEL2_STRING, TL_USER_LEVEL2);
trace_addUserLevel(TL_USER_LEVEL3_STRING, TL_USER_LEVEL3);
/*
* register our standard trace levels with the trace log system
* so our trace display can be formatted and aligned correctly
*/
trace_init("DEMO", NULL, logLevel, outputFunction, formatFunction, timestampFormat);
/* start our pshell server in NON_BLOCKING mode since this process has it's own control loop */
pshell_startServer("traceLogDemo", PSHELL_UDP_SERVER, PSHELL_NON_BLOCKING, PSHELL_LOCALHOST, 9191);
/* go into an infinite loop issuing some traces so we can demonstrate dynamic trace filtering */
for (;;)
{
TRACE_WARNING("message 1");
sleep(1);
TRACE_INFO("message 2");
sleep(1);
foo();
sleep(1);
bar();
sleep(1);
TRACE_DEBUG("message 3");
sleep(1);
TRACE_DUMP(dumpBuffer, sizeof(dumpBuffer), "dumping buffer: dumpBuffer");
sleep(1);
TRACE_ERROR("message 4");
sleep(1);
TRACE_FAILURE("message 5");
sleep(1);
TRACE_USER_LEVEL1("message 6");
sleep(1);
TRACE_USER_LEVEL2("message 7");
sleep(1);
TRACE_USER_LEVEL3("message 8");
sleep(1);
}
return (0);
}