Line data Source code
1 : /* This file is part of the cmail project (http://cmail.rocks/)
2 : * (c) 2015 Fabian "cbdev" Stumpf
3 : * License: Simplified BSD (2-Clause)
4 : * For further information, consult LICENSE.txt
5 : */
6 :
7 1406 : void logprintf(LOGGER log, unsigned level, char* fmt, ...){
8 : va_list args;
9 : va_list copy;
10 : char timestring[LOGGER_TIMESTRING_LEN];
11 :
12 1406 : va_start(args, fmt);
13 :
14 1406 : if(log.print_timestamp){
15 1406 : if(common_tprintf("%a, %d %b %Y %T %z", time(NULL), timestring, sizeof(timestring) - 1) < 0){
16 0 : snprintf(timestring, sizeof(timestring)-1, "Time failed");
17 : }
18 : }
19 :
20 1406 : if(log.log_secondary){
21 41 : if(log.verbosity >= level){
22 41 : va_copy(copy, args);
23 41 : if(log.print_timestamp){
24 41 : fprintf(stderr, "%s ", timestring);
25 : }
26 41 : vfprintf(stderr, fmt, copy);
27 41 : fflush(stderr);
28 41 : va_end(copy);
29 : }
30 : }
31 :
32 1406 : if(log.verbosity >= level){
33 1160 : if(log.print_timestamp){
34 1160 : fprintf(log.stream, "%s ", timestring);
35 : }
36 1160 : vfprintf(log.stream, fmt, args);
37 1160 : fflush(log.stream);
38 : }
39 1406 : va_end(args);
40 1406 : }
41 :
42 0 : void log_dump_buffer(LOGGER log, unsigned level, void* buffer, size_t bytes){
43 0 : uint8_t* data = (uint8_t*)buffer;
44 : size_t i;
45 :
46 0 : logprintf(log, level, "Buffer dump (%d bytes)\n", bytes);
47 :
48 0 : for(i = 0; i < bytes; i++){
49 0 : logprintf(log, level, "(%d, %c, %02x)", i, isprint(data[i]) ? data[i]:'.', data[i]);
50 0 : if(i && i % 8 == 0){
51 0 : logprintf(log, level, "\n");
52 : }
53 : }
54 :
55 0 : logprintf(log, level, "\n");
56 0 : }
|