log.c

filter: Statementstmt 63.2% 50 / 79 Branchbran 69.0% 29 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.2% 79 / 121 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« mutexnull.c ↑ Summary trim.im »
1 #define IMAGER_NO_CONTEXT
2 #include "imageri.h"
3 #include "imconfig.h"
4 #include "log.h"
5 #include <stdlib.h>
6 #include <errno.h>
7 #include "imerror.h"
8  
9 #ifdef IMAGER_LOG
10  
11 #define DTBUFF 50
12 #define DATABUFF DTBUFF+3+10+1+5+1+1
13  
14 #define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S"
15  
16 static i_mutex_t log_mutex;
17  
18 static void
19 im_vloog(pIMCTX, int level, const char *fmt, va_list ap);
20  
21 /*
22 * Logging is active
23 */
24  
25 int
26 56 im_init_log(pIMCTX, const char* name,int level) {
27 56 i_clear_error();
28  
29 56 ▶ if (!log_mutex) {
Branch: 1 branch
BranchTrueFalseTotal
if (!log_mutex) { 37 19 56
30 37 log_mutex = i_mutex_new();
31 }
32  
33 56 ▶ if (aIMCTX->lg_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (aIMCTX->lg_file) { 19 37 56
34 19 ▶ if (aIMCTX->own_log)
Branch: 1 branch
BranchTrueFalseTotal
if (aIMCTX->own_log) 19 0 19
35 19 fclose(aIMCTX->lg_file);
36 19 aIMCTX->lg_file = NULL;
37 }
38
39 56 aIMCTX->log_level = level;
40 56 ▶ if (level < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level < 0) { 19 37 56
41 19 aIMCTX->lg_file = NULL;
42 } else {
43 37 ▶ if (name == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (name == NULL) { 0 37 37
44 0 aIMCTX->lg_file = stderr;
45 0 aIMCTX->own_log = 0;
46 } else {
47 37 ▶ if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) { 0 37 37
48 0 im_push_errorf(aIMCTX, errno, "Cannot open file '%s': (%d)", name, errno);
49 0 return 0;
50 }
51 37 aIMCTX->own_log = 1;
52 37 setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ);
53 }
54 }
55 56 ▶ if (aIMCTX->lg_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (aIMCTX->lg_file) { 37 19 56
56 37 im_log((aIMCTX, 0,"Imager - log started (level = %d)\n", level));
57 }
58  
59 56 return aIMCTX->lg_file != NULL;
60 }
61  
62 void
63 0 i_fatal(int exitcode,const char *fmt, ... ) {
64 va_list ap;
65 0 dIMCTX;
66  
67 0 ▶ if (aIMCTX->lg_file != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (aIMCTX->lg_file != NULL) { 0 0 0
68 0 va_start(ap,fmt);
69 0 im_vloog(aIMCTX, 0, fmt, ap);
70 0 va_end(ap);
71 }
72 0 exit(exitcode);
73 }
74  
75 void
76 0 im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) {
77 va_list ap;
78
79 0 ▶ if (aIMCTX->lg_file != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (aIMCTX->lg_file != NULL) { 0 0 0
80 0 va_start(ap,fmt);
81 0 im_vloog(aIMCTX, 0, fmt, ap);
82 0 va_end(ap);
83 }
84 0 exit(exitcode);
85 }
86  
87 void
88 0 im_def_out_of_memory(pIMCTX, void *userdata, const char *func, size_t size) {
89 (void)userdata;
90  
91 0 im_log((aIMCTX, 0, "Out of memory %s allocating %zu bytes\n", func, size));
92 0 fprintf(stderr, "Out of memory\n");
93 0 }
94  
95 /*
96 =item im_out_of_memory(ctx, func, size)
97 =category Memory Management
98  
99 Called by Imager when mymalloc/i_malloc/myrealloc/i_realloc fail.
100  
101 Calls im_def_out_of_memory() by default, which prints to the log and
102 to stderr and calls abort().
103  
104 =cut
105 */
106  
107 void
108 0 im_out_of_memory(pIMCTX, const char *func, size_t size) {
109 0 aIMCTX->out_of_memory(aIMCTX, aIMCTX->out_of_memory_userdata,
110 func, size);
111 /* if the user function returns */
112 0 abort();
113 }
114  
115 void
116 0 im_set_out_of_memory(pIMCTX, i_out_of_memory_handler handler,
117 void *userdata) {
118 0 ▶ aIMCTX->out_of_memory = handler ? handler : im_def_out_of_memory;
Branch: 1 branch
BranchTrueFalseTotal
aIMCTX->out_of_memory = handler ? handler : im_def_out_of_memory; 0 0 0
119 0 aIMCTX->out_of_memory_userdata = userdata;
120 0 }
121  
122 /*
123 =item i_loog(level, format, ...)
124 =category Logging
125  
126 This is an internal function called by the mm_log() macro.
127  
128 =cut
129 */
130  
131 static void
132 62600 im_vloog(pIMCTX, int level, const char *fmt, va_list ap) {
133 time_t timi;
134 struct tm *str_tm;
135 char date_buffer[DTBUFF];
136  
137 62600 ▶ if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level)
Branch: 3 branches
BranchTrueFalseTotal
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 62600 0 62600
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 62600 0 62600
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 0 62600 62600
138 0 return;
139  
140 62600 i_mutex_lock(log_mutex);
141
142 62600 timi = time(NULL);
143 62600 str_tm = localtime(&timi);
144 62600 strftime(date_buffer, DTBUFF, LOG_DATE_FORMAT, str_tm);
145 62600 fprintf(aIMCTX->lg_file, "[%s] %10s:%-5d %3d: ", date_buffer,
146 aIMCTX->filename, aIMCTX->line, level);
147 62600 vfprintf(aIMCTX->lg_file, fmt, ap);
148 62600 fflush(aIMCTX->lg_file);
149  
150 62600 i_mutex_unlock(log_mutex);
151 }
152  
153 void
154 5505 i_loog(int level,const char *fmt, ... ) {
155 5505 dIMCTX;
156 va_list ap;
157  
158 5505 ▶ if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level)
Branch: 3 branches
BranchTrueFalseTotal
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 5505 0 5505
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 5419 86 5505
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 3450 1969 5419
159 3536 return;
160  
161 1969 va_start(ap,fmt);
162 1969 im_vloog(aIMCTX, level, fmt, ap);
163 1969 va_end(ap);
164 }
165  
166 void
167 82947 im_loog(pIMCTX, int level,const char *fmt, ... ) {
168 va_list ap;
169  
170 82947 ▶ if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level)
Branch: 3 branches
BranchTrueFalseTotal
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 82939 8 82947
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 63344 19595 82939
if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) 2713 60631 63344
171 22316 return;
172  
173 60631 va_start(ap,fmt);
174 60631 im_vloog(aIMCTX, level, fmt, ap);
175 60631 va_end(ap);
176 }
177  
178 /*
179 =item i_lhead(file, line)
180 =category Logging
181  
182 This is an internal function called by the mm_log() macro.
183  
184 =cut
185 */
186  
187 void
188 88452 im_lhead(pIMCTX, const char *file, int line) {
189 88452 ▶ if (aIMCTX && aIMCTX->lg_file != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (aIMCTX && aIMCTX->lg_file != NULL) { 88444 8 88452
if (aIMCTX && aIMCTX->lg_file != NULL) { 68763 19681 88444
190 68763 aIMCTX->filename = file;
191 68763 aIMCTX->line = line;
192 }
193 88452 }
194  
195 5505 void i_lhead(const char *file, int line) {
196 5505 dIMCTX;
197  
198 5505 im_lhead(aIMCTX, file, line);
199 5505 }
200  
201 #else
202  
203 /*
204 * Logging is inactive - insert dummy functions
205 */
206  
207 int im_init_log(pIMCTX, const char* name,int onoff) {
208 i_clear_error();
209 i_push_error(0, "Logging disabled");
210 return 0;
211 }
212  
213 void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
214 void im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { exit(exitcode); }
215  
216 void
217 i_loog(int level,const char *fmt, ... ) {
218 }
219  
220 void
221 im_loog(pIMCTX, int level,const char *fmt, ... ) {
222 }
223  
224 void
225 i_lhead(const char *file, int line) { }
226  
227 void
228 im_lhead(pIMCTX, const char *file, int line) { }
229  
230 #endif
« mutexnull.c ↑ Summary trim.im »