audiolog.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "precompile.h"
  2. #include "audiolog.h"
  3. #include <stdlib.h>
  4. #ifdef _WIN32
  5. static void __stdcall default_log_func(int level, const char* s)
  6. #else
  7. static void __attribute__((__stdcall)) default_log_func(int level, const char* s)
  8. #endif
  9. {
  10. OutputDebugStringA(s);
  11. }
  12. static int g_log_level = 0;
  13. static audio_log_func g_log_func = &default_log_func;
  14. static int can_log(int level)
  15. {
  16. return level >= g_log_level && g_log_func;
  17. }
  18. void audio_log_set_level(int level)
  19. {
  20. g_log_level = level;
  21. }
  22. audio_log_func audio_log_set_func(audio_log_func func)
  23. {
  24. audio_log_func old = g_log_func;
  25. g_log_func = func;
  26. return old;
  27. }
  28. void audio_log(int level, const char *str)
  29. {
  30. if (can_log(level)) {
  31. (*g_log_func)(level, str);
  32. }
  33. }
  34. void audio_log_v(int level, const char *fmt, ...)
  35. {
  36. int rc;
  37. va_list arg;
  38. if (can_log(level)) {
  39. va_start(arg, fmt);
  40. rc = vsnprintf(NULL, 0, fmt, arg);
  41. if (rc > 0) {
  42. char *p = malloc(rc + 1);
  43. if (p) {
  44. vsprintf(p, fmt, arg);
  45. audio_log(level, p);
  46. free(p);
  47. }
  48. }
  49. va_end(arg);
  50. }
  51. }