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