doc: sdl_opengl_player: fix build
[vlc.git] / modules / logger / console.c
blob8266906a4d9b0ad5d14d5c794477c024261e7852
1 /*****************************************************************************
2 * console.c: console logger
3 *****************************************************************************
4 * Copyright © 1998-2005 VLC authors and VideoLAN
5 * Copyright © 2006-2015 Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <inttypes.h>
29 #include <unistd.h> /* isatty(), STDERR_FILNO */
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 static const int ptr_width = 2 * /* hex digits */ sizeof (uintptr_t);
35 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
36 static char verbosities[VLC_MSG_DBG];
38 #ifdef __OS2__
39 #include <vlc_charset.h>
41 static int OS2ConsoleOutput(FILE *stream, const char *format, va_list ap)
43 char *msg;
44 char *os2msg;
46 if (vasprintf(&msg, format, ap) == -1 )
47 return -1;
49 if ((os2msg = ToLocale(msg)) == NULL)
51 free(msg);
53 return -1;
56 fputs(os2msg, stream);
58 LocaleFree(os2msg);
59 free(msg);
61 return 0;
63 #endif
65 #ifndef _WIN32
66 # define COL(x,y) "\033[" #x ";" #y "m"
67 # define RED COL(31,1)
68 # define GREEN COL(32,1)
69 # define YELLOW COL(0,33)
70 # define WHITE COL(0,1)
71 # define GRAY "\033[0m"
72 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
74 static void LogConsoleColor(void *opaque, int type, const vlc_log_t *meta,
75 const char *format, va_list ap)
77 FILE *stream = stderr;
78 int verbose = (char *)opaque - verbosities;
80 if (verbose < type)
81 return;
83 flockfile(stream);
84 fprintf(stream, "["GREEN"%0*"PRIxPTR GRAY"] ", ptr_width,
85 meta->i_object_id);
86 if (meta->psz_header != NULL)
87 fprintf(stream, "[%s] ", meta->psz_header);
88 fprintf(stream, "%s %s%s: %s", meta->psz_module, meta->psz_object_type,
89 msg_type[type], msg_color[type]);
90 #ifdef __OS2__
91 if (OS2ConsoleOutput(stream, format, ap) == -1)
92 #endif
93 vfprintf(stream, format, ap);
94 fputs(GRAY"\n", stream);
95 funlockfile(stream);
98 static const struct vlc_logger_operations color_ops =
100 LogConsoleColor,
101 NULL
103 #endif /* !_WIN32 */
105 static void LogConsoleGray(void *opaque, int type, const vlc_log_t *meta,
106 const char *format, va_list ap)
108 FILE *stream = stderr;
109 int verbose = (char *)opaque - verbosities;
111 if (verbose < type)
112 return;
114 flockfile(stream);
115 fprintf(stream, "[%0*"PRIxPTR"] ", ptr_width, meta->i_object_id);
116 if (meta->psz_header != NULL)
117 fprintf(stream, "[%s] ", meta->psz_header);
118 fprintf(stream, "%s %s%s: ", meta->psz_module, meta->psz_object_type,
119 msg_type[type]);
120 #ifdef __OS2__
121 if (OS2ConsoleOutput(stream, format, ap) == -1)
122 #endif
123 vfprintf(stream, format, ap);
124 putc_unlocked('\n', stream);
125 funlockfile(stream);
128 static const struct vlc_logger_operations gray_ops =
130 LogConsoleGray,
131 NULL
134 static const struct vlc_logger_operations *Open(vlc_object_t *obj,
135 void **restrict sysp)
137 int verbosity = -1;
139 if (!var_InheritBool(obj, "quiet"))
141 const char *str = getenv("VLC_VERBOSE");
142 if (str != NULL)
143 verbosity = atoi(str);
144 else
145 verbosity = var_InheritInteger(obj, "verbose");
148 if (verbosity < 0)
149 return NULL;
151 verbosity += VLC_MSG_ERR;
152 if (verbosity > VLC_MSG_DBG)
153 verbosity = VLC_MSG_DBG;
155 *sysp = verbosities + verbosity;
157 #if defined (HAVE_ISATTY) && !defined (_WIN32)
158 if (isatty(STDERR_FILENO) && var_InheritBool(obj, "color"))
159 return &color_ops;
160 #endif
161 return &gray_ops;
164 #define QUIET_TEXT N_("Be quiet")
165 #define QUIET_LONGTEXT N_("Turn off all messages on the console.")
167 vlc_module_begin()
168 set_shortname(N_("Console log"))
169 set_description(N_("Console logger"))
170 set_category(CAT_ADVANCED)
171 set_subcategory(SUBCAT_ADVANCED_MISC)
172 set_capability("logger", 10)
173 set_callback(Open)
175 add_bool("quiet", false, QUIET_TEXT, QUIET_LONGTEXT, false)
176 change_short('q')
177 change_volatile()
178 vlc_module_end ()