forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan-log.c
278 lines (239 loc) · 5.95 KB
/
airscan-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
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Logging
*/
#include "airscan.h"
#include <ctype.h>
#include <time.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
/* Static variables */
static char *log_buffer;
static bool log_configured;
static uint64_t log_start_time;
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Get time for logging purposes
*/
static uint64_t
log_get_time (void)
{
struct timespec tms;
clock_gettime(CLOCK_MONOTONIC, &tms);
return ((uint64_t) tms.tv_nsec) + 1000000000 * (uint64_t) tms.tv_sec;
}
/* Initialize logging
*
* No log messages should be generated before this call
*/
void
log_init (void)
{
log_buffer = str_new();
log_configured = false;
log_start_time = log_get_time();
}
/* Cleanup logging
*
* No log messages should be generated after this call
*/
void
log_cleanup (void)
{
mem_free(log_buffer);
log_buffer = NULL;
}
/* Flush buffered log to file
*/
static void
log_flush (void)
{
int rc = write(2, log_buffer, mem_len(log_buffer));
(void) rc;
str_trunc(log_buffer);
}
/* Notify logger that configuration is loaded and
* logger can configure itself
*
* This is safe to generate log messages before log_configure()
* is called. These messages will be buffered, and after
* logger is configured, either written or abandoned, depending
* on configuration
*/
void
log_configure (void)
{
log_configured = true;
if (conf.dbg_enabled) {
log_flush();
} else {
str_trunc(log_buffer);
}
}
/* Format time elapsed since logging began
*/
static void
log_fmt_time (char *buf, size_t size)
{
uint64_t t = log_get_time() - log_start_time;
int hour, min, sec, msec;
sec = (int) (t / 1000000000);
msec = ((int) (t % 1000000000)) / 1000000;
hour = sec / 3600;
sec = sec % 3600;
min = sec / 60;
sec = sec % 60;
snprintf(buf, size, "%2.2d:%2.2d:%2.2d.%3.3d", hour, min, sec, msec);
}
/* log_ctx represents logging context
*/
struct log_ctx {
const char *name; /* Log name */
trace *trace; /* Associated trace */
};
/* log_ctx_new creates new logging context
* If parent != NULL, new logging context will have its own prefix,
* but trace file will be inherited from parent
*/
log_ctx*
log_ctx_new (const char *name, log_ctx *parent)
{
log_ctx *log = mem_new(log_ctx, 1);
log->name = str_trim(str_dup(name));
if (parent != NULL) {
log->trace = trace_ref(parent->trace);
} else {
log->trace = trace_open(name);
}
return log;
}
/* log_ctx_free destroys logging context
*/
void
log_ctx_free (log_ctx *log)
{
trace_unref(log->trace);
mem_free((char*) log->name);
mem_free(log);
}
/* Get protocol trace associated with logging context
*/
trace*
log_ctx_trace (log_ctx *log)
{
return log->trace;
}
/* Write a log message
*/
static void
log_message (log_ctx *log, bool trace_only, bool force,
const char *fmt, va_list ap)
{
trace *t = log ? log->trace : NULL;
char msg[4096];
int len = 0, namelen = 0, required_bytes = 0;
bool dont_log = trace_only ||
(log_configured && !conf.dbg_enabled && !force);
/* If logs suppressed and trace not in use, we have nothing
* to do */
if ((t == NULL) && dont_log) {
return;
}
/* Format a log message */
if (log != NULL) {
len += sprintf(msg, "%.64s: ", log->name);
namelen = len;
}
required_bytes = vsnprintf(msg + len, sizeof(msg) - len, fmt, ap);
/* vsnprintf returns the number of bytes required for the whole message,
* even if that exceeds the buffer size.
* If required_bytes exceeds space remaining in msg, we know msg is full.
* Otherwise, we can increment len by required_bytes.
*/
if (required_bytes >= (int) sizeof(msg) - len) {
len = sizeof(msg) - 1;
} else {
len += required_bytes;
}
while (len > 0 && isspace((unsigned char) msg[len-1])) {
len --;
}
msg[len] = '\0';
/* Write to log */
if (!dont_log) {
pthread_mutex_lock(&log_mutex);
log_buffer = str_append(log_buffer, msg);
log_buffer = str_append_c(log_buffer, '\n');
if ((log_configured && conf.dbg_enabled) || force) {
log_flush();
}
pthread_mutex_unlock(&log_mutex);
}
/* Write to trace */
if (t != NULL) {
if (len > namelen) {
char prefix[64];
log_fmt_time(prefix, sizeof(prefix));
trace_printf(t, "%s: %s", prefix, msg);
} else {
trace_printf(t, "");
}
}
}
/* Write a debug message.
*/
void
log_debug (log_ctx *log, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_message(log, false, false, fmt, ap);
va_end(ap);
}
/* Write a protocol trace message
*/
void
log_trace (log_ctx *log, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_message(log, true, false, fmt, ap);
va_end(ap);
}
/* Write a block of data into protocol trace
*/
void
log_trace_data (log_ctx *log, const char *content_type,
const void *bytes, size_t size)
{
http_data data = {
.content_type = content_type,
.bytes = bytes,
.size = size
};
trace_dump_body(log->trace, &data);
}
/* Write an error message and terminate a program.
*/
void
log_panic (log_ctx *log, const char *fmt, ...)
{
va_list ap;
/* Note, log_buffer is not empty only if logger is not
* configured yet, but there are pending debug messages.
* At this case we discard these messages, but panic
* message is written anyway
*/
pthread_mutex_lock(&log_mutex);
str_trunc(log_buffer);
pthread_mutex_unlock(&log_mutex);
va_start(ap, fmt);
log_message(log, false, true, fmt, ap);
va_end(ap);
abort();
}
/* vim:ts=8:sw=4:et
*/