|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \file error.c
|
|
Packit Service |
db8eaa |
* \brief Error code handling routines
|
|
Packit Service |
db8eaa |
* \author Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
* \date 1998-2001
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* Error code handling routines.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* snd_strerror routine needs to be recoded for the locale support
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This library is free software; you can redistribute it and/or modify
|
|
Packit Service |
db8eaa |
* it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
db8eaa |
* published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
db8eaa |
* the License, or (at your option) any later version.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
db8eaa |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
db8eaa |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
db8eaa |
* GNU Lesser General Public License for more details.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
db8eaa |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
db8eaa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#include <stdio.h>
|
|
Packit Service |
db8eaa |
#include <stdlib.h>
|
|
Packit Service |
db8eaa |
#include <stdarg.h>
|
|
Packit Service |
db8eaa |
#include <string.h>
|
|
Packit Service |
db8eaa |
#include "local.h"
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* Array of error codes in US ASCII.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
static const char *snd_error_codes[] =
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
"Sound protocol is not compatible"
|
|
Packit Service |
db8eaa |
};
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Returns the message for an error code.
|
|
Packit Service |
db8eaa |
* \param errnum The error code number, which must be a system error code
|
|
Packit Service |
db8eaa |
* or an ALSA error code.
|
|
Packit Service |
db8eaa |
* \return The ASCII description of the given numeric error code.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
const char *snd_strerror(int errnum)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
if (errnum < 0)
|
|
Packit Service |
db8eaa |
errnum = -errnum;
|
|
Packit Service |
db8eaa |
if (errnum < SND_ERROR_BEGIN)
|
|
Packit Service |
db8eaa |
return (const char *) strerror(errnum);
|
|
Packit Service |
db8eaa |
errnum -= SND_ERROR_BEGIN;
|
|
Packit Service |
db8eaa |
if ((unsigned int) errnum >= sizeof(snd_error_codes) / sizeof(const char *))
|
|
Packit Service |
db8eaa |
return "Unknown error";
|
|
Packit Service |
db8eaa |
return snd_error_codes[errnum];
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#ifndef DOC_HIDDEN
|
|
Packit Service |
db8eaa |
#ifdef HAVE___THREAD
|
|
Packit Service |
db8eaa |
#define TLS_PFX __thread
|
|
Packit Service |
db8eaa |
#else
|
|
Packit Service |
db8eaa |
#define TLS_PFX /* NOP */
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static TLS_PFX snd_local_error_handler_t local_error = NULL;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Install local error handler
|
|
Packit Service |
db8eaa |
* \param func The local error handler function
|
|
Packit Service |
db8eaa |
* \retval Previous local error handler function
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
snd_local_error_handler_t snd_lib_error_set_local(snd_local_error_handler_t func)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_local_error_handler_t old = local_error;
|
|
Packit Service |
db8eaa |
local_error = func;
|
|
Packit Service |
db8eaa |
return old;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief The default error handler function.
|
|
Packit Service |
db8eaa |
* \param file The filename where the error was hit.
|
|
Packit Service |
db8eaa |
* \param line The line number.
|
|
Packit Service |
db8eaa |
* \param function The function name.
|
|
Packit Service |
db8eaa |
* \param err The error code.
|
|
Packit Service |
db8eaa |
* \param fmt The message (including the format characters).
|
|
Packit Service |
db8eaa |
* \param ... Optional arguments.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* If a local error function has been installed for the current thread by
|
|
Packit Service |
db8eaa |
* \ref snd_lib_error_set_local, it is called. Otherwise, prints the error
|
|
Packit Service |
db8eaa |
* message including location to \c stderr.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
va_list arg;
|
|
Packit Service |
db8eaa |
va_start(arg, fmt);
|
|
Packit Service |
db8eaa |
if (local_error) {
|
|
Packit Service |
db8eaa |
local_error(file, line, function, err, fmt, arg);
|
|
Packit Service |
db8eaa |
va_end(arg);
|
|
Packit Service |
db8eaa |
return;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
|
|
Packit Service |
db8eaa |
vfprintf(stderr, fmt, arg);
|
|
Packit Service |
db8eaa |
if (err)
|
|
Packit Service |
db8eaa |
fprintf(stderr, ": %s", snd_strerror(err));
|
|
Packit Service |
db8eaa |
putc('\n', stderr);
|
|
Packit Service |
db8eaa |
va_end(arg);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \ingroup Error
|
|
Packit Service |
db8eaa |
* Pointer to the error handler function.
|
|
Packit Service |
db8eaa |
* For internal use only.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Sets the error handler.
|
|
Packit Service |
db8eaa |
* \param handler The pointer to the new error handler function.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This function sets a new error handler, or (if \c handler is \c NULL)
|
|
Packit Service |
db8eaa |
* the default one which prints the error messages to \c stderr.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
|
|
Packit Service |
db8eaa |
#ifndef NDEBUG
|
|
Packit Service |
db8eaa |
if (snd_lib_error != snd_lib_error_default)
|
|
Packit Service |
db8eaa |
snd_err_msg = snd_lib_error;
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Returns the ALSA sound library version in ASCII format
|
|
Packit Service |
db8eaa |
* \return The ASCII description of the used ALSA sound library.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
const char *snd_asoundlib_version(void)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
return SND_LIB_VERSION_STR;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#ifndef NDEBUG
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* internal error handling
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
static void snd_err_msg_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
va_list arg;
|
|
Packit Service |
db8eaa |
const char *verbose;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
verbose = getenv("LIBASOUND_DEBUG");
|
|
Packit Service |
db8eaa |
if (! verbose || ! *verbose)
|
|
Packit Service |
db8eaa |
return;
|
|
Packit Service |
db8eaa |
va_start(arg, fmt);
|
|
Packit Service |
db8eaa |
fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
|
|
Packit Service |
db8eaa |
vfprintf(stderr, fmt, arg);
|
|
Packit Service |
db8eaa |
if (err)
|
|
Packit Service |
db8eaa |
fprintf(stderr, ": %s", snd_strerror(err));
|
|
Packit Service |
db8eaa |
putc('\n', stderr);
|
|
Packit Service |
db8eaa |
va_end(arg);
|
|
Packit Service |
db8eaa |
#ifdef ALSA_DEBUG_ASSERT
|
|
Packit Service |
db8eaa |
verbose = getenv("LIBASOUND_DEBUG_ASSERT");
|
|
Packit Service |
db8eaa |
if (verbose && *verbose)
|
|
Packit Service |
db8eaa |
assert(0);
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* The ALSA error message handler
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Copy a C-string into a sized buffer
|
|
Packit Service |
db8eaa |
* \param dst Where to copy the string to
|
|
Packit Service |
db8eaa |
* \param src Where to copy the string from
|
|
Packit Service |
db8eaa |
* \param size Size of destination buffer
|
|
Packit Service |
db8eaa |
* \retval The source string length
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* The result is always a valid NUL-terminated string that fits
|
|
Packit Service |
db8eaa |
* in the buffer (unless, of course, the buffer size is zero).
|
|
Packit Service |
db8eaa |
* It does not pad out the result like strncpy() does.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
size_t snd_strlcpy(char *dst, const char *src, size_t size)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
size_t ret = strlen(src);
|
|
Packit Service |
db8eaa |
if (size) {
|
|
Packit Service |
db8eaa |
size_t len = ret >= size ? size - 1 : ret;
|
|
Packit Service |
db8eaa |
memcpy(dst, src, len);
|
|
Packit Service |
db8eaa |
dst[len] = '\0';
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return ret;
|
|
Packit Service |
db8eaa |
}
|