WIP, more context changes
[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
02d1d628
AMH
22/*
23 * Logging is active
24 */
25
10ea52a3 26int
696cb85d 27im_init_log(pIMCTX, const char* name,int level) {
10ea52a3 28 i_clear_error();
696cb85d 29 aIMCTX->log_level = level;
02d1d628 30 if (level < 0) {
696cb85d 31 aIMCTX->lg_file = NULL;
02d1d628
AMH
32 } else {
33 if (name == NULL) {
696cb85d 34 aIMCTX->lg_file = stderr;
02d1d628 35 } else {
696cb85d 36 if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) {
d03fd5a4 37 i_push_errorf(errno, "Cannot open file '%s': (%d)", name, errno);
10ea52a3 38 return 0;
02d1d628
AMH
39 }
40 }
41 }
696cb85d
TC
42 if (aIMCTX->lg_file) {
43 setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ);
d03fd5a4 44 mm_log((0,"Imager - log started (level = %d)\n", level));
10ea52a3
TC
45 }
46
696cb85d 47 return aIMCTX->lg_file != NULL;
02d1d628
AMH
48}
49
696cb85d 50#if 0
02d1d628 51void
b1e96952 52i_fatal(int exitcode,const char *fmt, ... ) {
02d1d628 53 va_list ap;
d03fd5a4
TC
54 time_t timi;
55 struct tm *str_tm;
a482206e 56
d03fd5a4
TC
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);
a482206e 62 va_start(ap,fmt);
d03fd5a4 63 vfprintf(lg_file,fmt,ap);
a482206e
TC
64 va_end(ap);
65 }
66 exit(exitcode);
67}
02d1d628 68
696cb85d 69#endif
d03fd5a4 70
bd8052a6
TC
71/*
72=item i_loog(level, format, ...)
73=category Logging
74
75This is an internal function called by the mm_log() macro.
76
77=cut
78*/
02d1d628 79
696cb85d
TC
80static void
81im_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
02d1d628 98void
bf1573f9 99i_loog(int level,const char *fmt, ... ) {
696cb85d 100 pIMCTX = im_get_context();
02d1d628 101 va_list ap;
696cb85d
TC
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
111void
112im_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);
02d1d628
AMH
121}
122
6cfee9d1
TC
123/*
124=item i_lhead(file, line)
125=category Logging
126
127This is an internal function called by the mm_log() macro.
128
129=cut
130*/
02d1d628
AMH
131
132void
696cb85d
TC
133im_lhead(pIMCTX, const char *file, int line) {
134 if (aIMCTX->lg_file != NULL) {
135 aIMCTX->filename = file;
136 aIMCTX->line = line;
02d1d628
AMH
137 }
138}
10ea52a3 139
696cb85d
TC
140void i_lhead(const char *file, int line) {
141 pIMCTX = im_get_context();
142
143 im_lhead(aIMCTX, file, line);
144}
145
10ea52a3
TC
146#else
147
148/*
149 * Logging is inactive - insert dummy functions
150 */
151
152int i_init_log(const char* name,int onoff) {
153 i_clear_error();
154 i_push_error(0, "Logging disabled");
155 return 0;
156}
157
158void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
159
160void
161i_loog(int level,const char *fmt, ... ) {
162}
163
164void
165i_lhead(const char *file, int line) { }
166
167#endif