cleanup specfile for packaging
[profile/ivi/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 /**
64  * \brief The default error handler function.
65  * \param file The filename where the error was hit.
66  * \param line The line number.
67  * \param function The function name.
68  * \param err The error code.
69  * \param fmt The message (including the format characters).
70  * \param ... Optional arguments.
71  *
72  * Prints the error message including location to \c stderr.
73  */
74 static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
75 {
76         va_list arg;
77         va_start(arg, fmt);
78         fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
79         vfprintf(stderr, fmt, arg);
80         if (err)
81                 fprintf(stderr, ": %s", snd_strerror(err));
82         putc('\n', stderr);
83         va_end(arg);
84 }
85
86 /**
87  * \ingroup Error
88  * Pointer to the error handler function.
89  * For internal use only.
90  */
91 snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
92
93 /**
94  * \brief Sets the error handler.
95  * \param handler The pointer to the new error handler function.
96  *
97  * This function sets a new error handler, or (if \c handler is \c NULL)
98  * the default one which prints the error messages to \c stderr.
99  */
100 int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
101 {
102         snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
103 #ifndef NDEBUG
104         if (snd_lib_error != snd_lib_error_default)
105                 snd_err_msg = snd_lib_error;
106 #endif
107         return 0;
108 }
109
110 /**
111  * \brief Returns the ALSA sound library version in ASCII format
112  * \return The ASCII description of the used ALSA sound library.
113  */
114 const char *snd_asoundlib_version(void)
115 {
116         return SND_LIB_VERSION_STR;
117 }
118
119 #ifndef NDEBUG
120 /*
121  * internal error handling
122  */
123 static void snd_err_msg_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
124 {
125         va_list arg;
126         const char *verbose;
127         
128         verbose = getenv("LIBASOUND_DEBUG");
129         if (! verbose || ! *verbose)
130                 return;
131         va_start(arg, fmt);
132         fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
133         vfprintf(stderr, fmt, arg);
134         if (err)
135                 fprintf(stderr, ": %s", snd_strerror(err));
136         putc('\n', stderr);
137         va_end(arg);
138 #ifdef ALSA_DEBUG_ASSERT
139         verbose = getenv("LIBASOUND_DEBUG_ASSERT");
140         if (verbose && *verbose)
141                 assert(0);
142 #endif
143 }
144
145 /**
146  * The ALSA error message handler
147  */
148 snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
149
150 #endif