-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserver-log.c
290 lines (226 loc) · 7.3 KB
/
server-log.c
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
290
//
// Logging functions for LPrint, a Label Printer Application
//
// Note: Log format strings currently only support %d and %s!
//
// Copyright © 2019-2020 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Include necessary headers...
//
#include "lprint.h"
#include <stdarg.h>
#include <syslog.h>
//
// Local functions...
//
static void write_log(lprint_system_t *system, lprint_loglevel_t level, const char *message, va_list ap);
//
// Local globals...
//
static const int syslevels[] = // Mapping of log levels to syslog
{
LOG_DEBUG | LOG_PID | LOG_LPR,
LOG_INFO | LOG_PID | LOG_LPR,
LOG_WARNING | LOG_PID | LOG_LPR,
LOG_ERR | LOG_PID | LOG_LPR,
LOG_CRIT | LOG_PID | LOG_LPR
};
//
// 'lprintLog()' - Log a message for the system.
//
void
lprintLog(lprint_system_t *system, // I - System
lprint_loglevel_t level, // I - Log level
const char *message, // I - Printf-style message string
...) // I - Additional arguments as needed
{
va_list ap; // Pointer to arguments
if (level < system->loglevel)
return;
va_start(ap, message);
if (system->logfd >= 0)
write_log(system, level, message, ap);
else
vsyslog(syslevels[level], message, ap);
va_end(ap);
}
//
// 'lprintLogAttributes()' - Log attributes for a client connection.
//
void
lprintLogAttributes(
lprint_client_t *client, // I - Client
const char *title, // I - Title for attributes
ipp_t *ipp, // I - IPP message
int is_response) // I - 1 if a response, 0 if a request
{
if (client->system->loglevel > LPRINT_LOGLEVEL_DEBUG)
return;
}
//
// 'lprintLogClient()' - Log a message for a client.
//
void
lprintLogClient(
lprint_client_t *client, // I - Client
lprint_loglevel_t level, // I - Log level
const char *message, // I - Printf-style message string
...) // I - Additional arguments as needed
{
char cmessage[1024]; // Message with client prefix
va_list ap; // Pointer to arguments
if (level < client->system->loglevel)
return;
snprintf(cmessage, sizeof(cmessage), "[Client %d] %s", client->number, message);
va_start(ap, message);
if (client->system->logfd >= 0)
write_log(client->system, level, cmessage, ap);
else
vsyslog(syslevels[level], cmessage, ap);
va_end(ap);
}
//
// 'lprintLogJob()' - Log a message for a job.
//
void
lprintLogJob(
lprint_job_t *job, // I - Job
lprint_loglevel_t level, // I - Log level
const char *message, // I - Printf-style message string
...) // I - Additional arguments as needed
{
char jmessage[1024]; // Message with job prefix
va_list ap; // Pointer to arguments
if (level < job->system->loglevel)
return;
snprintf(jmessage, sizeof(jmessage), "[Job %d] %s", job->id, message);
va_start(ap, message);
if (job->system->logfd >= 0)
write_log(job->system, level, jmessage, ap);
else
vsyslog(syslevels[level], jmessage, ap);
va_end(ap);
}
//
// 'lprintLogPrinter()' - Log a message for a printer.
//
void
lprintLogPrinter(
lprint_printer_t *printer, // I - Printer
lprint_loglevel_t level, // I - Log level
const char *message, // I - Printf-style message string
...) // I - Additional arguments as needed
{
char pmessage[1024]; // Message with printer prefix
va_list ap; // Pointer to arguments
if (level < printer->system->loglevel)
return;
snprintf(pmessage, sizeof(pmessage), "[Printer %s] %s", printer->printer_name, message);
va_start(ap, message);
if (printer->system->logfd >= 0)
write_log(printer->system, level, pmessage, ap);
else
vsyslog(syslevels[level], pmessage, ap);
va_end(ap);
}
//
// 'write_log()' - Write a line to the log file...
//
static void
write_log(lprint_system_t *system, // I - System
lprint_loglevel_t level, // I - Log level
const char *message, // I - Printf-style message string
va_list ap) // I - Pointer to additional arguments
{
char buffer[2048], // Output buffer
*bufptr, // Pointer into buffer
*bufend; // Pointer to end of buffer
time_t curtime; // Current time
struct tm curdate; // Current date
static const char *prefix = "DIWEF"; // Message prefix
const char *sval; // String value
// Each log line starts with a standard prefix of log level and date/time...
time(&curtime);
gmtime_r(&curtime, &curdate);
snprintf(buffer, sizeof(buffer), "%c [%04d-%02d-%02dT%02d:%02d:%02dZ] ", prefix[level], curdate.tm_year + 1900, curdate.tm_mon + 1, curdate.tm_mday, curdate.tm_hour, curdate.tm_min, curdate.tm_sec);
bufptr = buffer + 25; // Skip level/date/time
bufend = buffer + sizeof(buffer) - 1; // Leave room for newline on end
// Then format the message line using a subset of printf format sequences...
while (*message && bufptr < bufend)
{
if (*message == '%')
{
message ++;
switch (*message)
{
case 'd' : // Log an integer
snprintf(bufptr, bufptr - bufend + 1, "%d", va_arg(ap, int));
bufptr += strlen(bufptr);
break;
case 'p' : // Log a pointer
snprintf(bufptr, bufptr - bufend + 1, "%p", va_arg(ap, void *));
bufptr += strlen(bufptr);
break;
case 's' : // Log a string
if ((sval = va_arg(ap, char *)) == NULL)
sval = "(null)";
while (*sval && bufptr < bufend)
{
int val = (*sval++) & 255;
if (val < ' ' || val == 0x7f || val == '\\' || val == '\'' || val == '\"')
{
// Escape control and special characters in the string...
if (bufptr > (bufend - 4))
break;
*bufptr++ = '\\';
if (val == '\\')
*bufptr++ = '\\';
else if (val == '\'')
*bufptr++ = '\'';
else if (val == '\"')
*bufptr++ = '\"';
else if (val == '\n')
*bufptr++ = 'n';
else if (val == '\r')
*bufptr++ = 'r';
else if (val == '\t')
*bufptr++ = 't';
else
{
// Use octal escape for other control characters...
*bufptr++ = '0' + (val / 64);
*bufptr++ = '0' + ((val / 8) & 7);
*bufptr++ = '0' + (val & 7);
}
}
else
*bufptr++ = val;
}
break;
case 'u' : // Log an unsigned integer
snprintf(bufptr, bufptr - bufend + 1, "%u", va_arg(ap, unsigned));
bufptr += strlen(bufptr);
break;
case 'x' : // Log an unsigned integer as hex
snprintf(bufptr, bufptr - bufend + 1, "%x", va_arg(ap, unsigned));
bufptr += strlen(bufptr);
break;
default : // Something else we don't support
*bufptr++ = '%';
if (bufptr < bufend)
*bufptr++ = *message;
break;
}
}
else
*bufptr++ = *message;
message ++;
}
// Add a newline and write it out...
*bufptr++ = '\n';
write(system->logfd, buffer, bufptr - buffer);
}