jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.3KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. Copyright (C) 2008 Nedko Arnaudov
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #if defined(HAVE_CONFIG_H)
  18. #include "config.h"
  19. #endif
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include "JackError.h"
  23. #include "JackGlobals.h"
  24. #include "JackMessageBuffer.h"
  25. int jack_verbose = 0;
  26. void change_thread_log_function(jack_log_function_t log_function)
  27. {
  28. if (!jack_tls_set(g_key_log_function, (void*)log_function))
  29. {
  30. jack_error("failed to set thread log function");
  31. }
  32. }
  33. EXPORT void set_threaded_log_function()
  34. {
  35. change_thread_log_function(Jack::JackMessageBufferAdd);
  36. }
  37. void jack_log_function(int level, const char *message)
  38. {
  39. void (* log_callback)(const char *);
  40. switch (level)
  41. {
  42. case LOG_LEVEL_INFO:
  43. log_callback = jack_info_callback;
  44. break;
  45. case LOG_LEVEL_ERROR:
  46. log_callback = jack_error_callback;
  47. break;
  48. default:
  49. return;
  50. }
  51. log_callback(message);
  52. }
  53. static
  54. void
  55. jack_format_and_log(int level, const char *prefix, const char *fmt, va_list ap)
  56. {
  57. char buffer[300];
  58. size_t len;
  59. jack_log_function_t log_function;
  60. if (prefix != NULL) {
  61. len = strlen(prefix);
  62. memcpy(buffer, prefix, len);
  63. } else {
  64. len = 0;
  65. }
  66. vsnprintf(buffer + len, sizeof(buffer) - len, fmt, ap);
  67. log_function = (jack_log_function_t)jack_tls_get(g_key_log_function);
  68. /* if log function is not overriden for thread, use default one */
  69. if (log_function == NULL)
  70. {
  71. log_function = jack_log_function;
  72. //log_function(LOG_LEVEL_INFO, "------ Using default log function");
  73. }
  74. else
  75. {
  76. //log_function(LOG_LEVEL_INFO, "++++++ Using thread-specific log function");
  77. }
  78. log_function(level, buffer);
  79. }
  80. EXPORT void jack_error(const char *fmt, ...)
  81. {
  82. va_list ap;
  83. va_start(ap, fmt);
  84. jack_format_and_log(LOG_LEVEL_ERROR, NULL, fmt, ap);
  85. va_end(ap);
  86. }
  87. EXPORT void jack_info(const char *fmt, ...)
  88. {
  89. va_list ap;
  90. va_start(ap, fmt);
  91. jack_format_and_log(LOG_LEVEL_INFO, NULL, fmt, ap);
  92. va_end(ap);
  93. }
  94. EXPORT void jack_log(const char *fmt,...)
  95. {
  96. if (jack_verbose) {
  97. va_list ap;
  98. va_start(ap, fmt);
  99. jack_format_and_log(LOG_LEVEL_INFO, "Jack: ", fmt, ap);
  100. va_end(ap);
  101. }
  102. }
  103. static void default_jack_error_callback(const char *desc)
  104. {
  105. fprintf(stderr, "%s\n", desc);
  106. fflush(stderr);
  107. }
  108. static void default_jack_info_callback (const char *desc)
  109. {
  110. fprintf(stdout, "%s\n", desc);
  111. fflush(stdout);
  112. }
  113. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  114. void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;