PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/alsa-lib/src/error.c

https://github.com/kularny/android_device_motorola_omap4-common
C | 179 lines | 87 code | 14 blank | 78 comment | 13 complexity | 2161c0924b9d59f517253435f0fea2f8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  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. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <stdarg.h>
  33. #include <string.h>
  34. #include "local.h"
  35. /**
  36. * Array of error codes in US ASCII.
  37. */
  38. static const char *snd_error_codes[] =
  39. {
  40. "Sound protocol is not compatible"
  41. };
  42. /**
  43. * \brief Returns the message for an error code.
  44. * \param errnum The error code number, which must be a system error code
  45. * or an ALSA error code.
  46. * \return The ASCII description of the given numeric error code.
  47. */
  48. const char *snd_strerror(int errnum)
  49. {
  50. if (errnum < 0)
  51. errnum = -errnum;
  52. if (errnum < SND_ERROR_BEGIN)
  53. return (const char *) strerror(errnum);
  54. errnum -= SND_ERROR_BEGIN;
  55. if ((unsigned int) errnum >= sizeof(snd_error_codes) / sizeof(const char *))
  56. return "Unknown error";
  57. return snd_error_codes[errnum];
  58. }
  59. #ifndef DOC_HIDDEN
  60. #ifdef HAVE___THREAD
  61. #define TLS_PFX __thread
  62. #else
  63. #define TLS_PFX /* NOP */
  64. #endif
  65. #endif
  66. static TLS_PFX snd_local_error_handler_t local_error = NULL;
  67. /**
  68. * \brief Install local error handler
  69. * \param func The local error handler function
  70. * \retval Previous local error handler function
  71. */
  72. snd_local_error_handler_t snd_lib_error_set_local(snd_local_error_handler_t func)
  73. {
  74. snd_local_error_handler_t old = local_error;
  75. local_error = func;
  76. return old;
  77. }
  78. /**
  79. * \brief The default error handler function.
  80. * \param file The filename where the error was hit.
  81. * \param line The line number.
  82. * \param function The function name.
  83. * \param err The error code.
  84. * \param fmt The message (including the format characters).
  85. * \param ... Optional arguments.
  86. *
  87. * If a local error function has been installed for the current thread by
  88. * \ref snd_lib_error_set_local, it is called. Otherwise, prints the error
  89. * message including location to \c stderr.
  90. */
  91. static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
  92. {
  93. va_list arg;
  94. va_start(arg, fmt);
  95. if (local_error) {
  96. local_error(file, line, function, err, fmt, arg);
  97. va_end(arg);
  98. return;
  99. }
  100. fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
  101. vfprintf(stderr, fmt, arg);
  102. if (err)
  103. fprintf(stderr, ": %s", snd_strerror(err));
  104. putc('\n', stderr);
  105. va_end(arg);
  106. }
  107. /**
  108. * \ingroup Error
  109. * Pointer to the error handler function.
  110. * For internal use only.
  111. */
  112. snd_lib_error_handler_t snd_lib_error = snd_lib_error_default;
  113. /**
  114. * \brief Sets the error handler.
  115. * \param handler The pointer to the new error handler function.
  116. *
  117. * This function sets a new error handler, or (if \c handler is \c NULL)
  118. * the default one which prints the error messages to \c stderr.
  119. */
  120. int snd_lib_error_set_handler(snd_lib_error_handler_t handler)
  121. {
  122. snd_lib_error = handler == NULL ? snd_lib_error_default : handler;
  123. #ifndef NDEBUG
  124. if (snd_lib_error != snd_lib_error_default)
  125. snd_err_msg = snd_lib_error;
  126. #endif
  127. return 0;
  128. }
  129. /**
  130. * \brief Returns the ALSA sound library version in ASCII format
  131. * \return The ASCII description of the used ALSA sound library.
  132. */
  133. const char *snd_asoundlib_version(void)
  134. {
  135. return SND_LIB_VERSION_STR;
  136. }
  137. #ifndef NDEBUG
  138. /*
  139. * internal error handling
  140. */
  141. static void snd_err_msg_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
  142. {
  143. va_list arg;
  144. const char *verbose;
  145. verbose = getenv("LIBASOUND_DEBUG");
  146. if (! verbose || ! *verbose)
  147. return;
  148. va_start(arg, fmt);
  149. fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
  150. vfprintf(stderr, fmt, arg);
  151. if (err)
  152. fprintf(stderr, ": %s", snd_strerror(err));
  153. putc('\n', stderr);
  154. va_end(arg);
  155. #ifdef ALSA_DEBUG_ASSERT
  156. verbose = getenv("LIBASOUND_DEBUG_ASSERT");
  157. if (verbose && *verbose)
  158. assert(0);
  159. #endif
  160. }
  161. /**
  162. * The ALSA error message handler
  163. */
  164. snd_lib_error_handler_t snd_err_msg = snd_err_msg_default;
  165. #endif