PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/error.c

https://github.com/mirsal/vlc-mirsal
C | 128 lines | 68 code | 18 blank | 42 comment | 9 complexity | 0077488f551df049e0a36ea04f227ff5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, WTFPL
  1. /*****************************************************************************
  2. * error.c: Error handling for libvlc
  3. *****************************************************************************
  4. * Copyright (C) 2009 RĂ©mi Denis-Courmont
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  19. *****************************************************************************/
  20. #include "libvlc_internal.h"
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include <vlc/libvlc.h>
  25. static const char oom[] = "Out of memory";
  26. /* TODO: use only one thread-specific key for whole libvlc */
  27. static vlc_threadvar_t context;
  28. static char *get_error (void)
  29. {
  30. return vlc_threadvar_get (context);
  31. }
  32. static void free_msg (void *msg)
  33. {
  34. if (msg != oom)
  35. free (msg);
  36. }
  37. static void free_error (void)
  38. {
  39. free_msg (get_error ());
  40. }
  41. static vlc_mutex_t lock = VLC_STATIC_MUTEX;
  42. static uintptr_t refs = 0;
  43. void libvlc_threads_init (void)
  44. {
  45. vlc_mutex_lock (&lock);
  46. if (refs++ == 0)
  47. vlc_threadvar_create (&context, free_msg);
  48. vlc_mutex_unlock (&lock);
  49. }
  50. void libvlc_threads_deinit (void)
  51. {
  52. vlc_mutex_lock (&lock);
  53. assert (refs > 0);
  54. if (--refs == 0)
  55. {
  56. free_error ();
  57. vlc_threadvar_delete (&context);
  58. }
  59. vlc_mutex_unlock (&lock);
  60. }
  61. /**
  62. * Gets a human-readable error message for the last LibVLC error in the calling
  63. * thread. The resulting string is valid until another error occurs (at least
  64. * until the next LibVLC call).
  65. *
  66. * @return NULL if there was no error, a nul-terminated string otherwise.
  67. */
  68. const char *libvlc_errmsg (void)
  69. {
  70. return get_error ();
  71. }
  72. /**
  73. * Clears the LibVLC error status for the current thread. This is optional.
  74. * By default, the error status is automatically overridden when a new error
  75. * occurs, and destroyed when the thread exits.
  76. */
  77. void libvlc_clearerr (void)
  78. {
  79. free_error ();
  80. vlc_threadvar_set (context, NULL);
  81. }
  82. /**
  83. * Sets the LibVLC error status and message for the current thread.
  84. * Any previous error is overridden.
  85. * @return a nul terminated string (always)
  86. */
  87. const char *libvlc_vprinterr (const char *fmt, va_list ap)
  88. {
  89. char *msg;
  90. assert (fmt != NULL);
  91. if (vasprintf (&msg, fmt, ap) == -1)
  92. msg = (char *)oom;
  93. free_error ();
  94. vlc_threadvar_set (context, msg);
  95. return msg;
  96. }
  97. /**
  98. * Sets the LibVLC error status and message for the current thread.
  99. * Any previous error is overridden.
  100. * @return a nul terminated string (always)
  101. */
  102. const char *libvlc_printerr (const char *fmt, ...)
  103. {
  104. va_list ap;
  105. const char *msg;
  106. va_start (ap, fmt);
  107. msg = libvlc_vprinterr (fmt, ap);
  108. va_end (ap);
  109. return msg;
  110. }