- Added support for native MinGW builds (thanks to Spencer Oliver and Michael Fischer...
[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
32 int debug_level = -1;
33
34 static FILE* log_output;
35
36 static char *log_strings[4] = 
37 {
38         "Error:  ",
39         "Warning:",
40         "Info:   ",
41         "Debug:  ",
42 };
43
44 void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
45 {
46         va_list args;
47         char buffer[512];
48
49         if (level > debug_level)
50                 return;
51
52         va_start(args, format);
53         vsnprintf(buffer, 512, format, args);
54
55         fprintf(log_output, "%s %s:%d %s(): %s\n", log_strings[level], file, line, function, buffer);
56         fflush(log_output);
57         
58         va_end(args);
59 }
60
61 void short_log_printf(enum log_levels level, const char *format, ...)
62 {
63         va_list args;
64         char buffer[512];
65
66         if (level > debug_level)
67                 return;
68
69         va_start(args, format);
70         vsnprintf(buffer, 512, format, args);
71
72         fprintf(log_output, "%s %s\n", log_strings[level], buffer);
73         fflush(log_output);
74
75         va_end(args);
76 }
77
78 /* change the current debug level on the fly
79  * 0: only ERRORS
80  * 1: + WARNINGS
81  * 2: + INFORMATIONAL MSGS
82  * 3: + DEBUG MSGS
83  */
84 int handle_debug_level_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
85 {
86         if (argc == 0)
87                 command_print(cmd_ctx, "debug_level: %i", debug_level);
88
89         if (argc > 0)
90                 debug_level = strtoul(args[0], NULL, 0);
91
92         if (debug_level < 0)
93                 debug_level = 0;
94
95         if (debug_level > 3)
96                 debug_level = 3;
97
98         return ERROR_OK;
99 }
100
101 int handle_log_output_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
102 {
103         if (argc == 1)
104         {
105                 FILE* file = fopen(args[0], "w");
106                 
107                 if (file)
108                 {
109                         log_output = file;
110                 }
111         }
112
113         return ERROR_OK;
114 }
115
116 int log_register_commands(struct command_context_s *cmd_ctx)
117 {
118         register_command(cmd_ctx, NULL, "log_output", handle_log_output_command,
119                 COMMAND_ANY, "redirect logging to <file> (default: stderr)");
120         register_command(cmd_ctx, NULL, "debug_level", handle_debug_level_command,
121                 COMMAND_ANY, "adjust debug level <0-3>");
122
123         return ERROR_OK;
124 }
125
126 int log_init(struct command_context_s *cmd_ctx)
127 {
128         /* set defaults for daemon configuration, if not set by cmdline or cfgfile */
129         if (debug_level == -1)
130                 debug_level = LOG_INFO;
131         
132         if (log_output == NULL)
133         {
134                 log_output = stderr;
135         }
136         
137         return ERROR_OK;
138 }