messages.c 13.3 KB
Newer Older
1
/*****************************************************************************
2 3
 * messages.c: messages interface
 * 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
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
6
 * Copyright (C) 1998-2005 VLC authors and VideoLAN
7
 * $Id$
8
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
9
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10
 *          Samuel Hocevar <sam@zoy.org>
11
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15
 * (at your option) any later version.
16
 *
17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

27
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
28
 * Preamble
29
 *****************************************************************************/
30

31 32 33 34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <stdlib.h>
36
#include <stdarg.h>                                       /* va_list for BSD */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
37
#include <unistd.h>
38
#include <assert.h>
39

40 41
#include <vlc_common.h>
#include <vlc_interface.h>
Clément Stenac's avatar
Clément Stenac committed
42
#include <vlc_charset.h>
43
#include <vlc_modules.h>
44
#include "../libvlc.h"
45

46 47 48 49
#ifdef __ANDROID__
#include <android/log.h>
#endif

50 51
struct vlc_logger_t
{
52
    VLC_COMMON_MEMBERS
53 54 55
    vlc_rwlock_t lock;
    vlc_log_cb log;
    void *sys;
56
    module_t *module;
57 58 59 60 61
};

static void vlc_vaLogCallback(libvlc_int_t *vlc, int type,
                              const vlc_log_t *item, const char *format,
                              va_list ap)
Michel Kaempf's avatar
Michel Kaempf committed
62
{
63 64 65 66 67 68 69 70 71 72 73 74
    vlc_logger_t *logger = libvlc_priv(vlc)->logger;

    assert(logger != NULL);
    vlc_rwlock_rdlock(&logger->lock);
    logger->log(logger->sys, type, item, format, ap);
    vlc_rwlock_unlock(&logger->lock);
}

static void vlc_LogCallback(libvlc_int_t *vlc, int type, const vlc_log_t *item,
                            const char *format, ...)
{
    va_list ap;
Michel Kaempf's avatar
Michel Kaempf committed
75

76 77 78
    va_start(ap, format);
    vlc_vaLogCallback(vlc, type, item, format, ap);
    va_end(ap);
Michel Kaempf's avatar
Michel Kaempf committed
79
}
80

81
#ifdef _WIN32
82
static void Win32DebugOutputMsg (void *, int , const vlc_log_t *,
83 84
                                 const char *, va_list);
#endif
85

Sigmund Augdal Helberg's avatar
all:  
Sigmund Augdal Helberg committed
86
/**
87 88
 * Emit a log message. This function is the variable argument list equivalent
 * to vlc_Log().
Sigmund Augdal Helberg's avatar
all:  
Sigmund Augdal Helberg committed
89
 */
90
void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
91
                const char *file, unsigned line, const char *func,
92
                const char *format, va_list args)
Michel Kaempf's avatar
Michel Kaempf committed
93
{
94
    if (obj != NULL && obj->i_flags & OBJECT_FLAGS_QUIET)
95
        return;
96

97 98 99 100 101 102
    /* Get basename from the module filename */
    char *p = strrchr(module, '/');
    if (p != NULL)
        module = p;
    p = strchr(module, '.');

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
103
    size_t modlen = (p != NULL) ? (p - module) : 0;
104
    char modulebuf[modlen + 1];
105 106 107 108 109 110 111
    if (p != NULL)
    {
        memcpy(modulebuf, module, modlen);
        modulebuf[modlen] = '\0';
        module = modulebuf;
    }

112
    /* Fill message information fields */
113
    vlc_log_t msg;
114

115
    msg.i_object_id = (uintptr_t)obj;
116
    msg.psz_object_type = (obj != NULL) ? obj->psz_object_type : "generic";
117
    msg.psz_module = module;
118
    msg.psz_header = NULL;
119 120 121
    msg.file = file;
    msg.line = line;
    msg.func = func;
122

123
    for (vlc_object_t *o = obj; o != NULL; o = o->p_parent)
124
        if (o->psz_header != NULL)
125
        {
126
            msg.psz_header = o->psz_header;
127
            break;
128
        }
129

130
#ifdef _WIN32
131 132
    va_list ap;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
133
    va_copy (ap, args);
134
    Win32DebugOutputMsg (NULL, type, &msg, format, ap);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
135
    va_end (ap);
136 137
#endif

138 139 140 141 142 143 144 145 146 147 148
    /* Pass message to the callback */
    if (obj != NULL)
        vlc_vaLogCallback(obj->p_libvlc, type, &msg, format, args);
}

/**
 * Emit a log message.
 * \param obj VLC object emitting the message or NULL
 * \param type VLC_MSG_* message type (info, error, warning or debug)
 * \param module name of module from which the message come
 *               (normally MODULE_STRING)
149 150 151
 * \param file source module file name (normally __FILE__) or NULL
 * \param line function call source line number (normally __LINE__) or 0
 * \param func calling function name (normally __func__) or NULL
152 153 154
 * \param format printf-like message format
 */
void vlc_Log(vlc_object_t *obj, int type, const char *module,
155
             const char *file, unsigned line, const char *func,
156 157 158 159 160
             const char *format, ... )
{
    va_list ap;

    va_start(ap, format);
161
    vlc_vaLog(obj, type, module, file, line, func, format, ap);
162
    va_end(ap);
Michel Kaempf's avatar
Michel Kaempf committed
163 164
}

165
#ifdef _WIN32
166 167
static const char msg_type[4][9] = { "", " error", " warning", " debug" };

168
static void Win32DebugOutputMsg (void* d, int type, const vlc_log_t *p_item,
169 170
                                 const char *format, va_list dol)
{
171 172
    VLC_UNUSED(p_item);

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    const signed char *pverbose = d;
    if (pverbose && (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR)))
        return;

    va_list dol2;
    va_copy (dol2, dol);
    int msg_len = vsnprintf(NULL, 0, format, dol2);
    va_end (dol2);

    if(msg_len <= 0)
        return;

    char *msg = malloc(msg_len + 1 + 1);
    if (!msg)
        return;

    msg_len = vsnprintf(msg, msg_len+1, format, dol);
    if (msg_len > 0){
        if(msg[msg_len-1] != '\n'){
            msg[msg_len] = '\n';
            msg[msg_len + 1] = '\0';
        }
195 196 197 198 199 200 201 202
        char* psz_msg = NULL;
        if(asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
                    p_item->psz_object_type, msg_type[type], msg) > 0) {
            wchar_t* wmsg = ToWide(psz_msg);
            OutputDebugStringW(wmsg);
            free(wmsg);
            free(psz_msg);
        }
203 204 205 206
    }
    free(msg);
}
#endif
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
#ifdef __ANDROID__
static void AndroidPrintMsg (void *d, int type, const vlc_log_t *p_item,
                             const char *format, va_list ap)
{
    int verbose = (intptr_t)d;
    int prio;

    if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
        return;

    int canc = vlc_savecancel ();

    char *format2;
    if (asprintf (&format2, "[%0*"PRIxPTR"] %s %s: %s",
                  ptr_width, p_item->i_object_id, p_item->psz_module,
                  p_item->psz_object_type, format) < 0)
        return;
    switch (type) {
        case VLC_MSG_INFO:
            prio = ANDROID_LOG_INFO;
            break;
        case VLC_MSG_ERR:
            prio = ANDROID_LOG_ERROR;
            break;
        case VLC_MSG_WARN:
            prio = ANDROID_LOG_WARN;
            break;
        default:
        case VLC_MSG_DBG:
            prio = ANDROID_LOG_DEBUG;
    }
    __android_log_vprint (prio, "VLC", format2, ap);
    free (format2);
    vlc_restorecancel (canc);
}
#endif

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
typedef struct vlc_log_early_t
{
    struct vlc_log_early_t *next;
    int type;
    vlc_log_t meta;
    char *msg;
} vlc_log_early_t;

typedef struct
{
    vlc_mutex_t lock;
    vlc_log_early_t *head;
    vlc_log_early_t **tailp;
} vlc_logger_early_t;

static void vlc_vaLogEarly(void *d, int type, const vlc_log_t *item,
                           const char *format, va_list ap)
{
    vlc_logger_early_t *sys = d;

    vlc_log_early_t *log = malloc(sizeof (*log));
    if (unlikely(log == NULL))
        return;

    log->next = NULL;
    log->type = type;
    log->meta.i_object_id = item->i_object_id;
    /* NOTE: Object types MUST be static constant - no need to copy them. */
    log->meta.psz_object_type = item->psz_object_type;
    log->meta.psz_module = item->psz_module; /* Ditto. */
    log->meta.psz_header = item->psz_header ? strdup(item->psz_header) : NULL;
276 277 278
    log->meta.file = item->file;
    log->meta.line = item->line;
    log->meta.func = item->func;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

    int canc = vlc_savecancel(); /* XXX: needed for vasprintf() ? */
    if (vasprintf(&log->msg, format, ap) == -1)
        log->msg = NULL;
    vlc_restorecancel(canc);

    vlc_mutex_lock(&sys->lock);
    assert(sys->tailp != NULL);
    assert(*(sys->tailp) == NULL);
    *(sys->tailp) = log;
    sys->tailp = &log->next;
    vlc_mutex_unlock(&sys->lock);
}

static int vlc_LogEarlyOpen(vlc_logger_t *logger)
{
    vlc_logger_early_t *sys = malloc(sizeof (*sys));

    if (unlikely(sys == NULL))
        return -1;

    vlc_mutex_init(&sys->lock);
    sys->head = NULL;
    sys->tailp = &sys->head;

    logger->log = vlc_vaLogEarly;
    logger->sys = sys;
    return 0;
}

309
static void vlc_LogEarlyClose(vlc_logger_t *logger, void *d)
310
{
311
    libvlc_int_t *vlc = logger->p_libvlc;
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    vlc_logger_early_t *sys = d;

    /* Drain early log messages */
    for (vlc_log_early_t *log = sys->head, *next; log != NULL; log = next)
    {
        vlc_LogCallback(vlc, log->type, &log->meta, "%s",
                        (log->msg != NULL) ? log->msg : "message lost");
        free(log->msg);
        next = log->next;
        free(log);
    }

    vlc_mutex_destroy(&sys->lock);
    free(sys);
}

static void vlc_vaLogDiscard(void *d, int type, const vlc_log_t *item,
                             const char *format, va_list ap)
{
    (void) d; (void) type; (void) item; (void) format; (void) ap;
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
static int vlc_logger_load(void *func, va_list ap)
{
    vlc_log_cb (*activate)(vlc_object_t *, void **) = func;
    vlc_logger_t *logger = va_arg(ap, vlc_logger_t *);
    vlc_log_cb *cb = va_arg(ap, vlc_log_cb *);
    void **sys = va_arg(ap, void **);

    *cb = activate(VLC_OBJECT(logger), sys);
    return (*cb != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
}

static void vlc_logger_unload(void *func, va_list ap)
{
    void (*deactivate)(vlc_logger_t *) = func;
    void *sys = va_arg(ap, void *);

    deactivate(sys);
}

353
/**
354 355 356 357 358 359 360
 * Performs preinitialization of the messages logging subsystem.
 *
 * Early log messages will be stored in memory until the subsystem is fully
 * initialized with vlc_LogInit(). This enables logging before the
 * configuration and modules bank are ready.
 *
 * \return 0 on success, -1 on error.
361
 */
362
int vlc_LogPreinit(libvlc_int_t *vlc)
363
{
364
    vlc_logger_t *logger = vlc_custom_create(vlc, sizeof (*logger), "logger");
365

366 367 368 369 370 371 372 373
    libvlc_priv(vlc)->logger = logger;

    if (unlikely(logger == NULL))
        return -1;

    vlc_rwlock_init(&logger->lock);

    if (vlc_LogEarlyOpen(logger))
374
    {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        logger->log = vlc_vaLogDiscard;
        return -1;
    }

    /* Announce who we are */
    msg_Dbg(vlc, "VLC media player - %s", VERSION_MESSAGE);
    msg_Dbg(vlc, "%s", COPYRIGHT_MESSAGE);
    msg_Dbg(vlc, "revision %s", psz_vlc_changeset);
    msg_Dbg(vlc, "configured with %s", CONFIGURE_LINE);
    return 0;
}

/**
 * Initializes the messages logging subsystem and drain the early messages to
 * the configured log.
 *
 * \return 0 on success, -1 on error.
 */
int vlc_LogInit(libvlc_int_t *vlc)
{
    vlc_logger_t *logger = libvlc_priv(vlc)->logger;
    if (unlikely(logger == NULL))
        return -1;

399 400 401 402 403 404 405
    vlc_log_cb cb;
    void *sys, *early_sys = NULL;

    /* TODO: module configuration item */
    module_t *module = vlc_module_load(logger, "logger", NULL, false,
                                       vlc_logger_load, logger, &cb, &sys);
    if (module == NULL)
406
#ifdef __ANDROID__
407 408 409
        cb = AndroidPrintMsg, sys = NULL;
#else
        cb = vlc_vaLogDiscard;
410
#endif
411

412 413 414 415 416
    vlc_rwlock_wrlock(&logger->lock);
    if (logger->log == vlc_vaLogEarly)
        early_sys = logger->sys;

    logger->log = cb;
417 418 419
    logger->sys = sys;
    assert(logger->module == NULL); /* Only one call to vlc_LogInit()! */
    logger->module = module;
420 421 422
    vlc_rwlock_unlock(&logger->lock);

    if (early_sys != NULL)
423
        vlc_LogEarlyClose(logger, early_sys);
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

    return 0;
}

/**
 * Sets the message logging callback.
 * \param cb message callback, or NULL to clear
 * \param data data pointer for the message callback
 */
void vlc_LogSet(libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
{
    vlc_logger_t *logger = libvlc_priv(vlc)->logger;

    if (unlikely(logger == NULL))
        return;

440 441 442
    module_t *module;
    void *sys;

443 444 445 446
    if (cb == NULL)
        cb = vlc_vaLogDiscard;

    vlc_rwlock_wrlock(&logger->lock);
447 448 449
    sys = logger->sys;
    module = logger->module;

450 451
    logger->log = cb;
    logger->sys = opaque;
452
    logger->module = NULL;
453
    vlc_rwlock_unlock(&logger->lock);
454

455 456 457
    if (module != NULL)
        vlc_module_unload(module, vlc_logger_unload, sys);

458 459 460 461 462
    /* Announce who we are */
    msg_Dbg (vlc, "VLC media player - %s", VERSION_MESSAGE);
    msg_Dbg (vlc, "%s", COPYRIGHT_MESSAGE);
    msg_Dbg (vlc, "revision %s", psz_vlc_changeset);
    msg_Dbg (vlc, "configured with %s", CONFIGURE_LINE);
463 464
}

465
void vlc_LogDeinit(libvlc_int_t *vlc)
466
{
467
    vlc_logger_t *logger = libvlc_priv(vlc)->logger;
468

469 470
    if (unlikely(logger == NULL))
        return;
471

472 473 474
    if (logger->module != NULL)
        vlc_module_unload(logger->module, vlc_logger_unload, logger->sys);
    else
475 476 477 478
    /* Flush early log messages (corner case: no call to vlc_LogInit()) */
    if (logger->log == vlc_vaLogEarly)
    {
        logger->log = vlc_vaLogDiscard;
479
        vlc_LogEarlyClose(logger, logger->sys);
480
    }
481

482
    vlc_rwlock_destroy(&logger->lock);
483
    vlc_object_release(logger);
484
    libvlc_priv(vlc)->logger = NULL;
485
}