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.2KB

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