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.

133 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. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include "JackError.h"
  20. #include "JackGlobals.h"
  21. #include "JackMessageBuffer.h"
  22. int jack_verbose = 0;
  23. void change_thread_log_function(jack_log_function_t log_function)
  24. {
  25. if (!jack_tls_set(g_key_log_function, (void*)log_function))
  26. {
  27. jack_error("failed to set thread log function");
  28. }
  29. }
  30. SERVER_EXPORT void set_threaded_log_function()
  31. {
  32. change_thread_log_function(Jack::JackMessageBufferAdd);
  33. }
  34. void jack_log_function(int level, const char *message)
  35. {
  36. void (* log_callback)(const char *);
  37. switch (level)
  38. {
  39. case LOG_LEVEL_INFO:
  40. log_callback = jack_info_callback;
  41. break;
  42. case LOG_LEVEL_ERROR:
  43. log_callback = jack_error_callback;
  44. break;
  45. default:
  46. return;
  47. }
  48. log_callback(message);
  49. }
  50. static void jack_format_and_log(int level, const char *prefix, const char *fmt, va_list ap)
  51. {
  52. char buffer[300];
  53. size_t len;
  54. jack_log_function_t log_function;
  55. if (prefix != NULL) {
  56. len = strlen(prefix);
  57. memcpy(buffer, prefix, len);
  58. } else {
  59. len = 0;
  60. }
  61. vsnprintf(buffer + len, sizeof(buffer) - len, fmt, ap);
  62. log_function = (jack_log_function_t)jack_tls_get(g_key_log_function);
  63. /* if log function is not overriden for thread, use default one */
  64. if (log_function == NULL)
  65. {
  66. log_function = jack_log_function;
  67. //log_function(LOG_LEVEL_INFO, "------ Using default log function");
  68. }
  69. else
  70. {
  71. //log_function(LOG_LEVEL_INFO, "++++++ Using thread-specific log function");
  72. }
  73. log_function(level, buffer);
  74. }
  75. SERVER_EXPORT void jack_error(const char *fmt, ...)
  76. {
  77. va_list ap;
  78. va_start(ap, fmt);
  79. jack_format_and_log(LOG_LEVEL_ERROR, NULL, fmt, ap);
  80. va_end(ap);
  81. }
  82. SERVER_EXPORT void jack_info(const char *fmt, ...)
  83. {
  84. va_list ap;
  85. va_start(ap, fmt);
  86. jack_format_and_log(LOG_LEVEL_INFO, NULL, fmt, ap);
  87. va_end(ap);
  88. }
  89. SERVER_EXPORT void jack_log(const char *fmt,...)
  90. {
  91. if (jack_verbose) {
  92. va_list ap;
  93. va_start(ap, fmt);
  94. jack_format_and_log(LOG_LEVEL_INFO, "Jack: ", fmt, ap);
  95. va_end(ap);
  96. }
  97. }
  98. static void default_jack_error_callback(const char *desc)
  99. {
  100. fprintf(stderr, "%s\n", desc);
  101. fflush(stderr);
  102. }
  103. static void default_jack_info_callback (const char *desc)
  104. {
  105. fprintf(stdout, "%s\n", desc);
  106. fflush(stdout);
  107. }
  108. SERVER_EXPORT void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  109. SERVER_EXPORT void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;