error.c 3.22 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * error.c: Error handling for libvlc
 *****************************************************************************
 * Copyright (C) 2009 Rémi Denis-Courmont
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
6 7 8
 * 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
9 10 11 12
 * (at your option) any later version.
 *
 * 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
Jean-Baptiste Kempf committed
13 14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
15
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17 18
 * 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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *****************************************************************************/

#include "libvlc_internal.h"

#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <vlc/libvlc.h>


static const char oom[] = "Out of memory";
/* TODO: use only one thread-specific key for whole libvlc */
static vlc_threadvar_t context;

33 34
static vlc_mutex_t lock = VLC_STATIC_MUTEX;
static uintptr_t refs = 0;
35

36 37
void libvlc_threads_init (void)
{
38
    vlc_mutex_lock (&lock);
39 40
    if (refs++ == 0)
        vlc_threadvar_create (&context, free);
41 42 43
    vlc_mutex_unlock (&lock);
}

44
void libvlc_threads_deinit (void)
45
{
46 47 48 49 50
    vlc_mutex_lock (&lock);
    assert (refs > 0);
    if (--refs == 0)
        vlc_threadvar_delete (&context);
    vlc_mutex_unlock (&lock);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
}

static char *get_error (void)
{
    return vlc_threadvar_get (context);
}

static void free_error (void)
{
    char *msg = get_error ();
    if (msg != oom)
        free (msg);
}

/**
 * Gets a human-readable error message for the last LibVLC error in the calling
 * thread. The resulting string is valid until another error occurs (at least
 * until the next LibVLC call).
 *
 * @return NULL if there was no error, a nul-terminated string otherwise.
 */
const char *libvlc_errmsg (void)
{
    return get_error ();
}

/**
 * Clears the LibVLC error status for the current thread. This is optional.
Rémi Duraffort's avatar
Rémi Duraffort committed
79
 * By default, the error status is automatically overridden when a new error
80 81 82 83 84 85 86 87 88 89
 * occurs, and destroyed when the thread exits.
 */
void libvlc_clearerr (void)
{
    free_error ();
    vlc_threadvar_set (context, NULL);
}

/**
 * Sets the LibVLC error status and message for the current thread.
Rémi Duraffort's avatar
Rémi Duraffort committed
90
 * Any previous error is overridden.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
 * @return a nul terminated string (always)
 */
const char *libvlc_vprinterr (const char *fmt, va_list ap)
{
    char *msg;

    assert (fmt != NULL);
    if (vasprintf (&msg, fmt, ap) == -1)
        msg = (char *)oom;

    free_error ();
    vlc_threadvar_set (context, msg);
    return msg;
}

/**
 * Sets the LibVLC error status and message for the current thread.
Rémi Duraffort's avatar
Rémi Duraffort committed
108
 * Any previous error is overridden.
109 110 111 112 113 114 115 116 117 118 119 120 121
 * @return a nul terminated string (always)
 */
const char *libvlc_printerr (const char *fmt, ...)
{
    va_list ap;
    const char *msg;

    va_start (ap, fmt);
    msg = libvlc_vprinterr (fmt, ap);
    va_end (ap);
    return msg;
}