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