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