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 <vlc_modules.h>
44 #include "../libvlc.h"
47 #include <android/log.h>
59 static void vlc_vaLogCallback(libvlc_int_t *vlc, int type,
60 const vlc_log_t *item, const char *format,
63 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
65 assert(logger != NULL);
66 vlc_rwlock_rdlock(&logger->lock);
67 logger->log(logger->sys, type, item, format, ap);
68 vlc_rwlock_unlock(&logger->lock);
71 static void vlc_LogCallback(libvlc_int_t *vlc, int type, const vlc_log_t *item,
72 const char *format, ...)
77 vlc_vaLogCallback(vlc, type, item, format, ap);
82 static void Win32DebugOutputMsg (void *, int , const vlc_log_t *,
83 const char *, va_list);
87 * Emit a log message. This function is the variable argument list equivalent
90 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
91 const char *format, va_list args)
93 if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
96 /* Get basename from the module filename */
97 char *p = strrchr(module, '/');
100 p = strchr(module, '.');
102 size_t modlen = (p != NULL) ? (p - module) : 0;
103 char modulebuf[modlen + 1];
106 memcpy(modulebuf, module, modlen);
107 modulebuf[modlen] = '\0';
111 /* Fill message information fields */
114 msg.i_object_id = (uintptr_t)obj;
115 msg.psz_object_type = (obj != NULL) ? obj->psz_object_type : "generic";
116 msg.psz_module = module;
117 msg.psz_header = NULL;
119 for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
120 if (o->psz_header != NULL)
122 msg.psz_header = o->psz_header;
130 Win32DebugOutputMsg (NULL, type, &msg, format, ap);
134 /* Pass message to the callback */
136 vlc_vaLogCallback(obj->p_libvlc, type, &msg, format, args);
140 * Emit a log message.
141 * \param obj VLC object emitting the message or NULL
142 * \param type VLC_MSG_* message type (info, error, warning or debug)
143 * \param module name of module from which the message come
144 * (normally MODULE_STRING)
145 * \param format printf-like message format
147 void vlc_Log(vlc_object_t *obj, int type, const char *module,
148 const char *format, ... )
152 va_start(ap, format);
153 vlc_vaLog(obj, type, module, format, ap);
158 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
160 static void Win32DebugOutputMsg (void* d, int type, const vlc_log_t *p_item,
161 const char *format, va_list dol)
165 const signed char *pverbose = d;
166 if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
171 int msg_len = vsnprintf(NULL, 0, format, dol2);
177 char *msg = malloc(msg_len + 1 + 1);
181 msg_len = vsnprintf(msg, msg_len+1, format, dol);
183 if(msg[msg_len-1] != '\n'){
185 msg[msg_len + 1] = '\0';
187 char* psz_msg = NULL;
188 if(asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
189 p_item->psz_object_type, msg_type[type], msg) > 0) {
190 wchar_t* wmsg = ToWide(psz_msg);
191 OutputDebugStringW(wmsg);
201 static void AndroidPrintMsg (void *d, int type, const vlc_log_t *p_item,
202 const char *format, va_list ap)
204 int verbose = (intptr_t)d;
207 if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
210 int canc = vlc_savecancel ();
213 if (asprintf (&format2, "[%0*"PRIxPTR"] %s %s: %s",
214 ptr_width, p_item->i_object_id, p_item->psz_module,
215 p_item->psz_object_type, format) < 0)
219 prio = ANDROID_LOG_INFO;
222 prio = ANDROID_LOG_ERROR;
225 prio = ANDROID_LOG_WARN;
229 prio = ANDROID_LOG_DEBUG;
231 __android_log_vprint (prio, "VLC", format2, ap);
233 vlc_restorecancel (canc);
237 typedef struct vlc_log_early_t
239 struct vlc_log_early_t *next;
248 vlc_log_early_t *head;
249 vlc_log_early_t **tailp;
250 } vlc_logger_early_t;
252 static void vlc_vaLogEarly(void *d, int type, const vlc_log_t *item,
253 const char *format, va_list ap)
255 vlc_logger_early_t *sys = d;
257 vlc_log_early_t *log = malloc(sizeof (*log));
258 if (unlikely(log == NULL))
263 log->meta.i_object_id = item->i_object_id;
264 /* NOTE: Object types MUST be static constant - no need to copy them. */
265 log->meta.psz_object_type = item->psz_object_type;
266 log->meta.psz_module = item->psz_module; /* Ditto. */
267 log->meta.psz_header = item->psz_header ? strdup(item->psz_header) : NULL;
269 int canc = vlc_savecancel(); /* XXX: needed for vasprintf() ? */
270 if (vasprintf(&log->msg, format, ap) == -1)
272 vlc_restorecancel(canc);
274 vlc_mutex_lock(&sys->lock);
275 assert(sys->tailp != NULL);
276 assert(*(sys->tailp) == NULL);
278 sys->tailp = &log->next;
279 vlc_mutex_unlock(&sys->lock);
282 static int vlc_LogEarlyOpen(vlc_logger_t *logger)
284 vlc_logger_early_t *sys = malloc(sizeof (*sys));
286 if (unlikely(sys == NULL))
289 vlc_mutex_init(&sys->lock);
291 sys->tailp = &sys->head;
293 logger->log = vlc_vaLogEarly;
298 static void vlc_LogEarlyClose(vlc_logger_t *logger, void *d)
300 libvlc_int_t *vlc = logger->p_libvlc;
301 vlc_logger_early_t *sys = d;
303 /* Drain early log messages */
304 for (vlc_log_early_t *log = sys->head, *next; log != NULL; log = next)
306 vlc_LogCallback(vlc, log->type, &log->meta, "%s",
307 (log->msg != NULL) ? log->msg : "message lost");
313 vlc_mutex_destroy(&sys->lock);
317 static void vlc_vaLogDiscard(void *d, int type, const vlc_log_t *item,
318 const char *format, va_list ap)
320 (void) d; (void) type; (void) item; (void) format; (void) ap;
323 static int vlc_logger_load(void *func, va_list ap)
325 vlc_log_cb (*activate)(vlc_object_t *, void **) = func;
326 vlc_logger_t *logger = va_arg(ap, vlc_logger_t *);
327 vlc_log_cb *cb = va_arg(ap, vlc_log_cb *);
328 void **sys = va_arg(ap, void **);
330 *cb = activate(VLC_OBJECT(logger), sys);
331 return (*cb != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
334 static void vlc_logger_unload(void *func, va_list ap)
336 void (*deactivate)(vlc_logger_t *) = func;
337 void *sys = va_arg(ap, void *);
343 * Performs preinitialization of the messages logging subsystem.
345 * Early log messages will be stored in memory until the subsystem is fully
346 * initialized with vlc_LogInit(). This enables logging before the
347 * configuration and modules bank are ready.
349 * \return 0 on success, -1 on error.
351 int vlc_LogPreinit(libvlc_int_t *vlc)
353 vlc_logger_t *logger = vlc_custom_create(vlc, sizeof (*logger), "logger");
355 libvlc_priv(vlc)->logger = logger;
357 if (unlikely(logger == NULL))
360 vlc_rwlock_init(&logger->lock);
362 if (vlc_LogEarlyOpen(logger))
364 logger->log = vlc_vaLogDiscard;
368 /* Announce who we are */
369 msg_Dbg(vlc, "VLC media player - %s", VERSION_MESSAGE);
370 msg_Dbg(vlc, "%s", COPYRIGHT_MESSAGE);
371 msg_Dbg(vlc, "revision %s", psz_vlc_changeset);
372 msg_Dbg(vlc, "configured with %s", CONFIGURE_LINE);
377 * Initializes the messages logging subsystem and drain the early messages to
378 * the configured log.
380 * \return 0 on success, -1 on error.
382 int vlc_LogInit(libvlc_int_t *vlc)
384 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
385 if (unlikely(logger == NULL))
389 void *sys, *early_sys = NULL;
391 /* TODO: module configuration item */
392 module_t *module = vlc_module_load(logger, "logger", NULL, false,
393 vlc_logger_load, logger, &cb, &sys);
396 cb = AndroidPrintMsg, sys = NULL;
398 cb = vlc_vaLogDiscard;
401 vlc_rwlock_wrlock(&logger->lock);
402 if (logger->log == vlc_vaLogEarly)
403 early_sys = logger->sys;
407 assert(logger->module == NULL); /* Only one call to vlc_LogInit()! */
408 logger->module = module;
409 vlc_rwlock_unlock(&logger->lock);
411 if (early_sys != NULL)
412 vlc_LogEarlyClose(logger, early_sys);
418 * Sets the message logging callback.
419 * \param cb message callback, or NULL to clear
420 * \param data data pointer for the message callback
422 void vlc_LogSet(libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
424 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
426 if (unlikely(logger == NULL))
433 cb = vlc_vaLogDiscard;
435 vlc_rwlock_wrlock(&logger->lock);
437 module = logger->module;
440 logger->sys = opaque;
441 logger->module = NULL;
442 vlc_rwlock_unlock(&logger->lock);
445 vlc_module_unload(module, vlc_logger_unload, sys);
447 /* Announce who we are */
448 msg_Dbg (vlc, "VLC media player - %s", VERSION_MESSAGE);
449 msg_Dbg (vlc, "%s", COPYRIGHT_MESSAGE);
450 msg_Dbg (vlc, "revision %s", psz_vlc_changeset);
451 msg_Dbg (vlc, "configured with %s", CONFIGURE_LINE);
454 void vlc_LogDeinit(libvlc_int_t *vlc)
456 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
458 if (unlikely(logger == NULL))
461 if (logger->module != NULL)
462 vlc_module_unload(logger->module, vlc_logger_unload, logger->sys);
464 /* Flush early log messages (corner case: no call to vlc_LogInit()) */
465 if (logger->log == vlc_vaLogEarly)
467 logger->log = vlc_vaLogDiscard;
468 vlc_LogEarlyClose(logger, logger->sys);
471 vlc_rwlock_destroy(&logger->lock);
472 vlc_object_release(logger);
473 libvlc_priv(vlc)->logger = NULL;