]> git.imager.perl.org - imager.git/blob - log.c
Autodetection of fileformat as loading.
[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 m_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   setvbuf(lg_file, NULL, _IONBF, BUFSIZ);
35   mm_log((0,"Imager - log started (level = %d)\n", level));
36 }
37
38 void
39 m_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
60  */
61
62 void init_log(const char* name,int onoff) {}
63 void m_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
64
65
66 #endif
67
68
69 void
70 m_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
84 void
85 m_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 }