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