Commit | Line | Data |
---|---|---|
835813fd | 1 | #define IMAGER_NO_CONTEXT |
696cb85d | 2 | #include "imageri.h" |
e11d297f | 3 | #include "imconfig.h" |
02d1d628 | 4 | #include "log.h" |
35891892 | 5 | #include <stdlib.h> |
10ea52a3 | 6 | #include <errno.h> |
8d14daab | 7 | #include "imerror.h" |
10ea52a3 TC |
8 | |
9 | #ifdef IMAGER_LOG | |
02d1d628 AMH |
10 | |
11 | #define DTBUFF 50 | |
12 | #define DATABUFF DTBUFF+3+10+1+5+1+1 | |
13 | ||
696cb85d | 14 | #define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S" |
a482206e | 15 | |
44d86483 TC |
16 | static void |
17 | im_vloog(pIMCTX, int level, const char *fmt, va_list ap); | |
18 | ||
02d1d628 AMH |
19 | /* |
20 | * Logging is active | |
21 | */ | |
22 | ||
10ea52a3 | 23 | int |
696cb85d | 24 | im_init_log(pIMCTX, const char* name,int level) { |
10ea52a3 | 25 | i_clear_error(); |
6ee0c41f TC |
26 | |
27 | if (aIMCTX->lg_file) { | |
28 | if (aIMCTX->own_log) | |
29 | fclose(aIMCTX->lg_file); | |
30 | aIMCTX->lg_file = NULL; | |
31 | } | |
32 | ||
696cb85d | 33 | aIMCTX->log_level = level; |
02d1d628 | 34 | if (level < 0) { |
696cb85d | 35 | aIMCTX->lg_file = NULL; |
02d1d628 AMH |
36 | } else { |
37 | if (name == NULL) { | |
696cb85d | 38 | aIMCTX->lg_file = stderr; |
6ee0c41f | 39 | aIMCTX->own_log = 0; |
02d1d628 | 40 | } else { |
696cb85d | 41 | if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) { |
835813fd | 42 | im_push_errorf(aIMCTX, errno, "Cannot open file '%s': (%d)", name, errno); |
10ea52a3 | 43 | return 0; |
02d1d628 | 44 | } |
6ee0c41f TC |
45 | aIMCTX->own_log = 1; |
46 | setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ); | |
02d1d628 AMH |
47 | } |
48 | } | |
696cb85d | 49 | if (aIMCTX->lg_file) { |
835813fd | 50 | im_log((aIMCTX, 0,"Imager - log started (level = %d)\n", level)); |
10ea52a3 TC |
51 | } |
52 | ||
696cb85d | 53 | return aIMCTX->lg_file != NULL; |
02d1d628 AMH |
54 | } |
55 | ||
56 | void | |
b1e96952 | 57 | i_fatal(int exitcode,const char *fmt, ... ) { |
02d1d628 | 58 | va_list ap; |
835813fd | 59 | dIMCTX; |
44d86483 TC |
60 | |
61 | if (aIMCTX->lg_file != NULL) { | |
a482206e | 62 | va_start(ap,fmt); |
44d86483 | 63 | im_vloog(aIMCTX, 0, fmt, ap); |
a482206e TC |
64 | va_end(ap); |
65 | } | |
66 | exit(exitcode); | |
67 | } | |
02d1d628 | 68 | |
44d86483 TC |
69 | void |
70 | im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { | |
71 | va_list ap; | |
44d86483 TC |
72 | |
73 | if (aIMCTX->lg_file != NULL) { | |
74 | va_start(ap,fmt); | |
75 | im_vloog(aIMCTX, 0, fmt, ap); | |
76 | va_end(ap); | |
77 | } | |
78 | exit(exitcode); | |
79 | } | |
d03fd5a4 | 80 | |
bd8052a6 TC |
81 | /* |
82 | =item i_loog(level, format, ...) | |
83 | =category Logging | |
84 | ||
85 | This is an internal function called by the mm_log() macro. | |
86 | ||
87 | =cut | |
88 | */ | |
02d1d628 | 89 | |
696cb85d TC |
90 | static void |
91 | im_vloog(pIMCTX, int level, const char *fmt, va_list ap) { | |
92 | time_t timi; | |
93 | struct tm *str_tm; | |
94 | char date_buffer[DTBUFF]; | |
95 | ||
96 | if (!aIMCTX->lg_file || level > aIMCTX->log_level) | |
97 | return; | |
98 | ||
99 | timi = time(NULL); | |
100 | str_tm = localtime(&timi); | |
101 | strftime(date_buffer, DTBUFF, LOG_DATE_FORMAT, str_tm); | |
102 | fprintf(aIMCTX->lg_file, "[%s] %10s:%-5d %3d: ", date_buffer, | |
103 | aIMCTX->filename, aIMCTX->line, level); | |
104 | vfprintf(aIMCTX->lg_file, fmt, ap); | |
105 | fflush(aIMCTX->lg_file); | |
106 | } | |
107 | ||
02d1d628 | 108 | void |
bf1573f9 | 109 | i_loog(int level,const char *fmt, ... ) { |
835813fd | 110 | dIMCTX; |
02d1d628 | 111 | va_list ap; |
696cb85d TC |
112 | |
113 | if (!aIMCTX->lg_file || level > aIMCTX->log_level) | |
114 | return; | |
115 | ||
116 | va_start(ap,fmt); | |
117 | im_vloog(aIMCTX, level, fmt, ap); | |
118 | va_end(ap); | |
119 | } | |
120 | ||
121 | void | |
122 | im_loog(pIMCTX, int level,const char *fmt, ... ) { | |
123 | va_list ap; | |
124 | ||
125 | if (!aIMCTX->lg_file || level > aIMCTX->log_level) | |
126 | return; | |
127 | ||
128 | va_start(ap,fmt); | |
129 | im_vloog(aIMCTX, level, fmt, ap); | |
130 | va_end(ap); | |
02d1d628 AMH |
131 | } |
132 | ||
6cfee9d1 TC |
133 | /* |
134 | =item i_lhead(file, line) | |
135 | =category Logging | |
136 | ||
137 | This is an internal function called by the mm_log() macro. | |
138 | ||
139 | =cut | |
140 | */ | |
02d1d628 AMH |
141 | |
142 | void | |
696cb85d TC |
143 | im_lhead(pIMCTX, const char *file, int line) { |
144 | if (aIMCTX->lg_file != NULL) { | |
145 | aIMCTX->filename = file; | |
146 | aIMCTX->line = line; | |
02d1d628 AMH |
147 | } |
148 | } | |
10ea52a3 | 149 | |
696cb85d | 150 | void i_lhead(const char *file, int line) { |
835813fd | 151 | dIMCTX; |
696cb85d TC |
152 | |
153 | im_lhead(aIMCTX, file, line); | |
154 | } | |
155 | ||
10ea52a3 TC |
156 | #else |
157 | ||
158 | /* | |
159 | * Logging is inactive - insert dummy functions | |
160 | */ | |
161 | ||
835813fd | 162 | int im_init_log(pIMCTX, const char* name,int onoff) { |
10ea52a3 TC |
163 | i_clear_error(); |
164 | i_push_error(0, "Logging disabled"); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); } | |
44d86483 | 169 | void im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { exit(exitcode); } |
10ea52a3 TC |
170 | |
171 | void | |
172 | i_loog(int level,const char *fmt, ... ) { | |
173 | } | |
174 | ||
835813fd TC |
175 | void |
176 | im_loog(pIMCTX, int level,const char *fmt, ... ) { | |
177 | } | |
178 | ||
10ea52a3 TC |
179 | void |
180 | i_lhead(const char *file, int line) { } | |
181 | ||
835813fd TC |
182 | void |
183 | im_lhead(pIMCTX, const char *file, int line) { } | |
184 | ||
10ea52a3 | 185 | #endif |