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

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)