Make use of JAYLINK_LOG_LEVEL_DEBUG_IO
[libjaylink.git] / libjaylink / log.c
blob07ef172d976ad13707a3aa249625235b27c65932
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2015 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <stdio.h>
25 #include <stdarg.h>
27 #include "libjaylink.h"
28 #include "libjaylink-internal.h"
30 /**
31 * @file
33 * Logging functions.
36 /**
37 * Set the libjaylink log level.
39 * @param[in,out] ctx libjaylink context.
40 * @param[in] level Log level to set.
42 * @retval JAYLINK_OK Success.
43 * @retval JAYLINK_ERR_ARG Invalid arguments.
45 * @since 0.1.0
47 JAYLINK_API int jaylink_log_set_level(struct jaylink_context *ctx,
48 enum jaylink_log_level level)
50 if (!ctx)
51 return JAYLINK_ERR_ARG;
53 if (level > JAYLINK_LOG_LEVEL_DEBUG_IO)
54 return JAYLINK_ERR_ARG;
56 ctx->log_level = level;
58 return JAYLINK_OK;
61 /**
62 * Get the libjaylink log level.
64 * @param[in] ctx libjaylink context.
65 * @param[out] level Log level on success, and undefined on failure.
67 * @retval JAYLINK_OK Success.
68 * @retval JAYLINK_ERR_ARG Invalid arguments.
70 * @since 0.1.0
72 JAYLINK_API int jaylink_log_get_level(const struct jaylink_context *ctx,
73 enum jaylink_log_level *level)
75 if (!ctx || !level)
76 return JAYLINK_ERR_ARG;
78 *level = ctx->log_level;
80 return JAYLINK_OK;
83 /**
84 * Set the libjaylink log callback function.
86 * @param[in,out] ctx libjaylink context.
87 * @param[in] callback Callback function to use, or NULL to use the default log
88 * function.
89 * @param[in] user_data User data to be passed to the callback function.
91 * @retval JAYLINK_OK Success.
92 * @retval JAYLINK_ERR_ARG Invalid arguments.
94 * @since 0.1.0
96 JAYLINK_API int jaylink_log_set_callback(struct jaylink_context *ctx,
97 jaylink_log_callback callback, void *user_data)
99 if (!ctx)
100 return JAYLINK_ERR_ARG;
102 if (callback) {
103 ctx->log_callback = callback;
104 ctx->log_callback_data = user_data;
105 } else {
106 ctx->log_callback = &log_vprintf;
107 ctx->log_callback_data = NULL;
110 return JAYLINK_OK;
114 * Set the libjaylink log domain.
116 * The log domain is a string which is used as prefix for all log messages to
117 * differentiate them from messages of other libraries.
119 * The maximum length of the log domain is #JAYLINK_LOG_DOMAIN_MAX_LENGTH
120 * bytes, excluding the trailing null-terminator. A log domain which exceeds
121 * this length will be silently truncated.
123 * @param[in,out] ctx libjaylink context.
124 * @param[in] domain Log domain to use. To set the default log domain, use
125 * #JAYLINK_LOG_DOMAIN_DEFAULT.
127 * @retval JAYLINK_OK Success.
128 * @retval JAYLINK_ERR_ARG Invalid arguments.
129 * @retval JAYLINK_ERR Other error conditions.
131 * @since 0.1.0
133 JAYLINK_API int jaylink_log_set_domain(struct jaylink_context *ctx,
134 const char *domain)
136 int ret;
138 if (!ctx || !domain)
139 return JAYLINK_ERR_ARG;
141 ret = snprintf(ctx->log_domain, JAYLINK_LOG_DOMAIN_MAX_LENGTH + 1,
142 "%s", domain);
144 if (ret < 0)
145 return JAYLINK_ERR;
147 return JAYLINK_OK;
151 * Get the libjaylink log domain.
153 * @param[in] ctx libjaylink context.
155 * @return A string which contains the current log domain on success, or NULL
156 * on failure. The string is null-terminated and must not be free'd by
157 * the caller.
159 * @since 0.1.0
161 JAYLINK_API const char *jaylink_log_get_domain(
162 const struct jaylink_context *ctx)
164 if (!ctx)
165 return NULL;
167 return ctx->log_domain;
170 /** @private */
171 JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx,
172 enum jaylink_log_level level, const char *format, va_list args,
173 void *user_data)
175 (void)user_data;
178 * Filter out messages with higher verbosity than the verbosity of the
179 * current log level.
181 if (level > ctx->log_level)
182 return 0;
184 if (ctx->log_domain[0] != '\0')
185 fprintf(stderr, "%s", ctx->log_domain);
187 vfprintf(stderr, format, args);
188 fprintf(stderr, "\n");
190 return 0;
193 /** @private */
194 JAYLINK_PRIV void log_err(const struct jaylink_context *ctx,
195 const char *format, ...)
197 va_list args;
199 if (!ctx)
200 return;
202 va_start(args, format);
203 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_ERROR, format, args,
204 ctx->log_callback_data);
205 va_end(args);
208 /** @private */
209 JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx,
210 const char *format, ...)
212 va_list args;
214 if (!ctx)
215 return;
217 va_start(args, format);
218 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_WARNING, format, args,
219 ctx->log_callback_data);
220 va_end(args);
223 /** @private */
224 JAYLINK_PRIV void log_info(const struct jaylink_context *ctx,
225 const char *format, ...)
227 va_list args;
229 if (!ctx)
230 return;
232 va_start(args, format);
233 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_INFO, format, args,
234 ctx->log_callback_data);
235 va_end(args);
238 /** @private */
239 JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx,
240 const char *format, ...)
242 va_list args;
244 if (!ctx)
245 return;
247 va_start(args, format);
248 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_DEBUG, format, args,
249 ctx->log_callback_data);
250 va_end(args);
253 /** @private */
254 JAYLINK_PRIV void log_dbgio(const struct jaylink_context *ctx,
255 const char *format, ...)
257 va_list args;
259 if (!ctx)
260 return;
262 va_start(args, format);
263 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_DEBUG_IO, format, args,
264 ctx->log_callback_data);
265 va_end(args);