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.

137 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. SERVER_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 void jack_format_and_log(int level, const char *prefix, const char *fmt, va_list ap)
  54. {
  55. char buffer[300];
  56. size_t len;
  57. jack_log_function_t log_function;
  58. if (prefix != NULL) {
  59. len = strlen(prefix);
  60. memcpy(buffer, prefix, len);
  61. } else {
  62. len = 0;
  63. }
  64. vsnprintf(buffer + len, sizeof(buffer) - len, fmt, ap);
  65. log_function = (jack_log_function_t)jack_tls_get(g_key_log_function);
  66. /* if log function is not overriden for thread, use default one */
  67. if (log_function == NULL)
  68. {
  69. log_function = jack_log_function;
  70. //log_function(LOG_LEVEL_INFO, "------ Using default log function");
  71. }
  72. else
  73. {
  74. //log_function(LOG_LEVEL_INFO, "++++++ Using thread-specific log function");
  75. }
  76. log_function(level, buffer);
  77. }
  78. SERVER_EXPORT void jack_error(const char *fmt, ...)
  79. {
  80. va_list ap;
  81. va_start(ap, fmt);
  82. jack_format_and_log(LOG_LEVEL_ERROR, NULL, fmt, ap);
  83. va_end(ap);
  84. }
  85. SERVER_EXPORT void jack_info(const char *fmt, ...)
  86. {
  87. va_list ap;
  88. va_start(ap, fmt);
  89. jack_format_and_log(LOG_LEVEL_INFO, NULL, fmt, ap);
  90. va_end(ap);
  91. }
  92. SERVER_EXPORT void jack_log(const char *fmt,...)
  93. {
  94. if (jack_verbose) {
  95. va_list ap;
  96. va_start(ap, fmt);
  97. jack_format_and_log(LOG_LEVEL_INFO, "Jack: ", fmt, ap);
  98. va_end(ap);
  99. }
  100. }
  101. static void default_jack_error_callback(const char *desc)
  102. {
  103. fprintf(stderr, "%s\n", desc);
  104. fflush(stderr);
  105. }
  106. static void default_jack_info_callback (const char *desc)
  107. {
  108. fprintf(stdout, "%s\n", desc);
  109. fflush(stdout);
  110. }
  111. SERVER_EXPORT void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  112. SERVER_EXPORT void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;