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.

logger.h 3.9KB

12 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
11 years ago
12 years ago
12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright 2012-2016 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. Set `map` as the URI map for `logger`.
  42. This affects the message type URIDs (Error, Warning, etc) which are passed
  43. to the log's print functions.
  44. */
  45. static inline void
  46. lv2_log_logger_set_map(LV2_Log_Logger* logger, LV2_URID_Map* map)
  47. {
  48. if (map) {
  49. logger->Error = map->map(map->handle, LV2_LOG__Error);
  50. logger->Note = map->map(map->handle, LV2_LOG__Note);
  51. logger->Trace = map->map(map->handle, LV2_LOG__Trace);
  52. logger->Warning = map->map(map->handle, LV2_LOG__Warning);
  53. } else {
  54. logger->Error = logger->Note = logger->Trace = logger->Warning = 0;
  55. }
  56. }
  57. /**
  58. Initialise `logger`.
  59. URIs will be mapped using `map` and stored, a reference to `map` itself is
  60. not held. Both `map` and `log` may be NULL when unsupported by the host,
  61. in which case the implementation will fall back to printing to stderr.
  62. */
  63. static inline void
  64. lv2_log_logger_init(LV2_Log_Logger* logger,
  65. LV2_URID_Map* map,
  66. LV2_Log_Log* log)
  67. {
  68. logger->log = log;
  69. lv2_log_logger_set_map(logger, map);
  70. }
  71. /**
  72. Log a message to the host, or stderr if support is unavailable.
  73. */
  74. LV2_LOG_FUNC(3, 0)
  75. static inline int
  76. lv2_log_vprintf(LV2_Log_Logger* logger,
  77. LV2_URID type,
  78. const char* fmt,
  79. va_list args)
  80. {
  81. if (logger && logger->log) {
  82. return logger->log->vprintf(logger->log->handle, type, fmt, args);
  83. } else {
  84. return vfprintf(stderr, fmt, args);
  85. }
  86. }
  87. /** Log an error via lv2_log_vprintf(). */
  88. LV2_LOG_FUNC(2, 3)
  89. static inline int
  90. lv2_log_error(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->Error, fmt, args);
  95. va_end(args);
  96. return ret;
  97. }
  98. /** Log a note via lv2_log_vprintf(). */
  99. LV2_LOG_FUNC(2, 3)
  100. static inline int
  101. lv2_log_note(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->Note, fmt, args);
  106. va_end(args);
  107. return ret;
  108. }
  109. /** Log a trace via lv2_log_vprintf(). */
  110. LV2_LOG_FUNC(2, 3)
  111. static inline int
  112. lv2_log_trace(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->Trace, fmt, args);
  117. va_end(args);
  118. return ret;
  119. }
  120. /** Log a warning via lv2_log_vprintf(). */
  121. LV2_LOG_FUNC(2, 3)
  122. static inline int
  123. lv2_log_warning(LV2_Log_Logger* logger, const char* fmt, ...)
  124. {
  125. va_list args;
  126. va_start(args, fmt);
  127. const int ret = lv2_log_vprintf(logger, logger->Warning, fmt, args);
  128. va_end(args);
  129. return ret;
  130. }
  131. #ifdef __cplusplus
  132. } /* extern "C" */
  133. #endif
  134. #endif /* LV2_LOG_LOGGER_H */
  135. /**
  136. @}
  137. */