1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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 *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include "configuration.h"
34 static FILE* log_output;
37 static void *privData;
38 static logCallback callback;
40 void log_setCallback(logCallback c, void *p)
46 static char *log_strings[4] =
54 void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
59 if (level > debug_level)
62 va_start(args, format);
63 vsnprintf(buffer, 512, format, args);
65 fprintf(log_output, "%s %s:%d %s(): %s\n", log_strings[level], file, line, function, buffer);
72 va_start(args, format);
74 callback(privData, file, line, function, format, args);
81 /* change the current debug level on the fly
84 * 2: + INFORMATIONAL MSGS
87 int handle_debug_level_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
90 command_print(cmd_ctx, "debug_level: %i", debug_level);
93 debug_level = strtoul(args[0], NULL, 0);
104 int handle_log_output_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
108 FILE* file = fopen(args[0], "w");
119 int log_register_commands(struct command_context_s *cmd_ctx)
121 register_command(cmd_ctx, NULL, "log_output", handle_log_output_command,
122 COMMAND_ANY, "redirect logging to <file> (default: stderr)");
123 register_command(cmd_ctx, NULL, "debug_level", handle_debug_level_command,
124 COMMAND_ANY, "adjust debug level <0-3>");
129 int log_init(struct command_context_s *cmd_ctx)
131 /* set defaults for daemon configuration, if not set by cmdline or cfgfile */
132 if (debug_level == -1)
133 debug_level = LOG_INFO;
135 if (log_output == NULL)
143 int set_log_output(struct command_context_s *cmd_ctx, FILE *output)
149 /* return allocated string w/printf() result */
150 char *allocPrintf(const char *fmt, va_list ap)
153 int size=0; // start by 0 to exercise all the code paths. Need minimum 2 bytes to fit 1 char and 0 terminator.
157 if ((string==NULL)||(!first))
161 string=realloc(string, size);
171 ret = vsnprintf(string, size, fmt, ap);
172 // NB! The result of the vsnprintf() might be an *EMPTY* string!
173 if ((ret>=0)&&((ret+1)<size))
177 // there was just enough or not enough space, allocate more.