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" };
36 static char verbosities
[VLC_MSG_DBG
];
39 #include <vlc_charset.h>
41 static int OS2ConsoleOutput(FILE *stream
, const char *format
, va_list ap
)
46 if (vasprintf(&msg
, format
, ap
) == -1 )
49 if ((os2msg
= ToLocale(msg
)) == NULL
)
56 fputs(os2msg
, stream
);
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
;
84 fprintf(stream
, "["GREEN
"%0*"PRIxPTR GRAY
"] ", ptr_width
,
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
]);
91 if (OS2ConsoleOutput(stream
, format
, ap
) == -1)
93 vfprintf(stream
, format
, ap
);
94 fputs(GRAY
"\n", stream
);
98 static const struct vlc_logger_operations color_ops
=
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
;
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
,
121 if (OS2ConsoleOutput(stream
, format
, ap
) == -1)
123 vfprintf(stream
, format
, ap
);
124 putc_unlocked('\n', stream
);
128 static const struct vlc_logger_operations gray_ops
=
134 static const struct vlc_logger_operations
*Open(vlc_object_t
*obj
,
135 void **restrict sysp
)
139 if (!var_InheritBool(obj
, "quiet"))
141 const char *str
= getenv("VLC_VERBOSE");
143 verbosity
= atoi(str
);
145 verbosity
= var_InheritInteger(obj
, "verbose");
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"))
164 #define QUIET_TEXT N_("Be quiet")
165 #define QUIET_LONGTEXT N_("Turn off all messages on the console.")
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)
175 add_bool("quiet", false, QUIET_TEXT
, QUIET_LONGTEXT
, false)