2 * Console log handler routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
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.
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.
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.
28 #include "epan/prefs.h"
35 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
36 const char *message, gpointer user_data _U_)
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
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) {
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 */
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. */
66 switch(log_level & G_LOG_LEVEL_MASK) {
67 case G_LOG_LEVEL_ERROR:
70 case G_LOG_LEVEL_CRITICAL:
73 case G_LOG_LEVEL_WARNING:
76 case G_LOG_LEVEL_MESSAGE:
79 case G_LOG_LEVEL_INFO:
82 case G_LOG_LEVEL_DEBUG:
86 fprintf(stderr, "unknown log_level %d\n", log_level);
88 g_assert_not_reached();
91 /* create a "timestamp" */
93 today = localtime(&curr);
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 : "",
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");
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);
122 void set_console_log_handler(void)
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.
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. */
132 /* We might want to have component specific log levels later ... */
134 log_flags = (GLogLevelFlags)
136 G_LOG_LEVEL_CRITICAL|
142 G_LOG_FLAG_RECURSION);
144 g_log_set_handler(NULL,
146 console_log_handler, NULL /* user_data */);
147 g_log_set_handler(LOG_DOMAIN_MAIN,
149 console_log_handler, NULL /* user_data */);
152 g_log_set_handler(LOG_DOMAIN_CAPTURE,
154 console_log_handler, NULL /* user_data */);
155 g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
157 console_log_handler, NULL /* user_data */);
168 * indent-tabs-mode: nil
171 * ex: set shiftwidth=4 tabstop=8 expandtab:
172 * :indentSize=4:tabSize=8:noTabs=true: