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