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