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