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.

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