Imported Upstream version 1.0.28
[platform/upstream/alsa-lib.git] / src / error.c
1 /**
2  * \file error.c
3  * \brief Error code handling routines
4  * \author Jaroslav Kysela <perex@perex.cz>
5  * \date 1998-2001
6  *
7  * Error code handling routines.
8  */
9 /*
10  *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
11  *
12  *  snd_strerror routine needs to be recoded for the locale support
13  *
14  *
15  *   This library is free software; you can redistribute it and/or modify
16  *   it under the terms of the GNU Lesser General Public License as
17  *   published by the Free Software Foundation; either version 2.1 of
18  *   the License, or (at your option) any later version.
19  *
20  *   This program is distributed in the hope that it will be useful,
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   GNU Lesser General Public License for more details.
24  *
25  *   You should have received a copy of the GNU Lesser General Public
26  *   License along with this library; if not, write to the Free Software
27  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  *
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include "local.h"
36
37 /**
38  * Array of error codes in US ASCII.
39  */
40 static const char *snd_error_codes[] =
41 {
42         "Sound protocol is not compatible"
43 };
44
45 /**
46  * \brief Returns the message for an error code.
47  * \param errnum The error code number, which must be a system error code
48  *               or an ALSA error code.
49  * \return The ASCII description of the given numeric error code.
50  */
51 const char *snd_strerror(int errnum)
52 {
53         if (errnum < 0)
54                 errnum = -errnum;
55         if (errnum < SND_ERROR_BEGIN)
56                 return (const char *) strerror(errnum);
57         errnum -= SND_ERROR_BEGIN;
58         if ((unsigned int) errnum >= sizeof(snd_error_codes) / sizeof(const char *))
59                  return "Unknown error";
60         return snd_error_codes[errnum];
61 }
62
63 #ifndef DOC_HIDDEN
64 #ifdef HAVE___THREAD
65 #define TLS_PFX         __thread
66 #else
67 #define TLS_PFX         /* NOP */
68 #endif
69 #endif
70
71 static TLS_PFX snd_local_error_handler_t local_error = NULL;
72
73 /**
74  * \brief Install local error handler
75  * \param func The local error handler function
76  * \retval Previous local error handler function
77  */
78 snd_local_error_handler_t snd_lib_error_set_local(snd_local_error_handler_t func)
79 {
80         snd_local_error_handler_t old = local_error;
81         local_error = func;
82         return old;
83 }
84
85 /**
86  * \brief The default error handler function.
87  * \param file The filename where the error was hit.
88  * \param line The line number.
89  * \param function The function name.
90  * \param err The error code.
91  * \param fmt The message (including the format characters).
92  * \param ... Optional arguments.
93  *
94  * If a local error function has been installed for the current thread by
95  * \ref snd_lib_error_set_local, it is called. Otherwise, prints the error
96  * message including location to \c stderr.
97  */
98 static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
99 {
100         va_list arg;
101         va_start(arg, fmt);
102         if (local_error) {
103                 local_error(file, line, function, err, fmt, arg);
104                 va_end(arg);
105                 return;
106         }
107         fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
108         vfprintf(stderr, fmt, arg);
109         if (err)
110                 fprintf(stderr, ": %s", snd_strerror(err));
111         putc('\n', stderr);
112         va_end(arg);
113 }
114
115 /**
116  * \ingroup Error
117  * Pointer to the error handler function.
118  * For internal use only.
119  */
120 snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
121
122 /**
123  * \brief Sets the error handler.
124  * \param handler The pointer to the new error handler function.
125  *
126  * This function sets a new error handler, or (if \c handler is \c NULL)
127  * the default one which prints the error messages to \c stderr.
128  */
129 int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
130 {
131         snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
132 #ifndef NDEBUG
133         if (snd_lib_error != snd_lib_error_default)
134                 snd_err_msg = snd_lib_error;
135 #endif
136         return 0;
137 }
138
139 /**
140  * \brief Returns the ALSA sound library version in ASCII format
141  * \return The ASCII description of the used ALSA sound library.
142  */
143 const char *snd_asoundlib_version(void)
144 {
145         return SND_LIB_VERSION_STR;
146 }
147
148 #ifndef NDEBUG
149 /*
150  * internal error handling
151  */
152 static void snd_err_msg_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
153 {
154         va_list arg;
155         const char *verbose;
156         
157         verbose = getenv("LIBASOUND_DEBUG");
158         if (! verbose || ! *verbose)
159                 return;
160         va_start(arg, fmt);
161         fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
162         vfprintf(stderr, fmt, arg);
163         if (err)
164                 fprintf(stderr, ": %s", snd_strerror(err));
165         putc('\n', stderr);
166         va_end(arg);
167 #ifdef ALSA_DEBUG_ASSERT
168         verbose = getenv("LIBASOUND_DEBUG_ASSERT");
169         if (verbose && *verbose)
170                 assert(0);
171 #endif
172 }
173
174 /**
175  * The ALSA error message handler
176  */
177 snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
178
179 #endif