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