]> git.imager.perl.org - imager.git/blob - log.c
tests missing from MANIFEST
[imager.git] / log.c
1 #include "log.h"
2
3 #define DTBUFF 50
4 #define DATABUFF DTBUFF+3+10+1+5+1+1
5
6 static int   log_level   = 0;
7 static FILE *lg_file     = NULL;
8 static char *date_format = "%Y/%m/%d %H:%M:%S";
9 static char  date_buffer[DTBUFF];
10 static char  data_buffer[DATABUFF];
11
12
13 #ifdef IMAGER_LOG
14
15 /*
16  * Logging is active
17  */
18
19 void
20 init_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
37 void
38 m_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
61 void init_log(const char* name,int onoff) {}
62 void m_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
63
64
65 #endif
66
67
68 void
69 m_loog(int level,const char *fmt, ... ) {
70   va_list ap;
71   if (level > log_level) return;
72   if (lg_file != NULL) {
73     fputs(data_buffer, lg_file);
74     fprintf(lg_file, "%3d: ",level);
75     va_start(ap,fmt);
76     vfprintf(lg_file, fmt, ap);
77     fflush(lg_file);
78     va_end(ap);
79   }
80 }
81
82
83 void
84 m_lhead(const char *file, int line) {
85   time_t timi;
86   struct tm *str_tm;
87   
88   if (lg_file != NULL) {
89     timi = time(NULL);
90     str_tm = localtime(&timi);
91     strftime(date_buffer, DTBUFF, date_format, str_tm);
92     sprintf(data_buffer, "[%s] %10s:%-5d ", date_buffer, file, line);
93   }
94 }