e5dc0a23e224e6193f3d3a86ee312ff1795eff1f
[openocd.git] / 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
38 static void *privData;
39 static logCallback callback;
40
41 void log_setCallback(logCallback c, void *p)
42 {
43 callback = c;
44 privData = p;
45 }
46
47 static char *log_strings[4] =
48 {
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 fprintf(log_output, "%s %d %ld %s:%d %s(): %s\n", log_strings[level], count, time(NULL), file, line, function, buffer);
73 fflush(log_output);
74
75 va_end(args);
76
77 /* Never forward LOG_DEBUG, too verbose and they can be found in the log if need be */
78 if (callback && (level <= LOG_INFO))
79 {
80 va_start(args, format);
81 callback(privData, file, line, function, format, args);
82 va_end(args);
83 }
84 }
85
86 /* change the current debug level on the fly
87 * 0: only ERRORS
88 * 1: + WARNINGS
89 * 2: + INFORMATIONAL MSGS
90 * 3: + DEBUG MSGS
91 */
92 int handle_debug_level_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
93 {
94 if (argc == 0)
95 command_print(cmd_ctx, "debug_level: %i", debug_level);
96
97 if (argc > 0)
98 debug_level = strtoul(args[0], NULL, 0);
99
100 if (debug_level < 0)
101 debug_level = 0;
102
103 if (debug_level > 3)
104 debug_level = 3;
105
106 return ERROR_OK;
107 }
108
109 int handle_log_output_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
110 {
111 if (argc == 1)
112 {
113 FILE* file = fopen(args[0], "w");
114
115 if (file)
116 {
117 log_output = file;
118 }
119 }
120
121 return ERROR_OK;
122 }
123
124 int log_register_commands(struct command_context_s *cmd_ctx)
125 {
126 register_command(cmd_ctx, NULL, "log_output", handle_log_output_command,
127 COMMAND_ANY, "redirect logging to <file> (default: stderr)");
128 register_command(cmd_ctx, NULL, "debug_level", handle_debug_level_command,
129 COMMAND_ANY, "adjust debug level <0-3>");
130
131 return ERROR_OK;
132 }
133
134 int log_init(struct command_context_s *cmd_ctx)
135 {
136 /* set defaults for daemon configuration, if not set by cmdline or cfgfile */
137 if (debug_level == -1)
138 debug_level = LOG_INFO;
139
140 if (log_output == NULL)
141 {
142 log_output = stderr;
143 }
144
145 return ERROR_OK;
146 }
147
148 int set_log_output(struct command_context_s *cmd_ctx, FILE *output)
149 {
150 log_output = output;
151 return ERROR_OK;
152 }
153
154 /* return allocated string w/printf() result */
155 char *allocPrintf(const char *fmt, va_list ap)
156 {
157 char *string = NULL;
158
159 /* start by 0 to exercise all the code paths. Need minimum 2 bytes to
160 * fit 1 char and 0 terminator. */
161 int size = 0;
162 int first = 1;
163 for (;;)
164 {
165 if ((string == NULL) || (!first))
166 {
167 size = size * 2 + 2;
168 char *t = string;
169 string = realloc(string, size);
170 if (string == NULL)
171 {
172 if (t != NULL)
173 free(t);
174 return NULL;
175 }
176 }
177
178 int ret;
179 ret = vsnprintf(string, size, fmt, ap);
180 /* NB! The result of the vsnprintf() might be an *EMPTY* string! */
181 if ((ret >= 0) && ((ret + 1) < size))
182 {
183 return string;
184 }
185 /* there was just enough or not enough space, allocate more. */
186 first = 0;
187 }
188 }