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

12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. @file logger.h Convenience API for easy logging in plugin code.
  16. This file provides simple wrappers for the most common log operations for
  17. use in plugin implementations. If host support for logging is not
  18. available, then these functions will print to stderr instead.
  19. This header is non-normative, it is provided for convenience.
  20. */
  21. #ifndef LV2_ATOM_LOGGER_H
  22. #define LV2_ATOM_LOGGER_H
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "log.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. Logger convenience API state.
  31. */
  32. typedef struct {
  33. LV2_Log_Log* log;
  34. LV2_URID Error;
  35. LV2_URID Note;
  36. LV2_URID Trace;
  37. LV2_URID Warning;
  38. } LV2_Log_Logger;
  39. /**
  40. Initialise @p logger.
  41. URIs will be mapped using @p map and stored, a reference to @p map itself is
  42. not held. Both @p map and @p log may be NULL when unsupported by the host,
  43. in which case the implementation will fall back to printing to stderr.
  44. */
  45. static inline void
  46. lv2_log_logger_init(LV2_Log_Logger* logger,
  47. LV2_URID_Map* map,
  48. LV2_Log_Log* log)
  49. {
  50. memset(logger, 0, sizeof(LV2_Log_Logger));
  51. logger->log = log;
  52. if (map) {
  53. logger->Error = map->map(map->handle, LV2_LOG__Error);
  54. logger->Note = map->map(map->handle, LV2_LOG__Note);
  55. logger->Trace = map->map(map->handle, LV2_LOG__Trace);
  56. logger->Warning = map->map(map->handle, LV2_LOG__Warning);
  57. }
  58. }
  59. /**
  60. Log a message to the host, or stderr if support is unavailable.
  61. */
  62. LV2_LOG_FUNC(3, 0)
  63. static inline int
  64. lv2_log_vprintf(LV2_Log_Logger* logger,
  65. LV2_URID type,
  66. const char* fmt,
  67. va_list args)
  68. {
  69. if (logger->log) {
  70. return logger->log->vprintf(logger->log->handle, type, fmt, args);
  71. } else {
  72. return vfprintf(stderr, fmt, args);
  73. }
  74. }
  75. /** Log an error via lv2_log_vprintf(). */
  76. LV2_LOG_FUNC(2, 3)
  77. static inline int
  78. lv2_log_error(LV2_Log_Logger* logger, const char* fmt, ...)
  79. {
  80. va_list args;
  81. va_start(args, fmt);
  82. const int ret = lv2_log_vprintf(logger, logger->Error, fmt, args);
  83. va_end(args);
  84. return ret;
  85. }
  86. /** Log a note via lv2_log_vprintf(). */
  87. LV2_LOG_FUNC(2, 3)
  88. static inline int
  89. lv2_log_note(LV2_Log_Logger* logger, const char* fmt, ...)
  90. {
  91. va_list args;
  92. va_start(args, fmt);
  93. const int ret = lv2_log_vprintf(logger, logger->Note, fmt, args);
  94. va_end(args);
  95. return ret;
  96. }
  97. /** Log a trace via lv2_log_vprintf(). */
  98. LV2_LOG_FUNC(2, 3)
  99. static inline int
  100. lv2_log_trace(LV2_Log_Logger* logger, const char* fmt, ...)
  101. {
  102. va_list args;
  103. va_start(args, fmt);
  104. const int ret = lv2_log_vprintf(logger, logger->Trace, fmt, args);
  105. va_end(args);
  106. return ret;
  107. }
  108. /** Log a warning via lv2_log_vprintf(). */
  109. LV2_LOG_FUNC(2, 3)
  110. static inline int
  111. lv2_log_warning(LV2_Log_Logger* logger, const char* fmt, ...)
  112. {
  113. va_list args;
  114. va_start(args, fmt);
  115. const int ret = lv2_log_vprintf(logger, logger->Warning, fmt, args);
  116. va_end(args);
  117. return ret;
  118. }
  119. /**
  120. @}
  121. */
  122. #ifdef __cplusplus
  123. } /* extern "C" */
  124. #endif
  125. #endif /* LV2_LOG_LOGGER_H */