tshark: load decode_as_entries file
[metze/wireshark/wip.git] / ui / console.c
1 /* console.c
2  * Console log handler routines
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26
27
28 #include "epan/prefs.h"
29
30 #include "console.h"
31
32 #include "log.h"
33
34 static void
35 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
36                     const char *message, gpointer user_data _U_)
37 {
38     time_t curr;
39     struct tm *today;
40     const char *level;
41
42
43     /* ignore log message, if log_level isn't interesting based
44        upon the console log preferences.
45        If the preferences haven't been loaded loaded yet, display the
46        message anyway.
47
48        The default console_log_level preference value is such that only
49          ERROR, CRITICAL and WARNING level messages are processed;
50          MESSAGE, INFO and DEBUG level messages are ignored.  */
51     if((log_level & G_LOG_LEVEL_MASK & prefs.console_log_level) == 0 &&
52         prefs.console_log_level != 0) {
53         return;
54     }
55
56 #ifdef _WIN32
57     if (prefs.gui_console_open != console_open_never || log_level & G_LOG_LEVEL_ERROR) {
58         /* the user wants a console or the application will terminate immediately */
59         create_console();
60     }
61     if (get_has_console()) {
62         /* For some unknown reason, the above doesn't appear to actually cause
63            anything to be sent to the standard output, so we'll just splat the
64            message out directly, just to make sure it gets out. */
65 #endif
66         switch(log_level & G_LOG_LEVEL_MASK) {
67             case G_LOG_LEVEL_ERROR:
68                 level = "Err ";
69                 break;
70             case G_LOG_LEVEL_CRITICAL:
71                 level = "Crit";
72                 break;
73             case G_LOG_LEVEL_WARNING:
74                 level = "Warn";
75                 break;
76             case G_LOG_LEVEL_MESSAGE:
77                 level = "Msg ";
78                 break;
79             case G_LOG_LEVEL_INFO:
80                 level = "Info";
81                 break;
82             case G_LOG_LEVEL_DEBUG:
83                 level = "Dbg ";
84                 break;
85             default:
86                 fprintf(stderr, "unknown log_level %d\n", log_level);
87                 level = NULL;
88                 g_assert_not_reached();
89         }
90
91         /* create a "timestamp" */
92         time(&curr);
93         today = localtime(&curr);
94
95         fprintf(stderr, "%02d:%02d:%02d %8s %s %s\n",
96                 today->tm_hour, today->tm_min, today->tm_sec,
97                 log_domain != NULL ? log_domain : "",
98                 level, message);
99 #ifdef _WIN32
100         if(log_level & G_LOG_LEVEL_ERROR) {
101             /* wait for a key press before the following error handler will terminate the program
102                this way the user at least can read the error message */
103             printf("\n\nPress any key to exit\n");
104             _getch();
105         }
106     } else {
107         /* XXX - on UN*X, should we just use g_log_default_handler()?
108            We want the error messages to go to the standard output;
109            on Mac OS X, that will cause them to show up in various
110            per-user logs accessible through Console (details depend
111            on whether you're running 10.0 through 10.4 or running
112            10.5 and later), and, on other UN*X desktop environments,
113            if they don't show up in some form of console log, that's
114            a deficiency in that desktop environment.  (Too bad
115            Windows doesn't set the standard output and error for
116            GUI apps to something that shows up in such a log.) */
117         g_log_default_handler(log_domain, log_level, message, user_data);
118     }
119 #endif
120 }
121
122 void set_console_log_handler(void)
123 {
124     GLogLevelFlags log_flags;
125     /* Arrange that if we have no console window, and a GLib message logging
126        routine is called to log a message, we pop up a console window.
127
128        We do that by inserting our own handler for all messages logged
129        to the default domain; that handler pops up a console if necessary,
130        and then calls the default handler. */
131
132     /* We might want to have component specific log levels later ... */
133
134     log_flags = (GLogLevelFlags)
135                 (G_LOG_LEVEL_ERROR|
136                  G_LOG_LEVEL_CRITICAL|
137                  G_LOG_LEVEL_WARNING|
138                  G_LOG_LEVEL_MESSAGE|
139                  G_LOG_LEVEL_INFO|
140                  G_LOG_LEVEL_DEBUG|
141                  G_LOG_FLAG_FATAL|
142                  G_LOG_FLAG_RECURSION);
143
144     g_log_set_handler(NULL,
145                       log_flags,
146                       console_log_handler, NULL /* user_data */);
147     g_log_set_handler(LOG_DOMAIN_MAIN,
148                       log_flags,
149                       console_log_handler, NULL /* user_data */);
150
151 #ifdef HAVE_LIBPCAP
152     g_log_set_handler(LOG_DOMAIN_CAPTURE,
153                       log_flags,
154                       console_log_handler, NULL /* user_data */);
155     g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
156                     log_flags,
157                     console_log_handler, NULL /* user_data */);
158
159 #endif
160 }
161
162 /*
163  * Editor modelines
164  *
165  * Local Variables:
166  * c-basic-offset: 4
167  * tab-width: 8
168  * indent-tabs-mode: nil
169  * End:
170  *
171  * ex: set shiftwidth=4 tabstop=8 expandtab:
172  * :indentSize=4:tabSize=8:noTabs=true:
173  */