Initial revision
[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 }
34 mm_log((0,"Imager - log started (level = %d)\n", level));
35}
36
37void
38m_fatal(int exitcode,const char *fmt, ... ) {
39 va_list ap;
40 time_t timi;
41 struct tm *str_tm;
42
43 if (lg_file != NULL) {
44 timi = time(NULL);
45 str_tm = localtime(&timi);
46 if ( strftime(date_buffer, DTBUFF, date_format, str_tm) )
47 fprintf(lg_file,"[%s] ",date_buffer);
48 va_start(ap,fmt);
49 vfprintf(lg_file,fmt,ap);
50 va_end(ap);
51 }
52 exit(exitcode);
53}
54
55#else
56
57/*
58 * Logging is inactive - insert dummy functions
59
60
61void init_log(const char* name,int onoff) {}
62void m_fatal(int exitcode,const char *fmt, ... ) { return(exitcode); }
63
64*/
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}