-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog.c
195 lines (151 loc) · 3.58 KB
/
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
#define IMAGER_NO_CONTEXT
#include "imageri.h"
#include "imconfig.h"
#include "log.h"
#include <stdlib.h>
#include <errno.h>
#include "imerror.h"
#ifdef IMAGER_LOG
#define DTBUFF 50
#define DATABUFF DTBUFF+3+10+1+5+1+1
#define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S"
static i_mutex_t log_mutex;
static void
im_vloog(pIMCTX, int level, const char *fmt, va_list ap);
/*
* Logging is active
*/
int
im_init_log(pIMCTX, const char* name,int level) {
i_clear_error();
if (!log_mutex) {
log_mutex = i_mutex_new();
}
if (aIMCTX->lg_file) {
if (aIMCTX->own_log)
fclose(aIMCTX->lg_file);
aIMCTX->lg_file = NULL;
}
aIMCTX->log_level = level;
if (level < 0) {
aIMCTX->lg_file = NULL;
} else {
if (name == NULL) {
aIMCTX->lg_file = stderr;
aIMCTX->own_log = 0;
} else {
if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) {
im_push_errorf(aIMCTX, errno, "Cannot open file '%s': (%d)", name, errno);
return 0;
}
aIMCTX->own_log = 1;
setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ);
}
}
if (aIMCTX->lg_file) {
im_log((aIMCTX, 0,"Imager - log started (level = %d)\n", level));
}
return aIMCTX->lg_file != NULL;
}
void
i_fatal(int exitcode,const char *fmt, ... ) {
va_list ap;
dIMCTX;
if (aIMCTX->lg_file != NULL) {
va_start(ap,fmt);
im_vloog(aIMCTX, 0, fmt, ap);
va_end(ap);
}
exit(exitcode);
}
void
im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) {
va_list ap;
if (aIMCTX->lg_file != NULL) {
va_start(ap,fmt);
im_vloog(aIMCTX, 0, fmt, ap);
va_end(ap);
}
exit(exitcode);
}
/*
=item i_loog(level, format, ...)
=category Logging
This is an internal function called by the mm_log() macro.
=cut
*/
static void
im_vloog(pIMCTX, int level, const char *fmt, va_list ap) {
time_t timi;
struct tm *str_tm;
char date_buffer[DTBUFF];
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level)
return;
i_mutex_lock(log_mutex);
timi = time(NULL);
str_tm = localtime(&timi);
strftime(date_buffer, DTBUFF, LOG_DATE_FORMAT, str_tm);
fprintf(aIMCTX->lg_file, "[%s] %10s:%-5d %3d: ", date_buffer,
aIMCTX->filename, aIMCTX->line, level);
vfprintf(aIMCTX->lg_file, fmt, ap);
fflush(aIMCTX->lg_file);
i_mutex_unlock(log_mutex);
}
void
i_loog(int level,const char *fmt, ... ) {
dIMCTX;
va_list ap;
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level)
return;
va_start(ap,fmt);
im_vloog(aIMCTX, level, fmt, ap);
va_end(ap);
}
void
im_loog(pIMCTX, int level,const char *fmt, ... ) {
va_list ap;
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level)
return;
va_start(ap,fmt);
im_vloog(aIMCTX, level, fmt, ap);
va_end(ap);
}
/*
=item i_lhead(file, line)
=category Logging
This is an internal function called by the mm_log() macro.
=cut
*/
void
im_lhead(pIMCTX, const char *file, int line) {
if (aIMCTX && aIMCTX->lg_file != NULL) {
aIMCTX->filename = file;
aIMCTX->line = line;
}
}
void i_lhead(const char *file, int line) {
dIMCTX;
im_lhead(aIMCTX, file, line);
}
#else
/*
* Logging is inactive - insert dummy functions
*/
int im_init_log(pIMCTX, const char* name,int onoff) {
i_clear_error();
i_push_error(0, "Logging disabled");
return 0;
}
void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
void im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { exit(exitcode); }
void
i_loog(int level,const char *fmt, ... ) {
}
void
im_loog(pIMCTX, int level,const char *fmt, ... ) {
}
void
i_lhead(const char *file, int line) { }
void
im_lhead(pIMCTX, const char *file, int line) { }
#endif