mux:ts: convert vlc_tick_t to seconds explicitly using SEC_FROM_VLC_TICK()
[vlc.git] / modules / logger / console.c
blob39d71489adfdb9dc9cf36bc7c202b8386d713be1
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" };
37 #ifdef __OS2__
38 #include <vlc_charset.h>
40 static int OS2ConsoleOutput(FILE *stream, const char *format, va_list ap)
42 char *msg;
43 char *os2msg;
45 if (vasprintf(&msg, format, ap) == -1 )
46 return -1;
48 if ((os2msg = ToLocale(msg)) == NULL)
50 free(msg);
52 return -1;
55 fputs(os2msg, stream);
57 LocaleFree(os2msg);
58 free(msg);
60 return 0;
62 #endif
64 #ifndef _WIN32
65 # define COL(x,y) "\033[" #x ";" #y "m"
66 # define RED COL(31,1)
67 # define GREEN COL(32,1)
68 # define YELLOW COL(0,33)
69 # define WHITE COL(0,1)
70 # define GRAY "\033[0m"
71 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
73 static void LogConsoleColor(void *opaque, int type, const vlc_log_t *meta,
74 const char *format, va_list ap)
76 FILE *stream = stderr;
77 int verbose = (intptr_t)opaque;
79 if (verbose < type)
80 return;
82 flockfile(stream);
83 fprintf(stream, "["GREEN"%0*"PRIxPTR GRAY"] ", ptr_width,
84 meta->i_object_id);
85 if (meta->psz_header != NULL)
86 fprintf(stream, "[%s] ", meta->psz_header);
87 fprintf(stream, "%s %s%s: %s", meta->psz_module, meta->psz_object_type,
88 msg_type[type], msg_color[type]);
89 #ifdef __OS2__
90 if (OS2ConsoleOutput(stream, format, ap) == -1)
91 #endif
92 vfprintf(stream, format, ap);
93 fputs(GRAY"\n", stream);
94 funlockfile(stream);
96 #endif /* !_WIN32 */
98 static void LogConsoleGray(void *opaque, int type, const vlc_log_t *meta,
99 const char *format, va_list ap)
101 FILE *stream = stderr;
102 int verbose = (intptr_t)opaque;
104 if (verbose < type)
105 return;
107 flockfile(stream);
108 fprintf(stream, "[%0*"PRIxPTR"] ", ptr_width, meta->i_object_id);
109 if (meta->psz_header != NULL)
110 fprintf(stream, "[%s] ", meta->psz_header);
111 fprintf(stream, "%s %s%s: ", meta->psz_module, meta->psz_object_type,
112 msg_type[type]);
113 #ifdef __OS2__
114 if (OS2ConsoleOutput(stream, format, ap) == -1)
115 #endif
116 vfprintf(stream, format, ap);
117 putc_unlocked('\n', stream);
118 funlockfile(stream);
121 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
123 int verbosity = -1;
125 if (!var_InheritBool(obj, "quiet"))
127 const char *str = getenv("VLC_VERBOSE");
128 if (str != NULL)
129 verbosity = atoi(str);
130 else
131 verbosity = var_InheritInteger(obj, "verbose");
134 if (verbosity < 0)
135 return NULL;
137 verbosity += VLC_MSG_ERR;
138 *sysp = (void *)(uintptr_t)verbosity;
140 #if defined (HAVE_ISATTY) && !defined (_WIN32)
141 if (isatty(STDERR_FILENO) && var_InheritBool(obj, "color"))
142 return LogConsoleColor;
143 #endif
144 return LogConsoleGray;
147 #define QUIET_TEXT N_("Be quiet")
148 #define QUIET_LONGTEXT N_("Turn off all messages on the console.")
150 vlc_module_begin()
151 set_shortname(N_("Console log"))
152 set_description(N_("Console logger"))
153 set_category(CAT_ADVANCED)
154 set_subcategory(SUBCAT_ADVANCED_MISC)
155 set_capability("logger", 10)
156 set_callbacks(Open, NULL)
158 add_bool("quiet", false, QUIET_TEXT, QUIET_LONGTEXT, false)
159 change_short('q')
160 change_volatile()
161 vlc_module_end ()