Audio plugin host https://kx.studio/carla
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.

148 lines
3.6KB

  1. /*
  2. Copyright 2012-2013 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. /**
  15. @defgroup logger Logger
  16. @ingroup log
  17. Convenience API for easy logging in plugin code. This API provides simple
  18. wrappers for logging from a plugin, which automatically fall back to
  19. printing to stderr if host support is unavailabe.
  20. @{
  21. */
  22. #ifndef LV2_ATOM_LOGGER_H
  23. #define LV2_ATOM_LOGGER_H
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "log.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /**
  31. Logger convenience API state.
  32. */
  33. typedef struct {
  34. LV2_Log_Log* log;
  35. LV2_URID Error;
  36. LV2_URID Note;
  37. LV2_URID Trace;
  38. LV2_URID Warning;
  39. } LV2_Log_Logger;
  40. /**
  41. Initialise `logger`.
  42. URIs will be mapped using `map` and stored, a reference to `map` itself is
  43. not held. Both `map` and `log` may be NULL when unsupported by the host,
  44. in which case the implementation will fall back to printing to stderr.
  45. */
  46. static inline void
  47. lv2_log_logger_init(LV2_Log_Logger* logger,
  48. LV2_URID_Map* map,
  49. LV2_Log_Log* log)
  50. {
  51. memset(logger, 0, sizeof(LV2_Log_Logger));
  52. logger->log = log;
  53. if (map) {
  54. logger->Error = map->map(map->handle, LV2_LOG__Error);
  55. logger->Note = map->map(map->handle, LV2_LOG__Note);
  56. logger->Trace = map->map(map->handle, LV2_LOG__Trace);
  57. logger->Warning = map->map(map->handle, LV2_LOG__Warning);
  58. }
  59. }
  60. /**
  61. Log a message to the host, or stderr if support is unavailable.
  62. */
  63. LV2_LOG_FUNC(3, 0)
  64. static inline int
  65. lv2_log_vprintf(LV2_Log_Logger* logger,
  66. LV2_URID type,
  67. const char* fmt,
  68. va_list args)
  69. {
  70. if (logger->log) {
  71. return logger->log->vprintf(logger->log->handle, type, fmt, args);
  72. } else {
  73. return vfprintf(stderr, fmt, args);
  74. }
  75. }
  76. /** Log an error via lv2_log_vprintf(). */
  77. LV2_LOG_FUNC(2, 3)
  78. static inline int
  79. lv2_log_error(LV2_Log_Logger* logger, const char* fmt, ...)
  80. {
  81. va_list args;
  82. va_start(args, fmt);
  83. const int ret = lv2_log_vprintf(logger, logger->Error, fmt, args);
  84. va_end(args);
  85. return ret;
  86. }
  87. /** Log a note via lv2_log_vprintf(). */
  88. LV2_LOG_FUNC(2, 3)
  89. static inline int
  90. lv2_log_note(LV2_Log_Logger* logger, const char* fmt, ...)
  91. {
  92. va_list args;
  93. va_start(args, fmt);
  94. const int ret = lv2_log_vprintf(logger, logger->Note, fmt, args);
  95. va_end(args);
  96. return ret;
  97. }
  98. /** Log a trace via lv2_log_vprintf(). */
  99. LV2_LOG_FUNC(2, 3)
  100. static inline int
  101. lv2_log_trace(LV2_Log_Logger* logger, const char* fmt, ...)
  102. {
  103. va_list args;
  104. va_start(args, fmt);
  105. const int ret = lv2_log_vprintf(logger, logger->Trace, fmt, args);
  106. va_end(args);
  107. return ret;
  108. }
  109. /** Log a warning via lv2_log_vprintf(). */
  110. LV2_LOG_FUNC(2, 3)
  111. static inline int
  112. lv2_log_warning(LV2_Log_Logger* logger, const char* fmt, ...)
  113. {
  114. va_list args;
  115. va_start(args, fmt);
  116. const int ret = lv2_log_vprintf(logger, logger->Warning, fmt, args);
  117. va_end(args);
  118. return ret;
  119. }
  120. #ifdef __cplusplus
  121. } /* extern "C" */
  122. #endif
  123. #endif /* LV2_LOG_LOGGER_H */
  124. /**
  125. @}
  126. */