File: error.c

package info (click to toggle)
vlc 3.0.22-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 208,324 kB
  • sloc: ansic: 443,399; cpp: 111,127; objc: 36,399; sh: 6,737; makefile: 6,623; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (128 lines) | stat: -rw-r--r-- 3,377 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*****************************************************************************
 * error.c: Error handling for libvlc
 *****************************************************************************
 * Copyright (C) 2009 Rémi Denis-Courmont
 *
 * 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
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/

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

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

static void free_msg (void *msg)
{
    if (msg != oom)
        free (msg);
}

static void free_error (void)
{
    free_msg (get_error ());
}

static vlc_mutex_t lock = VLC_STATIC_MUTEX;
static uintptr_t refs = 0;

void libvlc_threads_init (void)
{
    vlc_mutex_lock (&lock);
    if (refs++ == 0)
        vlc_threadvar_create (&context, free_msg);
    vlc_mutex_unlock (&lock);
}

void libvlc_threads_deinit (void)
{
    vlc_mutex_lock (&lock);
    assert (refs > 0);
    if (--refs == 0)
    {
        free_error ();
        vlc_threadvar_delete (&context);
    }
    vlc_mutex_unlock (&lock);
}

/**
 * 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.
 * By default, the error status is automatically overridden when a new error
 * 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.
 * Any previous error is overridden.
 * @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.
 * Any previous error is overridden.
 * @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;
}