]> code.delx.au - pulseaudio/blob - src/pulsecore/core-error.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / core-error.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/native-common.h>
39 #include <pulsecore/thread.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/log.h>
42
43 #include "core-error.h"
44
45 PA_STATIC_TLS_DECLARE(cstrerror, pa_xfree);
46
47 const char* pa_cstrerror(int errnum) {
48 const char *original = NULL;
49 char *translated, *t;
50 char errbuf[128];
51
52 if ((t = PA_STATIC_TLS_GET(cstrerror)))
53 pa_xfree(t);
54
55 #if defined(HAVE_STRERROR_R) && defined(__GLIBC__)
56 original = strerror_r(errnum, errbuf, sizeof(errbuf));
57 #elif defined(HAVE_STRERROR_R)
58 if (strerror_r(errnum, errbuf, sizeof(errbuf)) == 0) {
59 errbuf[sizeof(errbuf) - 1] = 0;
60 original = errbuf;
61 }
62 #else
63 /* This might not be thread safe, but we hope for the best */
64 original = strerror(errnum);
65 #endif
66
67 if (!original) {
68 pa_snprintf(errbuf, sizeof(errbuf), "Unknown error %i", errnum);
69 original = errbuf;
70 }
71
72 if (!(translated = pa_locale_to_utf8(original))) {
73 pa_log_warn("Unable to convert error string to locale, filtering.");
74 translated = pa_utf8_filter(original);
75 }
76
77 PA_STATIC_TLS_SET(cstrerror, translated);
78
79 return translated;
80 }