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