WIP, more context changes
[imager.git] / log.c
CommitLineData
156699af 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
156699af 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];
156699af 18#endif
02d1d628 19
156699af 20#define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S"
02d1d628 21
02d1d628
AMH
22/*
23 * Logging is active
24 */
25
10ea52a3 26int
156699af 27im_init_log(pIMCTX, const char* name,int level) {
10ea52a3 28 i_clear_error();
156699af 29 aIMCTX->log_level = level;
02d1d628 30 if (level < 0) {
156699af 31 aIMCTX->lg_file = NULL;
02d1d628
AMH
32 } else {
33 if (name == NULL) {
156699af 34 aIMCTX->lg_file = stderr;
02d1d628 35 } else {
156699af 36 if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) {
10ea52a3
TC
37 i_push_errorf(errno, "Cannot open file '%s': (%d)", name, errno);
38 return 0;
02d1d628
AMH
39 }
40 }
41 }
156699af
TC
42 if (aIMCTX->lg_file) {
43 setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ);
10ea52a3
TC
44 mm_log((0,"Imager - log started (level = %d)\n", level));
45 }
46
156699af 47 return aIMCTX->lg_file != NULL;
02d1d628
AMH
48}
49
156699af 50#if 0
02d1d628 51void
b1e96952 52i_fatal(int exitcode,const char *fmt, ... ) {
02d1d628
AMH
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
156699af 69#endif
02d1d628 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
156699af
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, ... ) {
156699af 100 pIMCTX = im_get_context();
02d1d628 101 va_list ap;
156699af
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
156699af
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
156699af
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