3 * \brief Error code handling routines
4 * \author Jaroslav Kysela <perex@perex.cz>
7 * Error code handling routines.
10 * Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
12 * snd_strerror routine needs to be recoded for the locale support
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.
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.
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
38 * Array of error codes in US ASCII.
40 static const char *snd_error_codes[] =
42 "Sound protocol is not compatible"
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.
51 const char *snd_strerror(int 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];
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.
72 * Prints the error message including location to \c stderr.
74 static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
78 fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
79 vfprintf(stderr, fmt, arg);
81 fprintf(stderr, ": %s", snd_strerror(err));
88 * Pointer to the error handler function.
89 * For internal use only.
91 snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
94 * \brief Sets the error handler.
95 * \param handler The pointer to the new error handler function.
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.
100 int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
102 snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
104 if (snd_lib_error != snd_lib_error_default)
105 snd_err_msg = snd_lib_error;
111 * \brief Returns the ALSA sound library version in ASCII format
112 * \return The ASCII description of the used ALSA sound library.
114 const char *snd_asoundlib_version(void)
116 return SND_LIB_VERSION_STR;
121 * internal error handling
123 static void snd_err_msg_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
128 verbose = getenv("LIBASOUND_DEBUG");
129 if (! verbose || ! *verbose)
132 fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
133 vfprintf(stderr, fmt, arg);
135 fprintf(stderr, ": %s", snd_strerror(err));
138 #ifdef ALSA_DEBUG_ASSERT
139 verbose = getenv("LIBASOUND_DEBUG_ASSERT");
140 if (verbose && *verbose)
146 * The ALSA error message handler
148 snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;