nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | if (today != NULL) { |
||
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 | } else { |
||
100 | fprintf(stderr, "Time not representable %8s %s %s\n", |
||
101 | log_domain != NULL ? log_domain : "", |
||
102 | level, message); |
||
103 | } |
||
104 | #ifdef _WIN32 |
||
105 | if(log_level & G_LOG_LEVEL_ERROR) { |
||
106 | /* wait for a key press before the following error handler will terminate the program |
||
107 | this way the user at least can read the error message */ |
||
108 | printf("\n\nPress any key to exit\n"); |
||
109 | _getch(); |
||
110 | } |
||
111 | } else { |
||
112 | /* XXX - on UN*X, should we just use g_log_default_handler()? |
||
113 | We want the error messages to go to the standard output; |
||
114 | on Mac OS X, that will cause them to show up in various |
||
115 | per-user logs accessible through Console (details depend |
||
116 | on whether you're running 10.0 through 10.4 or running |
||
117 | 10.5 and later), and, on other UN*X desktop environments, |
||
118 | if they don't show up in some form of console log, that's |
||
119 | a deficiency in that desktop environment. (Too bad |
||
120 | Windows doesn't set the standard output and error for |
||
121 | GUI apps to something that shows up in such a log.) */ |
||
122 | g_log_default_handler(log_domain, log_level, message, user_data); |
||
123 | } |
||
124 | #endif |
||
125 | } |
||
126 | |||
127 | void set_console_log_handler(void) |
||
128 | { |
||
129 | GLogLevelFlags log_flags; |
||
130 | /* Arrange that if we have no console window, and a GLib message logging |
||
131 | routine is called to log a message, we pop up a console window. |
||
132 | |||
133 | We do that by inserting our own handler for all messages logged |
||
134 | to the default domain; that handler pops up a console if necessary, |
||
135 | and then calls the default handler. */ |
||
136 | |||
137 | /* We might want to have component specific log levels later ... */ |
||
138 | |||
139 | log_flags = (GLogLevelFlags) |
||
140 | (G_LOG_LEVEL_ERROR| |
||
141 | G_LOG_LEVEL_CRITICAL| |
||
142 | G_LOG_LEVEL_WARNING| |
||
143 | G_LOG_LEVEL_MESSAGE| |
||
144 | G_LOG_LEVEL_INFO| |
||
145 | G_LOG_LEVEL_DEBUG| |
||
146 | G_LOG_FLAG_FATAL| |
||
147 | G_LOG_FLAG_RECURSION); |
||
148 | |||
149 | g_log_set_handler(NULL, |
||
150 | log_flags, |
||
151 | console_log_handler, NULL /* user_data */); |
||
152 | g_log_set_handler(LOG_DOMAIN_MAIN, |
||
153 | log_flags, |
||
154 | console_log_handler, NULL /* user_data */); |
||
155 | |||
156 | #ifdef HAVE_LIBPCAP |
||
157 | g_log_set_handler(LOG_DOMAIN_CAPTURE, |
||
158 | log_flags, |
||
159 | console_log_handler, NULL /* user_data */); |
||
160 | g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD, |
||
161 | log_flags, |
||
162 | console_log_handler, NULL /* user_data */); |
||
163 | |||
164 | #endif |
||
165 | } |
||
166 | |||
167 | /* |
||
168 | * Editor modelines |
||
169 | * |
||
170 | * Local Variables: |
||
171 | * c-basic-offset: 4 |
||
172 | * tab-width: 8 |
||
173 | * indent-tabs-mode: nil |
||
174 | * End: |
||
175 | * |
||
176 | * ex: set shiftwidth=4 tabstop=8 expandtab: |
||
177 | * :indentSize=4:tabSize=8:noTabs=true: |
||
178 | */ |