Fix last commit
[carla.git] / source / includes / lv2 / logger.h
blob138b96d1e12fd7a3b96e38c1ce7328ed741a0e2d
1 /*
2 Copyright 2012-2016 David Robillard <http://drobilla.net>
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.
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.
17 /**
18 @defgroup logger Logger
19 @ingroup log
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.
28 #ifndef LV2_ATOM_LOGGER_H
29 #define LV2_ATOM_LOGGER_H
31 #include <stdio.h>
32 #include <string.h>
34 #include "log.h"
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 /**
41 Logger convenience API state.
43 typedef struct {
44 LV2_Log_Log* log;
46 LV2_URID Error;
47 LV2_URID Note;
48 LV2_URID Trace;
49 LV2_URID Warning;
50 } LV2_Log_Logger;
52 /**
53 Set `map` as the URI map for `logger`.
55 This affects the message type URIDs (Error, Warning, etc) which are passed
56 to the log's print functions.
58 static inline void
59 lv2_log_logger_set_map(LV2_Log_Logger* logger, LV2_URID_Map* map)
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;
71 /**
72 Initialise `logger`.
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.
78 static inline void
79 lv2_log_logger_init(LV2_Log_Logger* logger,
80 LV2_URID_Map* map,
81 LV2_Log_Log* log)
83 logger->log = log;
84 lv2_log_logger_set_map(logger, map);
87 /**
88 Log a message to the host, or stderr if support is unavailable.
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)
97 if (logger && logger->log) {
98 return logger->log->vprintf(logger->log->handle, type, fmt, args);
99 } else {
100 return vfprintf(stderr, fmt, args);
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, ...)
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;
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, ...)
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;
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, ...)
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;
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, ...)
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;
152 #ifdef __cplusplus
153 } /* extern "C" */
154 #endif
156 #endif /* LV2_LOG_LOGGER_H */