Blame src/error.c

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