1 /*
2 Copyright 2012-2016 David Robillard <d@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 #ifndef LV2_ATOM_LOGGER_H
18 #define LV2_ATOM_LOGGER_H
19
20 /**
21 @defgroup logger Logger
22 @ingroup log
23
24 Convenience API for easy logging in plugin code. This API provides simple
25 wrappers for logging from a plugin, which automatically fall back to
26 printing to stderr if host support is unavailabe.
27
28 @{
29 */
30
31 #include "lv2/log/log.h"
32 #include "lv2/urid/urid.h"
33
34 #include <stdarg.h>
35 #include <stdio.h>
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42 Logger convenience API state.
43 */
44 typedef struct {
45 LV2_Log_Log* log;
46
47 LV2_URID Error;
48 LV2_URID Note;
49 LV2_URID Trace;
50 LV2_URID Warning;
51 } LV2_Log_Logger;
52
53 /**
54 Set `map` as the URI map for `logger`.
55
56 This affects the message type URIDs (Error, Warning, etc) which are passed
57 to the log's print functions.
58 */
59 static inline void
lv2_log_logger_set_map(LV2_Log_Logger * logger,LV2_URID_Map * map)60 lv2_log_logger_set_map(LV2_Log_Logger* logger, LV2_URID_Map* map)
61 {
62 if (map) {
63 logger->Error = map->map(map->handle, LV2_LOG__Error);
64 logger->Note = map->map(map->handle, LV2_LOG__Note);
65 logger->Trace = map->map(map->handle, LV2_LOG__Trace);
66 logger->Warning = map->map(map->handle, LV2_LOG__Warning);
67 } else {
68 logger->Error = logger->Note = logger->Trace = logger->Warning = 0;
69 }
70 }
71
72 /**
73 Initialise `logger`.
74
75 URIs will be mapped using `map` and stored, a reference to `map` itself is
76 not held. Both `map` and `log` may be NULL when unsupported by the host,
77 in which case the implementation will fall back to printing to stderr.
78 */
79 static inline void
lv2_log_logger_init(LV2_Log_Logger * logger,LV2_URID_Map * map,LV2_Log_Log * log)80 lv2_log_logger_init(LV2_Log_Logger* logger, LV2_URID_Map* map, LV2_Log_Log* log)
81 {
82 logger->log = log;
83 lv2_log_logger_set_map(logger, map);
84 }
85
86 /**
87 Log a message to the host, or stderr if support is unavailable.
88 */
89 LV2_LOG_FUNC(3, 0)
90 static inline int
lv2_log_vprintf(LV2_Log_Logger * logger,LV2_URID type,const char * fmt,va_list args)91 lv2_log_vprintf(LV2_Log_Logger* logger,
92 LV2_URID type,
93 const char* fmt,
94 va_list args)
95 {
96 return ((logger && logger->log)
97 ? logger->log->vprintf(logger->log->handle, type, fmt, args)
98 : vfprintf(stderr, fmt, args));
99 }
100
101 /** Log an error via lv2_log_vprintf(). */
102 LV2_LOG_FUNC(2, 3)
103 static inline int
lv2_log_error(LV2_Log_Logger * logger,const char * fmt,...)104 lv2_log_error(LV2_Log_Logger* logger, const char* fmt, ...)
105 {
106 va_list args;
107 va_start(args, fmt);
108 const int ret = lv2_log_vprintf(logger, logger->Error, fmt, args);
109 va_end(args);
110 return ret;
111 }
112
113 /** Log a note via lv2_log_vprintf(). */
114 LV2_LOG_FUNC(2, 3)
115 static inline int
lv2_log_note(LV2_Log_Logger * logger,const char * fmt,...)116 lv2_log_note(LV2_Log_Logger* logger, const char* fmt, ...)
117 {
118 va_list args;
119 va_start(args, fmt);
120 const int ret = lv2_log_vprintf(logger, logger->Note, fmt, args);
121 va_end(args);
122 return ret;
123 }
124
125 /** Log a trace via lv2_log_vprintf(). */
126 LV2_LOG_FUNC(2, 3)
127 static inline int
lv2_log_trace(LV2_Log_Logger * logger,const char * fmt,...)128 lv2_log_trace(LV2_Log_Logger* logger, const char* fmt, ...)
129 {
130 va_list args;
131 va_start(args, fmt);
132 const int ret = lv2_log_vprintf(logger, logger->Trace, fmt, args);
133 va_end(args);
134 return ret;
135 }
136
137 /** Log a warning via lv2_log_vprintf(). */
138 LV2_LOG_FUNC(2, 3)
139 static inline int
lv2_log_warning(LV2_Log_Logger * logger,const char * fmt,...)140 lv2_log_warning(LV2_Log_Logger* logger, const char* fmt, ...)
141 {
142 va_list args;
143 va_start(args, fmt);
144 const int ret = lv2_log_vprintf(logger, logger->Warning, fmt, args);
145 va_end(args);
146 return ret;
147 }
148
149 #ifdef __cplusplus
150 } /* extern "C" */
151 #endif
152
153 /**
154 @}
155 */
156
157 #endif /* LV2_LOG_LOGGER_H */
158