]> git.imager.perl.org - imager.git/blob - log.c
the PERL_INITIALIZE_IMAGER_PERL_CALLBACKS was checking the wrong version number
[imager.git] / log.c
1 #include "imconfig.h"
2 #include "log.h"
3 #include <stdlib.h>
4 #include <errno.h>
5
6 #ifdef IMAGER_LOG
7
8 #define DTBUFF 50
9 #define DATABUFF DTBUFF+3+10+1+5+1+1
10
11 static int   log_level   = 0;
12 static FILE *lg_file     = NULL;
13 static char *date_format = "%Y/%m/%d %H:%M:%S";
14 static char  date_buffer[DTBUFF];
15 static char  data_buffer[DATABUFF];
16
17
18 /*
19  * Logging is active
20  */
21
22 int
23 i_init_log(const char* name,int level) {
24   i_clear_error();
25   log_level = level;
26   if (level < 0) {
27     lg_file = NULL;
28   } else {
29     if (name == NULL) {
30       lg_file = stderr;
31     } else {
32       if (NULL == (lg_file = fopen(name, "w+")) ) { 
33         i_push_errorf(errno, "Cannot open file '%s': (%d)", name, errno);
34         return 0;
35       }
36     }
37   }
38   if (lg_file) {
39     setvbuf(lg_file, NULL, _IONBF, BUFSIZ);
40     mm_log((0,"Imager - log started (level = %d)\n", level));
41   }
42
43   return lg_file != NULL;
44 }
45
46 void
47 i_fatal(int exitcode,const char *fmt, ... ) {
48   va_list ap;
49   time_t timi;
50   struct tm *str_tm;
51   
52   if (lg_file != NULL) {
53     timi = time(NULL);
54     str_tm = localtime(&timi);
55     if ( strftime(date_buffer, DTBUFF, date_format, str_tm) )
56       fprintf(lg_file,"[%s] ",date_buffer);
57     va_start(ap,fmt);
58     vfprintf(lg_file,fmt,ap);
59     va_end(ap);
60   }
61   exit(exitcode);
62 }
63
64
65 /*
66 =item i_loog(level, format, ...)
67 =category Logging
68
69 This is an internal function called by the mm_log() macro.
70
71 =cut
72 */
73
74 void
75 i_loog(int level,const char *fmt, ... ) {
76   va_list ap;
77   if (level > log_level) return;
78   if (lg_file != NULL) {
79     fputs(data_buffer, lg_file);
80     fprintf(lg_file, "%3d: ",level);
81     va_start(ap,fmt);
82     vfprintf(lg_file, fmt, ap);
83     fflush(lg_file);
84     va_end(ap);
85   }
86 }
87
88 /*
89 =item i_lhead(file, line)
90 =category Logging
91
92 This is an internal function called by the mm_log() macro.
93
94 =cut
95 */
96
97 void
98 i_lhead(const char *file, int line) {
99   time_t timi;
100   struct tm *str_tm;
101   
102   if (lg_file != NULL) {
103     timi = time(NULL);
104     str_tm = localtime(&timi);
105     strftime(date_buffer, DTBUFF, date_format, str_tm);
106     sprintf(data_buffer, "[%s] %10s:%-5d ", date_buffer, file, line);
107   }
108 }
109
110 #else
111
112 /*
113  * Logging is inactive - insert dummy functions
114  */
115
116 int i_init_log(const char* name,int onoff) {
117   i_clear_error();
118   i_push_error(0, "Logging disabled");
119   return 0;
120 }
121
122 void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
123
124 void
125 i_loog(int level,const char *fmt, ... ) {
126 }
127
128 void
129 i_lhead(const char *file, int line) { }
130
131 #endif