Log the queue size before and after journal rotation, to see if rotation is killing us.
[lwes-journaller.git] / src / log.c
blobbbc4beb9110bcab9829b29a76aef3e8dae08f817
1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
4 * *
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. *
9 * *
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. *
14 * *
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 *======================================================================*/
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <time.h>
29 #include <errno.h>
30 #include "log.h"
31 #include "lwes_mondemand.h"
32 #include "opt.h"
33 #include "sig.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)
47 fclose(log);
48 log = NULL;
49 gbl_rotate_log = 0;
52 /* if we have no log open now, open one. */
53 if (log==NULL)
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)
69 switch (level)
71 case LOG_OFF:
72 return "OFF";
73 case LOG_ERROR:
74 return "ERR";
75 case LOG_WARNING:
76 return "WARN";
77 case LOG_INFO:
78 return "INFO";
79 case LOG_PROGRESS:
80 return "PROG";
81 default:
82 return "OTHER";
86 void log_msg(log_level_t level, const char* fname, int lineno, const char* format, ...)
88 char buf[1024];
89 va_list ap;
90 FILE* log;
91 char timestr[100];
92 time_t t;
94 /* check for illegal logging level */
95 if(level <= 0 || level > LOG_PROGRESS)
97 printf ("logging level was %d\n", level);
98 return;
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);
106 va_end(ap);
108 t = time(NULL);
109 if (strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", gmtime(&t))==0)
111 strncpy(timestr, "TIME ERROR", sizeof(timestr)-1);
113 log = get_log();
114 fprintf(log, "%s %s %s:%d : %s", timestr, log_get_level_string(level), fname, lineno, buf);
115 fflush(log);
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)
129 *str = '\0';
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';