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