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"
55 static void vlc_vaLogCallback(libvlc_int_t *vlc, int type,
56 const vlc_log_t *item, const char *format,
59 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
61 assert(logger != NULL);
62 vlc_rwlock_rdlock(&logger->lock);
63 logger->log(logger->sys, type, item, format, ap);
64 vlc_rwlock_unlock(&logger->lock);
67 static void vlc_LogCallback(libvlc_int_t *vlc, int type, const vlc_log_t *item,
68 const char *format, ...)
73 vlc_vaLogCallback(vlc, type, item, format, ap);
78 static void Win32DebugOutputMsg (void *, int , const vlc_log_t *,
79 const char *, va_list);
83 * Emit a log message. This function is the variable argument list equivalent
86 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
87 const char *file, unsigned line, const char *func,
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;
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 file source module file name (normally __FILE__) or NULL
146 * \param line function call source line number (normally __LINE__) or 0
147 * \param func calling function name (normally __func__) or NULL
148 * \param format printf-like message format
150 void vlc_Log(vlc_object_t *obj, int type, const char *module,
151 const char *file, unsigned line, const char *func,
152 const char *format, ... )
156 va_start(ap, format);
157 vlc_vaLog(obj, type, module, file, line, func, format, ap);
162 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
164 static void Win32DebugOutputMsg (void* d, int type, const vlc_log_t *p_item,
165 const char *format, va_list dol)
169 const signed char *pverbose = d;
170 if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
175 int msg_len = vsnprintf(NULL, 0, format, dol2);
181 char *msg = malloc(msg_len + 1 + 1);
185 msg_len = vsnprintf(msg, msg_len+1, format, dol);
187 if(msg[msg_len-1] != '\n'){
189 msg[msg_len + 1] = '\0';
191 char* psz_msg = NULL;
192 if(asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
193 p_item->psz_object_type, msg_type[type], msg) > 0) {
194 wchar_t* wmsg = ToWide(psz_msg);
195 OutputDebugStringW(wmsg);
204 typedef struct vlc_log_early_t
206 struct vlc_log_early_t *next;
215 vlc_log_early_t *head;
216 vlc_log_early_t **tailp;
217 } vlc_logger_early_t;
219 static void vlc_vaLogEarly(void *d, int type, const vlc_log_t *item,
220 const char *format, va_list ap)
222 vlc_logger_early_t *sys = d;
224 vlc_log_early_t *log = malloc(sizeof (*log));
225 if (unlikely(log == NULL))
230 log->meta.i_object_id = item->i_object_id;
231 /* NOTE: Object types MUST be static constant - no need to copy them. */
232 log->meta.psz_object_type = item->psz_object_type;
233 log->meta.psz_module = item->psz_module; /* Ditto. */
234 log->meta.psz_header = item->psz_header ? strdup(item->psz_header) : NULL;
235 log->meta.file = item->file;
236 log->meta.line = item->line;
237 log->meta.func = item->func;
239 int canc = vlc_savecancel(); /* XXX: needed for vasprintf() ? */
240 if (vasprintf(&log->msg, format, ap) == -1)
242 vlc_restorecancel(canc);
244 vlc_mutex_lock(&sys->lock);
245 assert(sys->tailp != NULL);
246 assert(*(sys->tailp) == NULL);
248 sys->tailp = &log->next;
249 vlc_mutex_unlock(&sys->lock);
252 static int vlc_LogEarlyOpen(vlc_logger_t *logger)
254 vlc_logger_early_t *sys = malloc(sizeof (*sys));
256 if (unlikely(sys == NULL))
259 vlc_mutex_init(&sys->lock);
261 sys->tailp = &sys->head;
263 logger->log = vlc_vaLogEarly;
268 static void vlc_LogEarlyClose(vlc_logger_t *logger, void *d)
270 libvlc_int_t *vlc = logger->p_libvlc;
271 vlc_logger_early_t *sys = d;
273 /* Drain early log messages */
274 for (vlc_log_early_t *log = sys->head, *next; log != NULL; log = next)
276 vlc_LogCallback(vlc, log->type, &log->meta, "%s",
277 (log->msg != NULL) ? log->msg : "message lost");
283 vlc_mutex_destroy(&sys->lock);
287 static void vlc_vaLogDiscard(void *d, int type, const vlc_log_t *item,
288 const char *format, va_list ap)
290 (void) d; (void) type; (void) item; (void) format; (void) ap;
293 static int vlc_logger_load(void *func, va_list ap)
295 vlc_log_cb (*activate)(vlc_object_t *, void **) = func;
296 vlc_logger_t *logger = va_arg(ap, vlc_logger_t *);
297 vlc_log_cb *cb = va_arg(ap, vlc_log_cb *);
298 void **sys = va_arg(ap, void **);
300 *cb = activate(VLC_OBJECT(logger), sys);
301 return (*cb != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
304 static void vlc_logger_unload(void *func, va_list ap)
306 void (*deactivate)(vlc_logger_t *) = func;
307 void *sys = va_arg(ap, void *);
313 * Performs preinitialization of the messages logging subsystem.
315 * Early log messages will be stored in memory until the subsystem is fully
316 * initialized with vlc_LogInit(). This enables logging before the
317 * configuration and modules bank are ready.
319 * \return 0 on success, -1 on error.
321 int vlc_LogPreinit(libvlc_int_t *vlc)
323 vlc_logger_t *logger = vlc_custom_create(vlc, sizeof (*logger), "logger");
325 libvlc_priv(vlc)->logger = logger;
327 if (unlikely(logger == NULL))
330 vlc_rwlock_init(&logger->lock);
332 if (vlc_LogEarlyOpen(logger))
334 logger->log = vlc_vaLogDiscard;
338 /* Announce who we are */
339 msg_Dbg(vlc, "VLC media player - %s", VERSION_MESSAGE);
340 msg_Dbg(vlc, "%s", COPYRIGHT_MESSAGE);
341 msg_Dbg(vlc, "revision %s", psz_vlc_changeset);
342 msg_Dbg(vlc, "configured with %s", CONFIGURE_LINE);
347 * Initializes the messages logging subsystem and drain the early messages to
348 * the configured log.
350 * \return 0 on success, -1 on error.
352 int vlc_LogInit(libvlc_int_t *vlc)
354 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
355 if (unlikely(logger == NULL))
359 void *sys, *early_sys = NULL;
361 /* TODO: module configuration item */
362 module_t *module = vlc_module_load(logger, "logger", NULL, false,
363 vlc_logger_load, logger, &cb, &sys);
365 cb = vlc_vaLogDiscard;
367 vlc_rwlock_wrlock(&logger->lock);
368 if (logger->log == vlc_vaLogEarly)
369 early_sys = logger->sys;
373 assert(logger->module == NULL); /* Only one call to vlc_LogInit()! */
374 logger->module = module;
375 vlc_rwlock_unlock(&logger->lock);
377 if (early_sys != NULL)
378 vlc_LogEarlyClose(logger, early_sys);
384 * Sets the message logging callback.
385 * \param cb message callback, or NULL to clear
386 * \param data data pointer for the message callback
388 void vlc_LogSet(libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
390 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
392 if (unlikely(logger == NULL))
399 cb = vlc_vaLogDiscard;
401 vlc_rwlock_wrlock(&logger->lock);
403 module = logger->module;
406 logger->sys = opaque;
407 logger->module = NULL;
408 vlc_rwlock_unlock(&logger->lock);
411 vlc_module_unload(module, vlc_logger_unload, sys);
413 /* Announce who we are */
414 msg_Dbg (vlc, "VLC media player - %s", VERSION_MESSAGE);
415 msg_Dbg (vlc, "%s", COPYRIGHT_MESSAGE);
416 msg_Dbg (vlc, "revision %s", psz_vlc_changeset);
417 msg_Dbg (vlc, "configured with %s", CONFIGURE_LINE);
420 void vlc_LogDeinit(libvlc_int_t *vlc)
422 vlc_logger_t *logger = libvlc_priv(vlc)->logger;
424 if (unlikely(logger == NULL))
427 if (logger->module != NULL)
428 vlc_module_unload(logger->module, vlc_logger_unload, logger->sys);
430 /* Flush early log messages (corner case: no call to vlc_LogInit()) */
431 if (logger->log == vlc_vaLogEarly)
433 logger->log = vlc_vaLogDiscard;
434 vlc_LogEarlyClose(logger, logger->sys);
437 vlc_rwlock_destroy(&logger->lock);
438 vlc_object_release(logger);
439 libvlc_priv(vlc)->logger = NULL;