update palimg.c with IMAGER_NO_CONTEXT
[imager.git] / log.c
CommitLineData
696cb85d 1#include "imageri.h"
e11d297f 2#include "imconfig.h"
02d1d628 3#include "log.h"
35891892 4#include <stdlib.h>
10ea52a3 5#include <errno.h>
8d14daab 6#include "imerror.h"
10ea52a3
TC
7
8#ifdef IMAGER_LOG
02d1d628
AMH
9
10#define DTBUFF 50
11#define DATABUFF DTBUFF+3+10+1+5+1+1
12
696cb85d 13#if 0
02d1d628
AMH
14static int log_level = 0;
15static FILE *lg_file = NULL;
02d1d628
AMH
16static char date_buffer[DTBUFF];
17static char data_buffer[DATABUFF];
696cb85d 18#endif
02d1d628 19
696cb85d 20#define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S"
a482206e 21
44d86483
TC
22static void
23im_vloog(pIMCTX, int level, const char *fmt, va_list ap);
24
02d1d628
AMH
25/*
26 * Logging is active
27 */
28
10ea52a3 29int
696cb85d 30im_init_log(pIMCTX, const char* name,int level) {
10ea52a3 31 i_clear_error();
696cb85d 32 aIMCTX->log_level = level;
02d1d628 33 if (level < 0) {
696cb85d 34 aIMCTX->lg_file = NULL;
02d1d628
AMH
35 } else {
36 if (name == NULL) {
696cb85d 37 aIMCTX->lg_file = stderr;
02d1d628 38 } else {
696cb85d 39 if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) {
d03fd5a4 40 i_push_errorf(errno, "Cannot open file '%s': (%d)", name, errno);
10ea52a3 41 return 0;
02d1d628
AMH
42 }
43 }
44 }
696cb85d
TC
45 if (aIMCTX->lg_file) {
46 setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ);
d03fd5a4 47 mm_log((0,"Imager - log started (level = %d)\n", level));
10ea52a3
TC
48 }
49
696cb85d 50 return aIMCTX->lg_file != NULL;
02d1d628
AMH
51}
52
53void
b1e96952 54i_fatal(int exitcode,const char *fmt, ... ) {
02d1d628 55 va_list ap;
d03fd5a4
TC
56 time_t timi;
57 struct tm *str_tm;
44d86483
TC
58 pIMCTX = im_get_context();
59
60 if (aIMCTX->lg_file != NULL) {
a482206e 61 va_start(ap,fmt);
44d86483 62 im_vloog(aIMCTX, 0, fmt, ap);
a482206e
TC
63 va_end(ap);
64 }
65 exit(exitcode);
66}
02d1d628 67
44d86483
TC
68void
69im_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}
d03fd5a4 81
bd8052a6
TC
82/*
83=item i_loog(level, format, ...)
84=category Logging
85
86This is an internal function called by the mm_log() macro.
87
88=cut
89*/
02d1d628 90
696cb85d
TC
91static void
92im_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
02d1d628 109void
bf1573f9 110i_loog(int level,const char *fmt, ... ) {
696cb85d 111 pIMCTX = im_get_context();
02d1d628 112 va_list ap;
696cb85d
TC
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
122void
123im_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);
02d1d628
AMH
132}
133
6cfee9d1
TC
134/*
135=item i_lhead(file, line)
136=category Logging
137
138This is an internal function called by the mm_log() macro.
139
140=cut
141*/
02d1d628
AMH
142
143void
696cb85d
TC
144im_lhead(pIMCTX, const char *file, int line) {
145 if (aIMCTX->lg_file != NULL) {
146 aIMCTX->filename = file;
147 aIMCTX->line = line;
02d1d628
AMH
148 }
149}
10ea52a3 150
696cb85d
TC
151void i_lhead(const char *file, int line) {
152 pIMCTX = im_get_context();
153
154 im_lhead(aIMCTX, file, line);
155}
156
10ea52a3
TC
157#else
158
159/*
160 * Logging is inactive - insert dummy functions
161 */
162
163int i_init_log(const char* name,int onoff) {
164 i_clear_error();
165 i_push_error(0, "Logging disabled");
166 return 0;
167}
168
169void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
44d86483 170void im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { exit(exitcode); }
10ea52a3
TC
171
172void
173i_loog(int level,const char *fmt, ... ) {
174}
175
176void
177i_lhead(const char *file, int line) { }
178
179#endif