2 * Console log handler routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include "epan/prefs.h"
17 #include "wsutil/time_util.h"
24 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
25 const char *message, gpointer user_data _U_)
32 /* ignore log message, if log_level isn't interesting based
33 upon the console log preferences.
34 If the preferences haven't been loaded loaded yet, display the
37 The default console_log_level preference value is such that only
38 ERROR, CRITICAL and WARNING level messages are processed;
39 MESSAGE, INFO and DEBUG level messages are ignored. */
40 if((log_level & G_LOG_LEVEL_MASK & prefs.console_log_level) == 0 &&
41 prefs.console_log_level != 0) {
46 if (prefs.gui_console_open != console_open_never || log_level & G_LOG_LEVEL_ERROR) {
47 /* the user wants a console or the application will terminate immediately */
50 if (get_has_console()) {
51 /* For some unknown reason, the above doesn't appear to actually cause
52 anything to be sent to the standard output, so we'll just splat the
53 message out directly, just to make sure it gets out. */
55 switch(log_level & G_LOG_LEVEL_MASK) {
56 case G_LOG_LEVEL_ERROR:
59 case G_LOG_LEVEL_CRITICAL:
62 case G_LOG_LEVEL_WARNING:
65 case G_LOG_LEVEL_MESSAGE:
68 case G_LOG_LEVEL_INFO:
71 case G_LOG_LEVEL_DEBUG:
75 fprintf(stderr, "unknown log_level %d\n", log_level);
77 g_assert_not_reached();
80 /* create a "timestamp" */
82 today = localtime(&curr);
83 guint64 microseconds = create_timestamp();
85 fprintf(stderr, "%02d:%02d:%02d.%03" G_GUINT64_FORMAT " %8s %s %s\n",
86 today->tm_hour, today->tm_min, today->tm_sec,
87 microseconds % 1000000 / 1000,
88 log_domain != NULL ? log_domain : "",
91 fprintf(stderr, "Time not representable %8s %s %s\n",
92 log_domain != NULL ? log_domain : "",
96 if(log_level & G_LOG_LEVEL_ERROR) {
97 /* wait for a key press before the following error handler will terminate the program
98 this way the user at least can read the error message */
99 printf("\n\nPress any key to exit\n");
103 /* XXX - on UN*X, should we just use g_log_default_handler()?
104 We want the error messages to go to the standard output;
105 on macOS, that will cause them to show up in various
106 per-user logs accessible through Console (details depend
107 on whether you're running 10.0 through 10.4 or running
108 10.5 and later), and, on other UN*X desktop environments,
109 if they don't show up in some form of console log, that's
110 a deficiency in that desktop environment. (Too bad
111 Windows doesn't set the standard output and error for
112 GUI apps to something that shows up in such a log.) */
113 g_log_default_handler(log_domain, log_level, message, user_data);
118 void set_console_log_handler(void)
120 GLogLevelFlags log_flags;
121 /* Arrange that if we have no console window, and a GLib message logging
122 routine is called to log a message, we pop up a console window.
124 We do that by inserting our own handler for all messages logged
125 to the default domain; that handler pops up a console if necessary,
126 and then calls the default handler. */
128 /* We might want to have component specific log levels later ... */
130 log_flags = (GLogLevelFlags)
132 G_LOG_LEVEL_CRITICAL|
138 G_LOG_FLAG_RECURSION);
140 g_log_set_handler(NULL,
142 console_log_handler, NULL /* user_data */);
143 g_log_set_handler(LOG_DOMAIN_MAIN,
145 console_log_handler, NULL /* user_data */);
148 g_log_set_handler(LOG_DOMAIN_CAPTURE,
150 console_log_handler, NULL /* user_data */);
151 g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
153 console_log_handler, NULL /* user_data */);
164 * indent-tabs-mode: nil
167 * ex: set shiftwidth=4 tabstop=8 expandtab:
168 * :indentSize=4:tabSize=8:noTabs=true: