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