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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)