]> git.sesse.net Git - vlc/blob - src/misc/messages.c
logger: allow logger as a module
[vlc] / src / misc / messages.c
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
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Samuel Hocevar <sam@zoy.org>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <stdarg.h>                                       /* va_list for BSD */
37 #include <unistd.h>
38 #include <assert.h>
39
40 #include <vlc_common.h>
41 #include <vlc_interface.h>
42 #include <vlc_charset.h>
43 #include <vlc_modules.h>
44 #include "../libvlc.h"
45
46 #ifdef __ANDROID__
47 #include <android/log.h>
48 #endif
49
50 struct vlc_logger_t
51 {
52     VLC_COMMON_MEMBERS
53     vlc_rwlock_t lock;
54     vlc_log_cb log;
55     void *sys;
56     module_t *module;
57 };
58
59 static void vlc_vaLogCallback(libvlc_int_t *vlc, int type,
60                               const vlc_log_t *item, const char *format,
61                               va_list ap)
62 {
63     vlc_logger_t *logger = libvlc_priv(vlc)->logger;
64
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);
69 }
70
71 static void vlc_LogCallback(libvlc_int_t *vlc, int type, const vlc_log_t *item,
72                             const char *format, ...)
73 {
74     va_list ap;
75
76     va_start(ap, format);
77     vlc_vaLogCallback(vlc, type, item, format, ap);
78     va_end(ap);
79 }
80
81 #ifdef _WIN32
82 static void Win32DebugOutputMsg (void *, int , const vlc_log_t *,
83                                  const char *, va_list);
84 #endif
85
86 /**
87  * Emit a log message. This function is the variable argument list equivalent
88  * to vlc_Log().
89  */
90 void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
91                 const char *format, va_list args)
92 {
93     if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
94         return;
95
96     /* Get basename from the module filename */
97     char *p = strrchr(module, '/');
98     if (p != NULL)
99         module = p;
100     p = strchr(module, '.');
101
102     size_t modlen = (p != NULL) ? (p - module) : 0;
103     char modulebuf[modlen + 1];
104     if (p != NULL)
105     {
106         memcpy(modulebuf, module, modlen);
107         modulebuf[modlen] = '\0';
108         module = modulebuf;
109     }
110
111     /* Fill message information fields */
112     vlc_log_t msg;
113
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;
118
119     for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
120         if (o->psz_header != NULL)
121         {
122             msg.psz_header = o->psz_header;
123             break;
124         }
125
126 #ifdef _WIN32
127     va_list ap;
128
129     va_copy (ap, args);
130     Win32DebugOutputMsg (NULL, type, &msg, format, ap);
131     va_end (ap);
132 #endif
133
134     /* Pass message to the callback */
135     if (obj != NULL)
136         vlc_vaLogCallback(obj->p_libvlc, type, &msg, format, args);
137 }
138
139 /**
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
146  */
147 void vlc_Log(vlc_object_t *obj, int type, const char *module,
148              const char *format, ... )
149 {
150     va_list ap;
151
152     va_start(ap, format);
153     vlc_vaLog(obj, type, module, format, ap);
154     va_end(ap);
155 }
156
157 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
158 #define COL(x,y)  "\033[" #x ";" #y "m"
159 #define RED     COL(31,1)
160 #define GREEN   COL(32,1)
161 #define YELLOW  COL(0,33)
162 #define WHITE   COL(0,1)
163 #define GRAY    "\033[0m"
164 static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
165
166 /* Display size of a pointer */
167 static const int ptr_width = 2 * /* hex digits */ sizeof(uintptr_t);
168
169 static void PrintColorMsg (void *d, int type, const vlc_log_t *p_item,
170                            const char *format, va_list ap)
171 {
172     FILE *stream = stderr;
173     int verbose = (intptr_t)d;
174
175     if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
176         return;
177
178     int canc = vlc_savecancel ();
179
180     flockfile (stream);
181     utf8_fprintf (stream, "["GREEN"%0*"PRIxPTR""GRAY"] ", ptr_width, p_item->i_object_id);
182     if (p_item->psz_header != NULL)
183         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
184     utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
185                   p_item->psz_object_type, msg_type[type], msg_color[type]);
186     utf8_vfprintf (stream, format, ap);
187     fputs (GRAY"\n", stream);
188 #if defined (_WIN32) || defined (__OS2__)
189     fflush (stream);
190 #endif
191     funlockfile (stream);
192     vlc_restorecancel (canc);
193 }
194
195 static void PrintMsg (void *d, int type, const vlc_log_t *p_item,
196                       const char *format, va_list ap)
197 {
198     FILE *stream = stderr;
199     int verbose = (intptr_t)d;
200
201     if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
202         return;
203
204     int canc = vlc_savecancel ();
205
206     flockfile (stream);
207     utf8_fprintf (stream, "[%0*"PRIxPTR"] ", ptr_width, p_item->i_object_id);
208     if (p_item->psz_header != NULL)
209         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
210     utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
211                   p_item->psz_object_type, msg_type[type]);
212     utf8_vfprintf (stream, format, ap);
213     putc_unlocked ('\n', stream);
214 #if defined (_WIN32) || defined (__OS2__)
215     fflush (stream);
216 #endif
217     funlockfile (stream);
218     vlc_restorecancel (canc);
219 }
220
221 #ifdef _WIN32
222 static void Win32DebugOutputMsg (void* d, int type, const vlc_log_t *p_item,
223                                  const char *format, va_list dol)
224 {
225     VLC_UNUSED(p_item);
226
227     const signed char *pverbose = d;
228     if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
229         return;
230
231     va_list dol2;
232     va_copy (dol2, dol);
233     int msg_len = vsnprintf(NULL, 0, format, dol2);
234     va_end (dol2);
235
236     if(msg_len <= 0)
237         return;
238
239     char *msg = malloc(msg_len + 1 + 1);
240     if (!msg)
241         return;
242
243     msg_len = vsnprintf(msg, msg_len+1, format, dol);
244     if (msg_len > 0){
245         if(msg[msg_len-1] != '\n'){
246             msg[msg_len] = '\n';
247             msg[msg_len + 1] = '\0';
248         }
249         char* psz_msg = NULL;
250         if(asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
251                     p_item->psz_object_type, msg_type[type], msg) > 0) {
252             wchar_t* wmsg = ToWide(psz_msg);
253             OutputDebugStringW(wmsg);
254             free(wmsg);
255             free(psz_msg);
256         }
257     }
258     free(msg);
259 }
260 #endif
261
262 #ifdef __ANDROID__
263 static void AndroidPrintMsg (void *d, int type, const vlc_log_t *p_item,
264                              const char *format, va_list ap)
265 {
266     int verbose = (intptr_t)d;
267     int prio;
268
269     if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
270         return;
271
272     int canc = vlc_savecancel ();
273
274     char *format2;
275     if (asprintf (&format2, "[%0*"PRIxPTR"] %s %s: %s",
276                   ptr_width, p_item->i_object_id, p_item->psz_module,
277                   p_item->psz_object_type, format) < 0)
278         return;
279     switch (type) {
280         case VLC_MSG_INFO:
281             prio = ANDROID_LOG_INFO;
282             break;
283         case VLC_MSG_ERR:
284             prio = ANDROID_LOG_ERROR;
285             break;
286         case VLC_MSG_WARN:
287             prio = ANDROID_LOG_WARN;
288             break;
289         default:
290         case VLC_MSG_DBG:
291             prio = ANDROID_LOG_DEBUG;
292     }
293     __android_log_vprint (prio, "VLC", format2, ap);
294     free (format2);
295     vlc_restorecancel (canc);
296 }
297 #endif
298
299 typedef struct vlc_log_early_t
300 {
301     struct vlc_log_early_t *next;
302     int type;
303     vlc_log_t meta;
304     char *msg;
305 } vlc_log_early_t;
306
307 typedef struct
308 {
309     vlc_mutex_t lock;
310     vlc_log_early_t *head;
311     vlc_log_early_t **tailp;
312 } vlc_logger_early_t;
313
314 static void vlc_vaLogEarly(void *d, int type, const vlc_log_t *item,
315                            const char *format, va_list ap)
316 {
317     vlc_logger_early_t *sys = d;
318
319     vlc_log_early_t *log = malloc(sizeof (*log));
320     if (unlikely(log == NULL))
321         return;
322
323     log->next = NULL;
324     log->type = type;
325     log->meta.i_object_id = item->i_object_id;
326     /* NOTE: Object types MUST be static constant - no need to copy them. */
327     log->meta.psz_object_type = item->psz_object_type;
328     log->meta.psz_module = item->psz_module; /* Ditto. */
329     log->meta.psz_header = item->psz_header ? strdup(item->psz_header) : NULL;
330
331     int canc = vlc_savecancel(); /* XXX: needed for vasprintf() ? */
332     if (vasprintf(&log->msg, format, ap) == -1)
333         log->msg = NULL;
334     vlc_restorecancel(canc);
335
336     vlc_mutex_lock(&sys->lock);
337     assert(sys->tailp != NULL);
338     assert(*(sys->tailp) == NULL);
339     *(sys->tailp) = log;
340     sys->tailp = &log->next;
341     vlc_mutex_unlock(&sys->lock);
342 }
343
344 static int vlc_LogEarlyOpen(vlc_logger_t *logger)
345 {
346     vlc_logger_early_t *sys = malloc(sizeof (*sys));
347
348     if (unlikely(sys == NULL))
349         return -1;
350
351     vlc_mutex_init(&sys->lock);
352     sys->head = NULL;
353     sys->tailp = &sys->head;
354
355     logger->log = vlc_vaLogEarly;
356     logger->sys = sys;
357     return 0;
358 }
359
360 static void vlc_LogEarlyClose(vlc_logger_t *logger, void *d)
361 {
362     libvlc_int_t *vlc = logger->p_libvlc;
363     vlc_logger_early_t *sys = d;
364
365     /* Drain early log messages */
366     for (vlc_log_early_t *log = sys->head, *next; log != NULL; log = next)
367     {
368         vlc_LogCallback(vlc, log->type, &log->meta, "%s",
369                         (log->msg != NULL) ? log->msg : "message lost");
370         free(log->msg);
371         next = log->next;
372         free(log);
373     }
374
375     vlc_mutex_destroy(&sys->lock);
376     free(sys);
377 }
378
379 static void vlc_vaLogDiscard(void *d, int type, const vlc_log_t *item,
380                              const char *format, va_list ap)
381 {
382     (void) d; (void) type; (void) item; (void) format; (void) ap;
383 }
384
385 static int vlc_logger_load(void *func, va_list ap)
386 {
387     vlc_log_cb (*activate)(vlc_object_t *, void **) = func;
388     vlc_logger_t *logger = va_arg(ap, vlc_logger_t *);
389     vlc_log_cb *cb = va_arg(ap, vlc_log_cb *);
390     void **sys = va_arg(ap, void **);
391
392     *cb = activate(VLC_OBJECT(logger), sys);
393     return (*cb != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
394 }
395
396 static void vlc_logger_unload(void *func, va_list ap)
397 {
398     void (*deactivate)(vlc_logger_t *) = func;
399     void *sys = va_arg(ap, void *);
400
401     deactivate(sys);
402 }
403
404 /**
405  * Performs preinitialization of the messages logging subsystem.
406  *
407  * Early log messages will be stored in memory until the subsystem is fully
408  * initialized with vlc_LogInit(). This enables logging before the
409  * configuration and modules bank are ready.
410  *
411  * \return 0 on success, -1 on error.
412  */
413 int vlc_LogPreinit(libvlc_int_t *vlc)
414 {
415     vlc_logger_t *logger = vlc_custom_create(vlc, sizeof (*logger), "logger");
416
417     libvlc_priv(vlc)->logger = logger;
418
419     if (unlikely(logger == NULL))
420         return -1;
421
422     vlc_rwlock_init(&logger->lock);
423
424     if (vlc_LogEarlyOpen(logger))
425     {
426         logger->log = vlc_vaLogDiscard;
427         return -1;
428     }
429
430     /* Announce who we are */
431     msg_Dbg(vlc, "VLC media player - %s", VERSION_MESSAGE);
432     msg_Dbg(vlc, "%s", COPYRIGHT_MESSAGE);
433     msg_Dbg(vlc, "revision %s", psz_vlc_changeset);
434     msg_Dbg(vlc, "configured with %s", CONFIGURE_LINE);
435     return 0;
436 }
437
438 /**
439  * Initializes the messages logging subsystem and drain the early messages to
440  * the configured log.
441  *
442  * \return 0 on success, -1 on error.
443  */
444 int vlc_LogInit(libvlc_int_t *vlc)
445 {
446     vlc_logger_t *logger = libvlc_priv(vlc)->logger;
447     if (unlikely(logger == NULL))
448         return -1;
449
450     vlc_log_cb cb;
451     void *sys, *early_sys = NULL;
452
453     /* TODO: module configuration item */
454     module_t *module = vlc_module_load(logger, "logger", NULL, false,
455                                        vlc_logger_load, logger, &cb, &sys);
456     if (module == NULL)
457     {
458 #ifdef __ANDROID__
459         cb = AndroidPrintMsg;
460 #elif defined (HAVE_ISATTY) && !defined (_WIN32)
461         if (isatty(STDERR_FILENO) && var_InheritBool(vlc, "color"))
462             cb = PrintColorMsg;
463 #endif
464         else
465             cb = PrintMsg;
466
467         signed char verbosity;
468
469         if (var_InheritBool(vlc, "quiet"))
470             verbosity = -1;
471         else
472         {
473             const char *str = getenv("VLC_VERBOSE");
474
475             if (str == NULL || sscanf(str, "%hhd", &verbosity) < 1)
476                 verbosity = var_InheritInteger(vlc, "verbose");
477         }
478         sys = (void *)(intptr_t)verbosity;
479     }
480
481     vlc_rwlock_wrlock(&logger->lock);
482     if (logger->log == vlc_vaLogEarly)
483         early_sys = logger->sys;
484
485     logger->log = cb;
486     logger->sys = sys;
487     assert(logger->module == NULL); /* Only one call to vlc_LogInit()! */
488     logger->module = module;
489     vlc_rwlock_unlock(&logger->lock);
490
491     if (early_sys != NULL)
492         vlc_LogEarlyClose(logger, early_sys);
493
494     return 0;
495 }
496
497 /**
498  * Sets the message logging callback.
499  * \param cb message callback, or NULL to clear
500  * \param data data pointer for the message callback
501  */
502 void vlc_LogSet(libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
503 {
504     vlc_logger_t *logger = libvlc_priv(vlc)->logger;
505
506     if (unlikely(logger == NULL))
507         return;
508
509     module_t *module;
510     void *sys;
511
512     if (cb == NULL)
513         cb = vlc_vaLogDiscard;
514
515     vlc_rwlock_wrlock(&logger->lock);
516     sys = logger->sys;
517     module = logger->module;
518
519     logger->log = cb;
520     logger->sys = opaque;
521     logger->module = NULL;
522     vlc_rwlock_unlock(&logger->lock);
523
524     if (module != NULL)
525         vlc_module_unload(module, vlc_logger_unload, sys);
526
527     /* Announce who we are */
528     msg_Dbg (vlc, "VLC media player - %s", VERSION_MESSAGE);
529     msg_Dbg (vlc, "%s", COPYRIGHT_MESSAGE);
530     msg_Dbg (vlc, "revision %s", psz_vlc_changeset);
531     msg_Dbg (vlc, "configured with %s", CONFIGURE_LINE);
532 }
533
534 void vlc_LogDeinit(libvlc_int_t *vlc)
535 {
536     vlc_logger_t *logger = libvlc_priv(vlc)->logger;
537
538     if (unlikely(logger == NULL))
539         return;
540
541     if (logger->module != NULL)
542         vlc_module_unload(logger->module, vlc_logger_unload, logger->sys);
543     else
544     /* Flush early log messages (corner case: no call to vlc_LogInit()) */
545     if (logger->log == vlc_vaLogEarly)
546     {
547         logger->log = vlc_vaLogDiscard;
548         vlc_LogEarlyClose(logger, logger->sys);
549     }
550
551     vlc_rwlock_destroy(&logger->lock);
552     vlc_object_release(logger);
553     libvlc_priv(vlc)->logger = NULL;
554 }