1 /*****************************************************************************
2 * messages.c: messages interface
3 * This library provides an interface to the message queue to be used by other
4 * modules, especially intf modules. See vlc_config.h for output configuration.
5 *****************************************************************************
6 * Copyright (C) 1998-2005 VLC authors and VideoLAN
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
36 #include <stdarg.h> /* va_list for BSD */
40 #include <vlc_common.h>
41 #include <vlc_interface.h>
42 #include <vlc_charset.h>
43 #include "../libvlc.h"
46 #include <android/log.h>
56 static void vlc_vaLogCallback(libvlc_int_t *vlc, int type,
57 const vlc_log_t *item, const char *format,
60 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
62 assert(logger != NULL);
63 vlc_rwlock_rdlock(&logger->lock);
64 logger->log(logger->sys, type, item, format, ap);
65 vlc_rwlock_unlock(&logger->lock);
68 static void vlc_LogCallback(libvlc_int_t *vlc, int type, const vlc_log_t *item,
69 const char *format, ...)
74 vlc_vaLogCallback(vlc, type, item, format, ap);
79 static void Win32DebugOutputMsg (void *, int , const vlc_log_t *,
80 const char *, va_list);
84 * Emit a log message. This function is the variable argument list equivalent
87 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
88 const char *format, va_list args)
90 if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
93 /* Get basename from the module filename */
94 char *p = strrchr(module, '/');
97 p = strchr(module, '.');
99 size_t modlen = (p != NULL) ? (p - module) : 0;
100 char modulebuf[modlen + 1];
103 memcpy(modulebuf, module, modlen);
104 modulebuf[modlen] = '\0';
108 /* Fill message information fields */
111 msg.i_object_id = (uintptr_t)obj;
112 msg.psz_object_type = (obj != NULL) ? obj->psz_object_type : "generic";
113 msg.psz_module = module;
114 msg.psz_header = NULL;
116 for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
117 if (o->psz_header != NULL)
119 msg.psz_header = o->psz_header;
127 Win32DebugOutputMsg (NULL, type, &msg, format, ap);
131 /* Pass message to the callback */
133 vlc_vaLogCallback(obj->p_libvlc, type, &msg, format, args);
137 * Emit a log message.
138 * \param obj VLC object emitting the message or NULL
139 * \param type VLC_MSG_* message type (info, error, warning or debug)
140 * \param module name of module from which the message come
141 * (normally MODULE_STRING)
142 * \param format printf-like message format
144 void vlc_Log(vlc_object_t *obj, int type, const char *module,
145 const char *format, ... )
149 va_start(ap, format);
150 vlc_vaLog(obj, type, module, format, ap);
154 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
155 #define COL(x,y) "\033[" #x ";" #y "m"
156 #define RED COL(31,1)
157 #define GREEN COL(32,1)
158 #define YELLOW COL(0,33)
159 #define WHITE COL(0,1)
160 #define GRAY "\033[0m"
161 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
163 /* Display size of a pointer */
164 static const int ptr_width = 2 * /* hex digits */ sizeof(uintptr_t);
166 static void PrintColorMsg (void *d, int type, const vlc_log_t *p_item,
167 const char *format, va_list ap)
169 FILE *stream = stderr;
170 int verbose = (intptr_t)d;
172 if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
175 int canc = vlc_savecancel ();
178 utf8_fprintf (stream, "["GREEN"%0*"PRIxPTR""GRAY"] ", ptr_width, p_item->i_object_id);
179 if (p_item->psz_header != NULL)
180 utf8_fprintf (stream, "[%s] ", p_item->psz_header);
181 utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
182 p_item->psz_object_type, msg_type[type], msg_color[type]);
183 utf8_vfprintf (stream, format, ap);
184 fputs (GRAY"\n", stream);
185 #if defined (_WIN32) || defined (__OS2__)
188 funlockfile (stream);
189 vlc_restorecancel (canc);
192 static void PrintMsg (void *d, int type, const vlc_log_t *p_item,
193 const char *format, va_list ap)
195 FILE *stream = stderr;
196 int verbose = (intptr_t)d;
198 if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
201 int canc = vlc_savecancel ();
204 utf8_fprintf (stream, "[%0*"PRIxPTR"] ", ptr_width, p_item->i_object_id);
205 if (p_item->psz_header != NULL)
206 utf8_fprintf (stream, "[%s] ", p_item->psz_header);
207 utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
208 p_item->psz_object_type, msg_type[type]);
209 utf8_vfprintf (stream, format, ap);
210 putc_unlocked ('\n', stream);
211 #if defined (_WIN32) || defined (__OS2__)
214 funlockfile (stream);
215 vlc_restorecancel (canc);
219 static void Win32DebugOutputMsg (void* d, int type, const vlc_log_t *p_item,
220 const char *format, va_list dol)
224 const signed char *pverbose = d;
225 if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
230 int msg_len = vsnprintf(NULL, 0, format, dol2);
236 char *msg = malloc(msg_len + 1 + 1);
240 msg_len = vsnprintf(msg, msg_len+1, format, dol);
242 if(msg[msg_len-1] != '\n'){
244 msg[msg_len + 1] = '\0';
246 char* psz_msg = NULL;
247 if(asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
248 p_item->psz_object_type, msg_type[type], msg) > 0) {
249 wchar_t* wmsg = ToWide(psz_msg);
250 OutputDebugStringW(wmsg);
260 static void AndroidPrintMsg (void *d, int type, const vlc_log_t *p_item,
261 const char *format, va_list ap)
263 int verbose = (intptr_t)d;
266 if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
269 int canc = vlc_savecancel ();
272 if (asprintf (&format2, "[%0*"PRIxPTR"] %s %s: %s",
273 ptr_width, p_item->i_object_id, p_item->psz_module,
274 p_item->psz_object_type, format) < 0)
278 prio = ANDROID_LOG_INFO;
281 prio = ANDROID_LOG_ERROR;
284 prio = ANDROID_LOG_WARN;
288 prio = ANDROID_LOG_DEBUG;
290 __android_log_vprint (prio, "VLC", format2, ap);
292 vlc_restorecancel (canc);
296 typedef struct vlc_log_early_t
298 struct vlc_log_early_t *next;
307 vlc_log_early_t *head;
308 vlc_log_early_t **tailp;
309 } vlc_logger_early_t;
311 static void vlc_vaLogEarly(void *d, int type, const vlc_log_t *item,
312 const char *format, va_list ap)
314 vlc_logger_early_t *sys = d;
316 vlc_log_early_t *log = malloc(sizeof (*log));
317 if (unlikely(log == NULL))
322 log->meta.i_object_id = item->i_object_id;
323 /* NOTE: Object types MUST be static constant - no need to copy them. */
324 log->meta.psz_object_type = item->psz_object_type;
325 log->meta.psz_module = item->psz_module; /* Ditto. */
326 log->meta.psz_header = item->psz_header ? strdup(item->psz_header) : NULL;
328 int canc = vlc_savecancel(); /* XXX: needed for vasprintf() ? */
329 if (vasprintf(&log->msg, format, ap) == -1)
331 vlc_restorecancel(canc);
333 vlc_mutex_lock(&sys->lock);
334 assert(sys->tailp != NULL);
335 assert(*(sys->tailp) == NULL);
337 sys->tailp = &log->next;
338 vlc_mutex_unlock(&sys->lock);
341 static int vlc_LogEarlyOpen(vlc_logger_t *logger)
343 vlc_logger_early_t *sys = malloc(sizeof (*sys));
345 if (unlikely(sys == NULL))
348 vlc_mutex_init(&sys->lock);
350 sys->tailp = &sys->head;
352 logger->log = vlc_vaLogEarly;
357 static void vlc_LogEarlyClose(libvlc_int_t *vlc, void *d)
359 vlc_logger_early_t *sys = d;
361 /* Drain early log messages */
362 for (vlc_log_early_t *log = sys->head, *next; log != NULL; log = next)
364 vlc_LogCallback(vlc, log->type, &log->meta, "%s",
365 (log->msg != NULL) ? log->msg : "message lost");
371 vlc_mutex_destroy(&sys->lock);
375 static void vlc_vaLogDiscard(void *d, int type, const vlc_log_t *item,
376 const char *format, va_list ap)
378 (void) d; (void) type; (void) item; (void) format; (void) ap;
382 * Performs preinitialization of the messages logging subsystem.
384 * Early log messages will be stored in memory until the subsystem is fully
385 * initialized with vlc_LogInit(). This enables logging before the
386 * configuration and modules bank are ready.
388 * \return 0 on success, -1 on error.
390 int vlc_LogPreinit(libvlc_int_t *vlc)
392 vlc_logger_t *logger = malloc(sizeof (*logger));
394 libvlc_priv(vlc)->logger = logger;
396 if (unlikely(logger == NULL))
399 vlc_rwlock_init(&logger->lock);
401 if (vlc_LogEarlyOpen(logger))
403 logger->log = vlc_vaLogDiscard;
407 /* Announce who we are */
408 msg_Dbg(vlc, "VLC media player - %s", VERSION_MESSAGE);
409 msg_Dbg(vlc, "%s", COPYRIGHT_MESSAGE);
410 msg_Dbg(vlc, "revision %s", psz_vlc_changeset);
411 msg_Dbg(vlc, "configured with %s", CONFIGURE_LINE);
416 * Initializes the messages logging subsystem and drain the early messages to
417 * the configured log.
419 * \return 0 on success, -1 on error.
421 int vlc_LogInit(libvlc_int_t *vlc)
423 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
424 void *early_sys = NULL;
425 vlc_log_cb cb = PrintMsg;
426 signed char verbosity;
428 if (unlikely(logger == NULL))
432 cb = AndroidPrintMsg;
433 #elif defined (HAVE_ISATTY) && !defined (_WIN32)
434 if (isatty(STDERR_FILENO) && var_InheritBool(vlc, "color"))
438 if (var_InheritBool(vlc, "quiet"))
442 const char *str = getenv("VLC_VERBOSE");
444 if (str == NULL || sscanf(str, "%hhd", &verbosity) < 1)
445 verbosity = var_InheritInteger(vlc, "verbose");
448 vlc_rwlock_wrlock(&logger->lock);
450 if (logger->log == vlc_vaLogEarly)
451 early_sys = logger->sys;
454 logger->sys = (void *)(intptr_t)verbosity;
455 vlc_rwlock_unlock(&logger->lock);
457 if (early_sys != NULL)
458 vlc_LogEarlyClose(vlc, early_sys);
464 * Sets the message logging callback.
465 * \param cb message callback, or NULL to clear
466 * \param data data pointer for the message callback
468 void vlc_LogSet(libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
470 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
472 if (unlikely(logger == NULL))
476 cb = vlc_vaLogDiscard;
478 vlc_rwlock_wrlock(&logger->lock);
480 logger->sys = opaque;
481 vlc_rwlock_unlock(&logger->lock);
483 /* Announce who we are */
484 msg_Dbg (vlc, "VLC media player - %s", VERSION_MESSAGE);
485 msg_Dbg (vlc, "%s", COPYRIGHT_MESSAGE);
486 msg_Dbg (vlc, "revision %s", psz_vlc_changeset);
487 msg_Dbg (vlc, "configured with %s", CONFIGURE_LINE);
490 void vlc_LogDeinit(libvlc_int_t *vlc)
492 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
494 if (unlikely(logger == NULL))
497 /* Flush early log messages (corner case: no call to vlc_LogInit()) */
498 if (logger->log == vlc_vaLogEarly)
500 logger->log = vlc_vaLogDiscard;
501 vlc_LogEarlyClose(vlc, logger->sys);
504 vlc_rwlock_destroy(&logger->lock);