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