Added an Imager::log_entry() function and some extra logging
[imager.git] / log.c
CommitLineData
02d1d628
AMH
1#include "log.h"
2
3#define DTBUFF 50
4#define DATABUFF DTBUFF+3+10+1+5+1+1
5
6static int log_level = 0;
7static FILE *lg_file = NULL;
8static char *date_format = "%Y/%m/%d %H:%M:%S";
9static char date_buffer[DTBUFF];
10static char data_buffer[DATABUFF];
11
12
13#ifdef IMAGER_LOG
14
15/*
16 * Logging is active
17 */
18
19void
20init_log(const char* name,int level) {
21 log_level = level;
22 if (level < 0) {
23 lg_file = NULL;
24 } else {
25 if (name == NULL) {
26 lg_file = stderr;
27 } else {
28 if (NULL == (lg_file = fopen(name, "w+")) ) {
29 fprintf(stderr,"Cannot open file '%s'\n",name);
30 exit(2);
31 }
32 }
33 }
7f882a01 34 setvbuf(lg_file, NULL, _IONBF, BUFSIZ);
02d1d628
AMH
35 mm_log((0,"Imager - log started (level = %d)\n", level));
36}
37
38void
39m_fatal(int exitcode,const char *fmt, ... ) {
40 va_list ap;
41 time_t timi;
42 struct tm *str_tm;
43
44 if (lg_file != NULL) {
45 timi = time(NULL);
46 str_tm = localtime(&timi);
47 if ( strftime(date_buffer, DTBUFF, date_format, str_tm) )
48 fprintf(lg_file,"[%s] ",date_buffer);
49 va_start(ap,fmt);
50 vfprintf(lg_file,fmt,ap);
51 va_end(ap);
52 }
53 exit(exitcode);
54}
55
56#else
57
58/*
59 * Logging is inactive - insert dummy functions
8bb2a86a 60 */
02d1d628
AMH
61
62void init_log(const char* name,int onoff) {}
8bb2a86a 63void m_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
02d1d628 64
02d1d628
AMH
65
66#endif
67
68
69void
70m_loog(int level,const char *fmt, ... ) {
71 va_list ap;
72 if (level > log_level) return;
73 if (lg_file != NULL) {
74 fputs(data_buffer, lg_file);
75 fprintf(lg_file, "%3d: ",level);
76 va_start(ap,fmt);
77 vfprintf(lg_file, fmt, ap);
78 fflush(lg_file);
79 va_end(ap);
80 }
81}
82
83
84void
85m_lhead(const char *file, int line) {
86 time_t timi;
87 struct tm *str_tm;
88
89 if (lg_file != NULL) {
90 timi = time(NULL);
91 str_tm = localtime(&timi);
92 strftime(date_buffer, DTBUFF, date_format, str_tm);
93 sprintf(data_buffer, "[%s] %10s:%-5d ", date_buffer, file, line);
94 }
95}