52 |
#include "monitor.h" |
#include "monitor.h" |
53 |
|
|
54 |
|
|
|
/* Private variables */ |
|
|
static FILE *LOG= NULL; |
|
|
static char time_str[STRLEN]; |
|
|
|
|
|
/* Private prototypes */ |
|
|
static int open_log(); |
|
|
static char *timestr(void); |
|
|
|
|
|
|
|
55 |
/** |
/** |
56 |
* Implementation of a logger that appends log messages to a file |
* Implementation of a logger that appends log messages to a file |
57 |
* with a preceding timestamp. Methods support both syslog or own |
* with a preceding timestamp. Methods support both syslog or own |
65 |
*/ |
*/ |
66 |
|
|
67 |
|
|
68 |
|
/* ------------------------------------------------------- Private variables */ |
69 |
|
|
70 |
|
|
71 |
|
static FILE *LOG= NULL; |
72 |
|
static char time_str[STRLEN]; |
73 |
|
|
74 |
|
|
75 |
|
/* -------------------------------------------------------------- Prototypes */ |
76 |
|
|
77 |
|
|
78 |
|
static int open_log(); |
79 |
|
static char *timestr(void); |
80 |
|
|
81 |
|
|
82 |
/* ------------------------------------------------------------------ Public */ |
/* ------------------------------------------------------------------ Public */ |
83 |
|
|
84 |
|
|
115 |
*/ |
*/ |
116 |
void log(const char *format, ...) { |
void log(const char *format, ...) { |
117 |
|
|
|
char msg[STRLEN]; |
|
118 |
va_list ap; |
va_list ap; |
119 |
|
char msg[STRLEN]; |
120 |
|
|
121 |
ASSERT(format); |
ASSERT(format); |
122 |
|
|
131 |
syslog(LOG_ERR|LOG_USER, "%s", msg); |
syslog(LOG_ERR|LOG_USER, "%s", msg); |
132 |
|
|
133 |
} else if(LOG) { |
} else if(LOG) { |
134 |
|
|
135 |
fprintf(LOG,"[%s] %s", timestr(), msg); |
fprintf(LOG,"[%s] %s", timestr(), msg); |
|
fflush(LOG); |
|
136 |
|
|
137 |
} |
} |
138 |
|
|
170 |
} |
} |
171 |
|
|
172 |
|
|
|
/** |
|
|
* @return the log filedescriptor or -1 if it's not open, i.e. syslog |
|
|
* is used or logging is not activated |
|
|
*/ |
|
|
int get_log_fd() { |
|
|
|
|
|
return (LOG!=NULL?fileno(LOG):-1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
173 |
/* ----------------------------------------------------------------- Private */ |
/* ----------------------------------------------------------------- Private */ |
174 |
|
|
175 |
|
|
194 |
return(FALSE); |
return(FALSE); |
195 |
|
|
196 |
} |
} |
197 |
|
|
198 |
|
/* Set logger in unbuffered mode */ |
199 |
|
setvbuf(LOG, NULL, _IONBF, 0); |
200 |
|
|
201 |
} |
} |
202 |
|
|
203 |
return TRUE; |
return TRUE; |
212 |
static char *timestr() { |
static char *timestr() { |
213 |
|
|
214 |
time_t now; |
time_t now; |
215 |
|
struct tm tm; |
216 |
|
|
217 |
time(&now); |
time(&now); |
218 |
if(!strftime(time_str, STRLEN, TIMEFORMAT, localtime(&now))) { |
if(!strftime(time_str, STRLEN, TIMEFORMAT, localtime_r(&now, &tm))) { |
219 |
|
|
220 |
memset(time_str, 0, STRLEN); |
memset(time_str, 0, STRLEN); |
221 |
|
|