audiolog.c 919 B

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