Commit | Line | Data |
---|---|---|
e11d297f | 1 | #include "imconfig.h" |
02d1d628 | 2 | #include "log.h" |
35891892 | 3 | #include <stdlib.h> |
10ea52a3 | 4 | #include <errno.h> |
8d14daab | 5 | #include "imerror.h" |
10ea52a3 TC |
6 | |
7 | #ifdef IMAGER_LOG | |
02d1d628 AMH |
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; | |
d03fd5a4 | 14 | static char *date_format = "%Y/%m/%d %H:%M:%S"; |
02d1d628 AMH |
15 | static char date_buffer[DTBUFF]; |
16 | static char data_buffer[DATABUFF]; | |
02d1d628 | 17 | |
a482206e | 18 | |
02d1d628 AMH |
19 | /* |
20 | * Logging is active | |
21 | */ | |
22 | ||
10ea52a3 | 23 | int |
d03fd5a4 | 24 | i_init_log(const char* name,int level) { |
10ea52a3 | 25 | i_clear_error(); |
d03fd5a4 | 26 | log_level = level; |
02d1d628 | 27 | if (level < 0) { |
d03fd5a4 | 28 | lg_file = NULL; |
02d1d628 AMH |
29 | } else { |
30 | if (name == NULL) { | |
d03fd5a4 | 31 | lg_file = stderr; |
02d1d628 | 32 | } else { |
d03fd5a4 TC |
33 | if (NULL == (lg_file = fopen(name, "w+")) ) { |
34 | i_push_errorf(errno, "Cannot open file '%s': (%d)", name, errno); | |
10ea52a3 | 35 | return 0; |
02d1d628 AMH |
36 | } |
37 | } | |
38 | } | |
d03fd5a4 TC |
39 | if (lg_file) { |
40 | setvbuf(lg_file, NULL, _IONBF, BUFSIZ); | |
41 | mm_log((0,"Imager - log started (level = %d)\n", level)); | |
10ea52a3 TC |
42 | } |
43 | ||
d03fd5a4 | 44 | return lg_file != NULL; |
02d1d628 AMH |
45 | } |
46 | ||
47 | void | |
b1e96952 | 48 | i_fatal(int exitcode,const char *fmt, ... ) { |
02d1d628 | 49 | va_list ap; |
d03fd5a4 TC |
50 | time_t timi; |
51 | struct tm *str_tm; | |
a482206e | 52 | |
d03fd5a4 TC |
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); | |
a482206e | 58 | va_start(ap,fmt); |
d03fd5a4 | 59 | vfprintf(lg_file,fmt,ap); |
a482206e TC |
60 | va_end(ap); |
61 | } | |
62 | exit(exitcode); | |
63 | } | |
02d1d628 | 64 | |
d03fd5a4 | 65 | |
bd8052a6 TC |
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 | */ | |
02d1d628 AMH |
74 | |
75 | void | |
bf1573f9 | 76 | i_loog(int level,const char *fmt, ... ) { |
02d1d628 | 77 | va_list ap; |
d03fd5a4 TC |
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 | } | |
02d1d628 AMH |
87 | } |
88 | ||
6cfee9d1 TC |
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 | */ | |
02d1d628 AMH |
97 | |
98 | void | |
d03fd5a4 TC |
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 | |
02d1d628 AMH |
112 | } |
113 | } | |
10ea52a3 TC |
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 |