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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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];
65 #define TLS_PFX __thread
67 #define TLS_PFX /* NOP */
71 static TLS_PFX snd_local_error_handler_t local_error = NULL;
74 * \brief Install local error handler
75 * \param func The local error handler function
76 * \retval Previous local error handler function
78 snd_local_error_handler_t snd_lib_error_set_local(snd_local_error_handler_t func)
80 snd_local_error_handler_t old = local_error;
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.
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.
98 static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
103 local_error(file, line, function, err, fmt, arg);
107 fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
108 vfprintf(stderr, fmt, arg);
110 fprintf(stderr, ": %s", snd_strerror(err));
117 * Pointer to the error handler function.
118 * For internal use only.
120 snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
123 * \brief Sets the error handler.
124 * \param handler The pointer to the new error handler function.
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.
129 int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
131 snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
133 if (snd_lib_error != snd_lib_error_default)
134 snd_err_msg = snd_lib_error;
140 * \brief Returns the ALSA sound library version in ASCII format
141 * \return The ASCII description of the used ALSA sound library.
143 const char *snd_asoundlib_version(void)
145 return SND_LIB_VERSION_STR;
150 * internal error handling
152 static void snd_err_msg_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
157 verbose = getenv("LIBASOUND_DEBUG");
158 if (! verbose || ! *verbose)
161 fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
162 vfprintf(stderr, fmt, arg);
164 fprintf(stderr, ": %s", snd_strerror(err));
167 #ifdef ALSA_DEBUG_ASSERT
168 verbose = getenv("LIBASOUND_DEBUG_ASSERT");
169 if (verbose && *verbose)
175 * The ALSA error message handler
177 snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
182 * \brief Copy a C-string into a sized buffer
183 * \param dst Where to copy the string to
184 * \param src Where to copy the string from
185 * \param size Size of destination buffer
186 * \retval The source string length
188 * The result is always a valid NUL-terminated string that fits
189 * in the buffer (unless, of course, the buffer size is zero).
190 * It does not pad out the result like strncpy() does.
192 size_t snd_strlcpy(char *dst, const char *src, size_t size)
194 size_t ret = strlen(src);
196 size_t len = ret >= size ? size - 1 : ret;
197 memcpy(dst, src, len);