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 *****************************************************************************/
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" };
38 #include <vlc_charset.h>
40 static int OS2ConsoleOutput(FILE *stream
, const char *format
, va_list ap
)
45 if (vasprintf(&msg
, format
, ap
) == -1 )
48 if ((os2msg
= ToLocale(msg
)) == NULL
)
55 fputs(os2msg
, stream
);
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
;
83 fprintf(stream
, "["GREEN
"%0*"PRIxPTR GRAY
"] ", ptr_width
,
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
]);
90 if (OS2ConsoleOutput(stream
, format
, ap
) == -1)
92 vfprintf(stream
, format
, ap
);
93 fputs(GRAY
"\n", stream
);
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
;
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
,
114 if (OS2ConsoleOutput(stream
, format
, ap
) == -1)
116 vfprintf(stream
, format
, ap
);
117 putc_unlocked('\n', stream
);
121 static vlc_log_cb
Open(vlc_object_t
*obj
, void **sysp
)
125 if (!var_InheritBool(obj
, "quiet"))
127 const char *str
= getenv("VLC_VERBOSE");
129 verbosity
= atoi(str
);
131 verbosity
= var_InheritInteger(obj
, "verbose");
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
;
144 return LogConsoleGray
;
147 #define QUIET_TEXT N_("Be quiet")
148 #define QUIET_LONGTEXT N_("Turn off all messages on the console.")
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)