clean up some old junk in log.c
[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 void
17 im_vloog(pIMCTX, int level, const char *fmt, va_list ap);
18
19 /*
20  * Logging is active
21  */
22
23 int
24 im_init_log(pIMCTX, const char* name,int level) {
25   i_clear_error();
26
27   if (aIMCTX->lg_file) {
28     if (aIMCTX->own_log)
29       fclose(aIMCTX->lg_file);
30     aIMCTX->lg_file = NULL;
31   }
32   
33   aIMCTX->log_level = level;
34   if (level < 0) {
35     aIMCTX->lg_file = NULL;
36   } else {
37     if (name == NULL) {
38       aIMCTX->lg_file = stderr;
39       aIMCTX->own_log = 0;
40     } else {
41       if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) { 
42         im_push_errorf(aIMCTX, errno, "Cannot open file '%s': (%d)", name, errno);
43         return 0;
44       }
45       aIMCTX->own_log = 1;
46       setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ);
47     }
48   }
49   if (aIMCTX->lg_file) {
50     im_log((aIMCTX, 0,"Imager - log started (level = %d)\n", level));
51   }
52
53   return aIMCTX->lg_file != NULL;
54 }
55
56 void
57 i_fatal(int exitcode,const char *fmt, ... ) {
58   va_list ap;
59   dIMCTX;
60
61   if (aIMCTX->lg_file != NULL) {
62     va_start(ap,fmt);
63     im_vloog(aIMCTX, 0, fmt, ap);
64     va_end(ap);
65   }
66   exit(exitcode);
67 }
68
69 void
70 im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) {
71   va_list ap;
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 }
80
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 */
89
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
108 void
109 i_loog(int level,const char *fmt, ... ) {
110   dIMCTX;
111   va_list ap;
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);
131 }
132
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 */
141
142 void
143 im_lhead(pIMCTX, const char *file, int line) {
144   if (aIMCTX->lg_file != NULL) {
145     aIMCTX->filename = file;
146     aIMCTX->line = line;
147   }
148 }
149
150 void i_lhead(const char *file, int line) {
151   dIMCTX;
152
153   im_lhead(aIMCTX, file, line);
154 }
155
156 #else
157
158 /*
159  * Logging is inactive - insert dummy functions
160  */
161
162 int im_init_log(pIMCTX, const char* name,int onoff) {
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); }
169 void im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { exit(exitcode); }
170
171 void
172 i_loog(int level,const char *fmt, ... ) {
173 }
174
175 void
176 im_loog(pIMCTX, int level,const char *fmt, ... ) {
177 }
178
179 void
180 i_lhead(const char *file, int line) { }
181
182 void
183 im_lhead(pIMCTX, const char *file, int line) { }
184
185 #endif