/src/vlc/modules/logger/console.c
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * console.c: console logger |
3 | | ***************************************************************************** |
4 | | * Copyright © 1998-2005 VLC authors and VideoLAN |
5 | | * Copyright © 2006-2015 Rémi Denis-Courmont |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as published by |
9 | | * the Free Software Foundation; either version 2.1 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with this program; if not, write to the Free Software Foundation, |
19 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
20 | | *****************************************************************************/ |
21 | | |
22 | | #ifdef HAVE_CONFIG_H |
23 | | # include "config.h" |
24 | | #endif |
25 | | |
26 | | #include <stdarg.h> |
27 | | #include <stdio.h> |
28 | | #include <inttypes.h> |
29 | | #ifdef _WIN32 |
30 | | # include <io.h> // isatty() |
31 | | #endif |
32 | | #include <unistd.h> /* isatty(), STDERR_FILNO */ |
33 | | |
34 | | #include <vlc_common.h> |
35 | | #include <vlc_plugin.h> |
36 | | |
37 | | static const int ptr_width = 2 * /* hex digits */ sizeof (uintptr_t); |
38 | | static const char msg_type[4][9] = { "", " error", " warning", " debug" }; |
39 | | static char verbosities[VLC_MSG_DBG]; |
40 | | |
41 | | #ifdef __OS2__ |
42 | | #include <vlc_charset.h> |
43 | | |
44 | | static int OS2ConsoleOutput(FILE *stream, const char *format, va_list ap) |
45 | | { |
46 | | char *msg; |
47 | | char *os2msg; |
48 | | |
49 | | if (vasprintf(&msg, format, ap) == -1 ) |
50 | | return -1; |
51 | | |
52 | | if ((os2msg = ToLocale(msg)) == NULL) |
53 | | { |
54 | | free(msg); |
55 | | |
56 | | return -1; |
57 | | } |
58 | | |
59 | | fputs(os2msg, stream); |
60 | | |
61 | | LocaleFree(os2msg); |
62 | | free(msg); |
63 | | |
64 | | return 0; |
65 | | } |
66 | | #endif |
67 | | |
68 | | # define COL(x,y) "\033[" #x ";" #y "m" |
69 | | # define RED COL(31,1) |
70 | | # define GREEN COL(32,1) |
71 | | # define YELLOW COL(0,33) |
72 | | # define WHITE COL(0,1) |
73 | 0 | # define GRAY "\033[0m" |
74 | | static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY }; |
75 | | |
76 | | static void LogConsoleColor(void *opaque, int type, const vlc_log_t *meta, |
77 | | const char *format, va_list ap) |
78 | 0 | { |
79 | 0 | FILE *stream = stderr; |
80 | 0 | int verbose = (char *)opaque - verbosities; |
81 | |
|
82 | 0 | if (verbose < type) |
83 | 0 | return; |
84 | | |
85 | 0 | flockfile(stream); |
86 | 0 | fprintf(stream, "["GREEN"%0*"PRIxPTR GRAY"] ", ptr_width, |
87 | 0 | meta->i_object_id); |
88 | 0 | if (meta->psz_header != NULL) |
89 | 0 | fprintf(stream, "[%s] ", meta->psz_header); |
90 | 0 | fprintf(stream, "%s %s%s: %s", meta->psz_module, meta->psz_object_type, |
91 | 0 | msg_type[type], msg_color[type]); |
92 | | #ifdef __OS2__ |
93 | | if (OS2ConsoleOutput(stream, format, ap) == -1) |
94 | | #endif |
95 | 0 | vfprintf(stream, format, ap); |
96 | 0 | fputs(GRAY"\n", stream); |
97 | 0 | funlockfile(stream); |
98 | 0 | } |
99 | | |
100 | | static const struct vlc_logger_operations color_ops = |
101 | | { |
102 | | LogConsoleColor, |
103 | | NULL |
104 | | }; |
105 | | |
106 | | static void LogConsoleGray(void *opaque, int type, const vlc_log_t *meta, |
107 | | const char *format, va_list ap) |
108 | 0 | { |
109 | 0 | FILE *stream = stderr; |
110 | 0 | int verbose = (char *)opaque - verbosities; |
111 | |
|
112 | 0 | if (verbose < type) |
113 | 0 | return; |
114 | | |
115 | 0 | flockfile(stream); |
116 | 0 | fprintf(stream, "[%0*"PRIxPTR"] ", ptr_width, meta->i_object_id); |
117 | 0 | if (meta->psz_header != NULL) |
118 | 0 | fprintf(stream, "[%s] ", meta->psz_header); |
119 | 0 | fprintf(stream, "%s %s%s: ", meta->psz_module, meta->psz_object_type, |
120 | 0 | msg_type[type]); |
121 | | #ifdef __OS2__ |
122 | | if (OS2ConsoleOutput(stream, format, ap) == -1) |
123 | | #endif |
124 | 0 | vfprintf(stream, format, ap); |
125 | 0 | putc_unlocked('\n', stream); |
126 | 0 | funlockfile(stream); |
127 | 0 | } |
128 | | |
129 | | static const struct vlc_logger_operations gray_ops = |
130 | | { |
131 | | LogConsoleGray, |
132 | | NULL |
133 | | }; |
134 | | |
135 | | static const struct vlc_logger_operations *Open(vlc_object_t *obj, |
136 | | void **restrict sysp) |
137 | 2 | { |
138 | 2 | int verbosity = -1; |
139 | | |
140 | 2 | if (!var_InheritBool(obj, "quiet")) |
141 | 0 | { |
142 | 0 | const char *str = getenv("VLC_VERBOSE"); |
143 | 0 | if (str != NULL) |
144 | 0 | verbosity = atoi(str); |
145 | 0 | else |
146 | 0 | verbosity = var_InheritInteger(obj, "verbose"); |
147 | 0 | } |
148 | | |
149 | 2 | if (verbosity < 0) |
150 | 2 | return NULL; |
151 | | |
152 | 0 | verbosity += VLC_MSG_ERR; |
153 | 0 | if (verbosity > VLC_MSG_DBG) |
154 | 0 | verbosity = VLC_MSG_DBG; |
155 | |
|
156 | 0 | *sysp = verbosities + verbosity; |
157 | |
|
158 | 0 | #if defined (HAVE_ISATTY) |
159 | 0 | if (isatty(STDERR_FILENO) && var_InheritBool(obj, "color")) |
160 | 0 | return &color_ops; |
161 | 0 | #endif |
162 | 0 | return &gray_ops; |
163 | 0 | } |
164 | | |
165 | | #define QUIET_TEXT N_("Be quiet") |
166 | | #define QUIET_LONGTEXT N_("Turn off all messages on the console.") |
167 | | |
168 | 4 | vlc_module_begin() |
169 | 2 | set_shortname(N_("Console log")) |
170 | 2 | set_description(N_("Console logger")) |
171 | 2 | set_subcategory(SUBCAT_ADVANCED_MISC) |
172 | 2 | set_capability("logger", 10) |
173 | 2 | set_callback(Open) |
174 | | |
175 | 2 | add_bool("quiet", false, QUIET_TEXT, QUIET_LONGTEXT) |
176 | 2 | change_short('q') |
177 | 2 | change_volatile() |
178 | 2 | vlc_module_end () |