-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathlog_test.c
445 lines (365 loc) · 9.14 KB
/
log_test.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#define SC_LOG_PRINT_FILE_NAME
#include "sc_log.h"
#include <assert.h>
#include <stdarg.h>
#include <time.h>
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
int callback(void *arg, enum sc_log_level level, const char *fmt, va_list va)
{
(void) level;
(void) fmt;
(void) va;
*(int *) arg = *(int *) arg + 1;
return 0;
}
void test1(void)
{
int count = 0;
sc_log_term();
sc_log_init();
sc_log_set_callback(&count, callback);
assert(sc_log_set_level("errrorr") == -1);
assert(sc_log_set_level("errox") == -1);
assert(sc_log_set_level("err") == -1);
sc_log_debug("test \n");
assert(count == 0);
assert(sc_log_set_level("debuG") == 0);
sc_log_set_level("DEBUG");
sc_log_debug("test \n");
assert(count == 1);
sc_log_info("test \n");
assert(count == 2);
sc_log_warn("test \n");
assert(count == 3);
sc_log_error("test \n");
assert(count == 4);
count = 0;
assert(sc_log_set_level("iNfO") == 0);
sc_log_set_level("INFO");
sc_log_debug("test \n");
assert(count == 0);
sc_log_info("test \n");
assert(count == 1);
sc_log_warn("test \n");
assert(count == 2);
sc_log_error("test \n");
assert(count == 3);
count = 0;
sc_log_set_level("WARN");
sc_log_debug("test \n");
assert(count == 0);
sc_log_info("test \n");
assert(count == 0);
sc_log_warn("test \n");
assert(count == 1);
sc_log_error("test \n");
assert(count == 2);
count = 0;
sc_log_set_level("OFF");
sc_log_debug("test \n");
assert(count == 0);
sc_log_info("test \n");
assert(count == 0);
sc_log_warn("test \n");
assert(count == 0);
sc_log_error("test \n");
assert(count == 0);
sc_log_set_level("INFO");
sc_log_set_stdout(false);
assert(sc_log_set_file("prev.txt", "current.txt") == 0);
for (int i = 0; i < 100000; i++) {
sc_log_error("testtesttesttesttesttesttesttesttesttesttesttes"
"t");
}
FILE *fp = fopen("prev.txt", "rb");
assert(fp != NULL);
fseek(fp, 0, SEEK_END);
assert(ftell(fp) >= SC_LOG_FILE_SIZE);
fclose(fp);
sc_log_term();
sc_log_init();
sc_log_term();
}
void verify_size(const char* filename, size_t expected)
{
FILE *fp = fopen(filename, "rb");
assert(fp != NULL);
fseek(fp, 0, SEEK_END);
assert((size_t) ftell(fp) == expected);
fclose(fp);
}
void test2(void)
{
int rc;
char buf1[256] = {0};
char buf2[257] = {0};
char buf3[200] = {0};
memset(buf1, 'a', sizeof(buf1) - 1);
memset(buf2, 'b', sizeof(buf2) - 1);
memset(buf3, 'c', sizeof(buf3) - 1);
sc_log_init();
sc_log_set_file("prev1.txt", "current1.txt");
sc_log_info("h1+");
rc = sc_log_set_file(buf1, buf3);
assert(rc != 0);
sc_log_info("nolog1");
rc = sc_log_set_file("prev1.txt", "current1.txt");
assert(rc == 0);
sc_log_info("h2+");
rc = sc_log_set_file(buf3, buf1);
assert(rc != 0);
sc_log_info("nolog2");
rc = sc_log_set_file("prev1.txt", "current1.txt");
assert(rc == 0);
sc_log_info("h3+");
rc = sc_log_term();
assert(rc == 0);
char tmp[1024] = {0};
FILE *fp = fopen("current1.txt", "rb");
fread(tmp, sizeof(tmp), 1, fp);
fclose(fp);
assert(strstr(tmp, "h1+") != NULL);
assert(strstr(tmp, "h2+") != NULL);
assert(strstr(tmp, "h3+") != NULL);
assert(strstr(tmp, "nolog1") == NULL);
assert(strstr(tmp, "nolog2") == NULL);
sc_log_init();
rc = sc_log_set_file(buf2, buf3);
assert(rc != 0);
rc = sc_log_set_file(buf3, buf2);
assert(rc != 0);
rc = sc_log_term();
assert(rc == 0);
rc = sc_log_term();
assert(rc != 0);
}
#ifdef SC_HAVE_WRAP
#include <errno.h>
#include <pthread.h>
int callback2(void *arg, enum sc_log_level level, const char *fmt, va_list va)
{
(void) arg;
(void) level;
vfprintf(stdout, fmt, va);
vfprintf(stdout, fmt, va);
return 0;
}
bool mock_fprintf = false;
FILE *fprintf_file;
int fprintf_count = 0;
int fprintf_ret = 0;
extern int __real_fprintf(FILE *stream, const char *format, ...);
int __wrap_fprintf(FILE *stream, const char *format, ...)
{
int rc;
va_list va;
if (!mock_fprintf) {
va_start(va, format);
rc = vfprintf(stream, format, va);
va_end(va);
return rc;
}
fprintf_file = stream;
fprintf_count++;
return fprintf_ret;
}
bool mock_vfprintf = false;
FILE *vprintf_file;
int vfprintf_count = 0;
int vfprintf_ret = 0;
extern int __real_vfprintf(FILE *stream, const char *format, va_list arg);
int __wrap_vfprintf(FILE *stream, const char *format, va_list arg)
{
if (!mock_vfprintf) {
return __real_vfprintf(stream, format, arg);
}
vprintf_file = stream;
vfprintf_count++;
return vfprintf_ret;
}
bool mock_fopen = false;
extern FILE *__real_fopen(const char *filename, const char *format);
FILE *__wrap_fopen(const char *filename, const char *mode)
{
if (!mock_fopen) {
return __real_fopen(filename, mode);
}
return NULL;
}
bool mock_ftell = false;
extern long int __real_ftell(FILE *stream);
extern long int __wrap_ftell(FILE *stream)
{
if (mock_ftell) {
return -1;
}
return __real_ftell(stream);
}
bool mock_fclose = false;
extern int __real_fclose(FILE *__stream);
int __wrap_fclose(FILE *__stream)
{
if (!mock_fclose) {
return __real_fclose(__stream);
}
__real_fclose(__stream);
return -1;
}
bool mock_localtime_r = false;
extern struct tm *__real_localtime_r(const time_t *timer, struct tm *res);
struct tm *__wrap_localtime_r(const time_t *timer, struct tm *res)
{
if (!mock_localtime_r) {
return __real_localtime_r(timer, res);
}
return NULL;
}
bool mock_attrinit = false;
extern int __real_pthread_mutexattr_init(pthread_mutexattr_t *attr);
int __wrap_pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
if (!mock_attrinit) {
return __real_pthread_mutexattr_init(attr);
}
return -1;
}
bool mock_mutexinit = false;
extern int __real_pthread_mutex_init(pthread_mutex_t *__mutex,
const pthread_mutexattr_t *__mutexattr);
int __wrap_pthread_mutex_init(pthread_mutex_t *__mutex,
const pthread_mutexattr_t *__mutexattr)
{
if (!mock_mutexinit) {
return __real_pthread_mutex_init(__mutex, __mutexattr);
}
return -1;
}
void fail_test(void)
{
mock_attrinit = true;
assert(sc_log_init() < 0);
mock_attrinit = false;
mock_mutexinit = true;
assert(sc_log_init() < 0);
mock_mutexinit = false;
assert(sc_log_init() == 0);
mock_fprintf = true;
mock_vfprintf = true;
vfprintf_count = 0;
sc_log_info("loggg");
assert(vfprintf_count > 0);
assert(vprintf_file == stdout);
vfprintf_ret = -1;
assert(sc_log_info("log") != 0);
vfprintf_ret = 0;
vfprintf_count = 0;
sc_log_set_stdout(false);
sc_log_info("loggg");
assert(vfprintf_count == 0);
sc_log_set_stdout(true);
sc_log_set_file("tmp.txt", "tmp2.txt");
sc_log_set_callback(NULL, callback2);
fprintf_count = 0;
vfprintf_count = 0;
sc_log_info("loggg");
assert(vfprintf_count + fprintf_count == 6);
sc_log_set_callback(NULL, NULL);
fprintf_count = 0;
vfprintf_count = 0;
sc_log_set_stdout(false);
sc_log_set_file(NULL, NULL);
sc_log_info("loggg");
assert(vfprintf_count + fprintf_count == 0);
sc_log_set_stdout(true);
fprintf_ret = -1;
assert(sc_log_info("test") == -1);
fprintf_ret = 0;
assert(sc_log_info("test") == 0);
sc_log_set_stdout(false);
sc_log_set_file("tmp.txt", "tmp2.txt");
vfprintf_ret = -1;
assert(sc_log_info("test") == -1);
vfprintf_ret = 0;
assert(sc_log_info("test") == 0);
fprintf_ret = -1;
assert(sc_log_info("test") == -1);
fprintf_ret = 0;
assert(sc_log_info("test") == 0);
fprintf_ret = -1;
assert(sc_log_set_file("tmp.txt", "tmp2.txt") == -1);
fprintf_ret = 0;
assert(sc_log_set_file(NULL, "test.txt") == 0);
assert(sc_log_set_file("test.txt", NULL) == 0);
mock_fopen = true;
assert(sc_log_set_file("prev.txt", "current.txt") == -1);
mock_fopen = false;
mock_ftell = true;
assert(sc_log_set_file("prev.txt", "current.txt") == -1);
mock_ftell = false;
assert(sc_log_set_file("prev.txt", "current.txt") == 0);
mock_localtime_r = true;
assert(sc_log_error("test") == -1);
mock_localtime_r = false;
mock_vfprintf = false;
mock_fprintf = false;
mock_fopen = true;
int failed = 0;
for (int i = 0; i < 40000; i++) {
failed = sc_log_error("testtesttesttesttesttesttesttesttesttest"
"est");
if (failed < 0) {
break;
}
}
assert(failed == -1);
mock_fopen = false;
assert(sc_log_set_file("prev.txt", "current.txt") == 0);
mock_fclose = true;
assert(sc_log_term() != 0);
mock_fclose = false;
mock_fprintf = false;
mock_vfprintf = false;
mock_localtime_r = false;
mock_fopen = false;
}
#else
void fail_test(void)
{
}
#endif
int log_callback(void *arg, enum sc_log_level level, const char *fmt,
va_list va)
{
const char *my_app = arg;
const char *level_str = sc_log_levels[level].str;
fprintf(stdout, " %s received log : level = [%s] ", my_app, level_str);
vfprintf(stdout, fmt, va);
return 0;
}
void example(void)
{
const char *my_app_name = "my app";
sc_log_init(); // Call once when your app starts.
// Default log-level is 'info' and default destination is 'stdout'
sc_log_info("Hello world!\n");
// Enable logging to file.
sc_log_set_file("log.0.txt", "log-latest.txt");
// stdout and file will get the log line
sc_log_info("to stdout and file!\n");
// Enable callback
sc_log_set_callback((void *) my_app_name, log_callback);
// stdout, file and callback will get the log line
sc_log_info("to all!\n");
sc_log_term(); // Call once on shutdown.
}
int main(void)
{
sc_log_set_thread_name("My thread");
fail_test();
example();
test1();
test2();
return 0;
}