messages.c 12.2 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
 *
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
struct vlc_logger_t
{
48
    VLC_COMMON_MEMBERS
49 50 51
    vlc_rwlock_t lock;
    vlc_log_cb log;
    void *sys;
52
    module_t *module;
53 54 55 56 57
};

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
58
{
59
    vlc_logger_t *logger = libvlc_priv(vlc)->logger;
60
    int canc;
61 62

    assert(logger != NULL);
63
    canc = vlc_savecancel();
64 65 66
    vlc_rwlock_rdlock(&logger->lock);
    logger->log(logger->sys, type, item, format, ap);
    vlc_rwlock_unlock(&logger->lock);
67
    vlc_restorecancel(canc);
68 69 70 71 72 73
}

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
74

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

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

Sigmund Augdal Helberg's avatar
all:  
Sigmund Augdal Helberg committed
85
/**
86 87
 * 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
88
 */
89
void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
90
                const char *file, unsigned line, const char *func,
91
                const char *format, va_list args)
Michel Kaempf's avatar
Michel Kaempf committed
92
{
93
    if (obj != NULL && obj->obj.flags & OBJECT_FLAGS_QUIET)
94
        return;
95

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

102
    size_t modlen = (p != NULL) ? (p - module) : 0;
103
    char modulebuf[modlen + 1];
104 105 106 107 108 109 110
    if (p != NULL)
    {
        memcpy(modulebuf, module, modlen);
        modulebuf[modlen] = '\0';
        module = modulebuf;
    }

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->obj.object_type : "generic";
116
    msg.psz_module = module;
117
    msg.psz_header = NULL;
118 119 120
    msg.file = file;
    msg.line = line;
    msg.func = func;
121
    msg.tid = vlc_thread_id();
122

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

130
#ifdef _WIN32
131 132
    va_list ap;

133
    va_copy (ap, args);
134
    Win32DebugOutputMsg (NULL, type, &msg, format, ap);
135
    va_end (ap);
136 137
#endif

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

/**
 * 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
    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);

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
182
    if (msg_len <= 0)
183 184 185 186 187 188 189 190
        return;

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

    msg_len = vsnprintf(msg, msg_len+1, format, dol);
    if (msg_len > 0){
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
191
        if (msg[msg_len-1] != '\n') {
192 193 194
            msg[msg_len] = '\n';
            msg[msg_len + 1] = '\0';
        }
195
        char* psz_msg = NULL;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
196
        if (asprintf(&psz_msg, "%s %s%s: %s", p_item->psz_module,
197 198 199 200 201 202
                    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
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;
239 240 241
    log->meta.file = item->file;
    log->meta.line = item->line;
    log->meta.func = item->func;
242 243 244 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

    if (vasprintf(&log->msg, format, ap) == -1)
        log->msg = NULL;

    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;
}

270
static void vlc_LogEarlyClose(vlc_logger_t *logger, void *d)
271
{
272
    libvlc_int_t *vlc = logger->obj.libvlc;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    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;
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
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);
}

314
/**
315 316 317 318 319 320 321
 * 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.
322
 */
323
int vlc_LogPreinit(libvlc_int_t *vlc)
324
{
325
    vlc_logger_t *logger = vlc_custom_create(vlc, sizeof (*logger), "logger");
326

327 328 329 330 331 332 333 334
    libvlc_priv(vlc)->logger = logger;

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

    vlc_rwlock_init(&logger->lock);

    if (vlc_LogEarlyOpen(logger))
335
    {
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        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;

360 361 362 363 364 365 366
    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)
367
        cb = vlc_vaLogDiscard;
368

369 370 371 372 373
    vlc_rwlock_wrlock(&logger->lock);
    if (logger->log == vlc_vaLogEarly)
        early_sys = logger->sys;

    logger->log = cb;
374 375 376
    logger->sys = sys;
    assert(logger->module == NULL); /* Only one call to vlc_LogInit()! */
    logger->module = module;
377 378 379
    vlc_rwlock_unlock(&logger->lock);

    if (early_sys != NULL)
380
        vlc_LogEarlyClose(logger, early_sys);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

    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;

397 398 399
    module_t *module;
    void *sys;

400 401 402 403
    if (cb == NULL)
        cb = vlc_vaLogDiscard;

    vlc_rwlock_wrlock(&logger->lock);
404 405 406
    sys = logger->sys;
    module = logger->module;

407 408
    logger->log = cb;
    logger->sys = opaque;
409
    logger->module = NULL;
410
    vlc_rwlock_unlock(&logger->lock);
411

412
    if (module != NULL)
413
        vlc_module_unload(vlc, module, vlc_logger_unload, sys);
414

415 416 417 418 419
    /* 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);
420 421
}

422
void vlc_LogDeinit(libvlc_int_t *vlc)
423
{
424
    vlc_logger_t *logger = libvlc_priv(vlc)->logger;
425

426 427
    if (unlikely(logger == NULL))
        return;
428

429
    if (logger->module != NULL)
430
        vlc_module_unload(vlc, logger->module, vlc_logger_unload, logger->sys);
431
    else
432 433 434 435
    /* Flush early log messages (corner case: no call to vlc_LogInit()) */
    if (logger->log == vlc_vaLogEarly)
    {
        logger->log = vlc_vaLogDiscard;
436
        vlc_LogEarlyClose(logger, logger->sys);
437
    }
438

439
    vlc_rwlock_destroy(&logger->lock);
440
    vlc_object_release(logger);
441
    libvlc_priv(vlc)->logger = NULL;
442
}