- added patch to remove count and time information from log_printf
[fw/openocd] / src / helper / log.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "log.h"
25 #include "configuration.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <time.h>
32
33 int debug_level = -1;
34
35 static FILE* log_output;
36
37 static void *privData;
38 static logCallback callback;
39
40 void log_setCallback(logCallback c, void *p)
41 {
42         callback = c;
43         privData = p;
44 }
45
46 static char *log_strings[5] = 
47 {
48         "User:  ",
49         "Error:  ",
50         "Warning:",
51         "Info:   ",
52         "Debug:  "
53 };
54
55 void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
56 {
57         static int count = 0;
58         count++;
59         va_list args;
60         char buffer[512];
61
62         if (level > debug_level)
63                 return;
64
65         va_start(args, format);
66         vsnprintf(buffer, 512, format, args);
67
68         char *f = strrchr(file, '/');
69         if (f != NULL)
70                 file = f + 1;
71
72         if (debug_level >= LOG_DEBUG)
73         {
74                 /* print with count and time information */
75                 fprintf(log_output, "%s %d %ld %s:%d %s(): %s\n", log_strings[level+1], count, time(NULL), file, line, function, buffer);
76         }
77         else
78         {
79                 /* do not print count and time */
80                 fprintf(log_output, "%s %s:%d %s(): %s\n", log_strings[level+1], file, line, function, buffer);
81         }
82
83         fflush(log_output);
84         
85         va_end(args);
86
87         /* Never forward LOG_DEBUG, too verbose and they can be found in the log if need be */
88         if (callback && (level <= LOG_INFO))
89         {
90                 va_start(args, format);
91                 callback(privData, file, line, function, format, args);
92                 va_end(args);
93         }
94 }
95
96 /* change the current debug level on the fly
97  * 0: only ERRORS
98  * 1: + WARNINGS
99  * 2: + INFORMATIONAL MSGS
100  * 3: + DEBUG MSGS
101  */
102 int handle_debug_level_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
103 {
104         if (argc == 0)
105                 command_print(cmd_ctx, "debug_level: %i", debug_level);
106
107         if (argc > 0)
108                 debug_level = strtoul(args[0], NULL, 0);
109
110         if (debug_level < 0)
111                 debug_level = 0;
112
113         if (debug_level > 3)
114                 debug_level = 3;
115
116         return ERROR_OK;
117 }
118
119 int handle_log_output_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
120 {
121         if (argc == 1)
122         {
123                 FILE* file = fopen(args[0], "w");
124                 
125                 if (file)
126                 {
127                         log_output = file;
128                 }
129         }
130
131         return ERROR_OK;
132 }
133
134 int log_register_commands(struct command_context_s *cmd_ctx)
135 {
136         register_command(cmd_ctx, NULL, "log_output", handle_log_output_command,
137                 COMMAND_ANY, "redirect logging to <file> (default: stderr)");
138         register_command(cmd_ctx, NULL, "debug_level", handle_debug_level_command,
139                 COMMAND_ANY, "adjust debug level <0-3>");
140
141         return ERROR_OK;
142 }
143
144 int log_init(struct command_context_s *cmd_ctx)
145 {
146         /* set defaults for daemon configuration, if not set by cmdline or cfgfile */
147         if (debug_level == -1)
148                 debug_level = LOG_INFO;
149         
150         if (log_output == NULL)
151         {
152                 log_output = stderr;
153         }
154         
155         return ERROR_OK;
156 }
157         
158 int set_log_output(struct command_context_s *cmd_ctx, FILE *output)
159 {
160         log_output = output;
161         return ERROR_OK;
162 }
163
164 /* return allocated string w/printf() result */
165 char *allocPrintf(const char *fmt, va_list ap)
166 {
167         char *string = NULL;
168         
169         /* start by 0 to exercise all the code paths. Need minimum 2 bytes to
170          * fit 1 char and 0 terminator. */
171         int size = 0;
172         int first = 1;
173         for (;;)
174         {
175                 if ((string == NULL) || (!first))
176                 {
177                         size = size * 2 + 2;
178                         char *t = string;
179                         string = realloc(string, size);
180                         if (string == NULL)
181                         {
182                                 if (t != NULL)
183                                         free(t);
184                                 return NULL;
185                         }
186                 }
187         
188             int ret;
189             ret = vsnprintf(string, size, fmt, ap);
190             /* NB! The result of the vsnprintf() might be an *EMPTY* string! */
191             if ((ret >= 0) && ((ret + 1) < size))
192             {
193                 return string;
194             }
195             /* there was just enough or not enough space, allocate more. */
196             first = 0;
197         }
198 }