1 /***************************************************************************
\r
2 * Copyright (C) 2005 by Dominic Rath *
\r
3 * Dominic.Rath@gmx.de *
\r
5 * This program is free software; you can redistribute it and/or modify *
\r
6 * it under the terms of the GNU General Public License as published by *
\r
7 * the Free Software Foundation; either version 2 of the License, or *
\r
8 * (at your option) any later version. *
\r
10 * This program is distributed in the hope that it will be useful, *
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
13 * GNU General Public License for more details. *
\r
15 * You should have received a copy of the GNU General Public License *
\r
16 * along with this program; if not, write to the *
\r
17 * Free Software Foundation, Inc., *
\r
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
19 ***************************************************************************/
\r
20 #ifdef HAVE_CONFIG_H
\r
25 #include "configuration.h"
\r
33 int debug_level = -1;
\r
35 static FILE* log_output;
\r
37 static void *privData;
\r
38 static logCallback callback;
\r
39 static time_t start;
\r
41 void log_setCallback(logCallback c, void *p)
\r
47 static char *log_strings[5] =
\r
56 void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
\r
58 static int count = 0;
\r
63 if (level > debug_level)
\r
66 va_start(args, format);
\r
67 vsnprintf(buffer, 512, format, args);
\r
69 char *f = strrchr(file, '/');
\r
73 if (debug_level >= LOG_DEBUG)
\r
75 /* print with count and time information */
\r
76 time_t t=time(NULL)-start;
\r
77 fprintf(log_output, "%s %d %ld %s:%d %s(): %s\n", log_strings[level+1], count, t, file, line, function, buffer);
\r
81 /* do not print count and time */
\r
82 fprintf(log_output, "%s %s:%d %s(): %s\n", log_strings[level+1], file, line, function, buffer);
\r
89 /* Never forward LOG_DEBUG, too verbose and they can be found in the log if need be */
\r
90 if (callback && (level <= LOG_INFO))
\r
92 va_start(args, format);
\r
93 callback(privData, file, line, function, format, args);
\r
98 /* change the current debug level on the fly
\r
101 * 2: + INFORMATIONAL MSGS
\r
104 int handle_debug_level_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
107 command_print(cmd_ctx, "debug_level: %i", debug_level);
\r
110 debug_level = strtoul(args[0], NULL, 0);
\r
112 if (debug_level < 0)
\r
115 if (debug_level > 3)
\r
121 int handle_log_output_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
125 FILE* file = fopen(args[0], "w");
\r
136 int log_register_commands(struct command_context_s *cmd_ctx)
\r
138 start = time(NULL);
\r
139 register_command(cmd_ctx, NULL, "log_output", handle_log_output_command,
\r
140 COMMAND_ANY, "redirect logging to <file> (default: stderr)");
\r
141 register_command(cmd_ctx, NULL, "debug_level", handle_debug_level_command,
\r
142 COMMAND_ANY, "adjust debug level <0-3>");
\r
147 int log_init(struct command_context_s *cmd_ctx)
\r
149 /* set defaults for daemon configuration, if not set by cmdline or cfgfile */
\r
150 if (debug_level == -1)
\r
151 debug_level = LOG_INFO;
\r
153 if (log_output == NULL)
\r
155 log_output = stderr;
\r
161 int set_log_output(struct command_context_s *cmd_ctx, FILE *output)
\r
163 log_output = output;
\r
167 /* return allocated string w/printf() result */
\r
168 char *allocPrintf(const char *fmt, va_list ap)
\r
170 char *string = NULL;
\r
172 /* start by 0 to exercise all the code paths. Need minimum 2 bytes to
\r
173 * fit 1 char and 0 terminator. */
\r
178 if ((string == NULL) || (!first))
\r
180 size = size * 2 + 2;
\r
182 string = realloc(string, size);
\r
183 if (string == NULL)
\r
192 ret = vsnprintf(string, size, fmt, ap);
\r
193 /* NB! The result of the vsnprintf() might be an *EMPTY* string! */
\r
194 if ((ret >= 0) && ((ret + 1) < size))
\r
198 /* there was just enough or not enough space, allocate more. */
\r