1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 *======================================================================*/
31 #include "lwes_mondemand.h"
35 static FILE* log
= NULL
;
37 static FILE* get_log()
39 /* if no log file was specified, log to stdout. */
40 /* note that this means /dev/null unless --nodaemonize is set */
41 if (arg_log_file
==NULL
) return stdout
;
43 /* if a log file rotation has been requested, close the current handle. */
44 /* it is probably the case that logrotate(8) has moved it to another name. */
45 if (gbl_rotate_log
&& log
)
52 /* if we have no log open now, open one. */
55 log
= fopen(arg_log_file
,"a+");
58 /* if we still have no log open, use stdout. */
59 return log
==NULL
? stdout
: log
;
62 static int is_logging(log_level_t level
)
64 return !!((1 << level
) & arg_log_level
);
67 static const char* log_get_level_string(int level
)
86 void log_msg(log_level_t level
, const char* fname
, int lineno
, const char* format
, ...)
94 /* check for illegal logging level */
95 if(level
<= 0 || level
> LOG_PROGRESS
)
97 printf ("logging level was %d\n", level
);
101 /* check for ignored message logging level */
102 if(! is_logging(level
)) return;
104 va_start(ap
, format
);
105 vsnprintf(buf
, sizeof(buf
), format
, ap
);
109 if (strftime(timestr
, sizeof(timestr
), "%Y-%m-%d %H:%M:%S", gmtime(&t
))==0)
111 strncpy(timestr
, "TIME ERROR", sizeof(timestr
)-1);
114 fprintf(log
, "%s %s %s:%d : %s", timestr
, log_get_level_string(level
), fname
, lineno
, buf
);
117 mondemand_log_msg(level
, fname
, lineno
, buf
);
120 static void log_append_masked_level(char* str
, int len
, int level
)
122 if (!is_logging(level
)) return;
123 strncat(str
, log_get_level_string(level
), len
- strlen(str
));
124 strncat(str
, " ", len
- strlen(str
));
127 void log_get_mask_string(char* str
, int len
)
131 log_append_masked_level(str
, len
, LOG_ERROR
);
132 log_append_masked_level(str
, len
, LOG_WARNING
);
133 log_append_masked_level(str
, len
, LOG_INFO
);
134 log_append_masked_level(str
, len
, LOG_PROGRESS
);
136 if (len
>0) str
[len
- 1] = '\0';