Add jaylink_get_device()
[libjaylink.git] / libjaylink / log.c
blob75074d182083c7966de4d88f6d1d551e09d13683
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 #include <stdio.h>
21 #include <stdarg.h>
23 #include "libjaylink.h"
24 #include "libjaylink-internal.h"
26 /**
27 * @file
29 * Logging functions.
32 /**
33 * Set the libjaylink log level.
35 * @param[in,out] ctx libjaylink context.
36 * @param[in] level Log level to set. See #jaylink_log_level for valid values.
38 * @retval JAYLINK_OK Success.
39 * @retval JAYLINK_ERR_ARG Invalid arguments.
41 JAYLINK_API int jaylink_log_set_level(struct jaylink_context *ctx, int level)
43 if (!ctx)
44 return JAYLINK_ERR_ARG;
46 if (level < JAYLINK_LOG_LEVEL_NONE || level > JAYLINK_LOG_LEVEL_DEBUG)
47 return JAYLINK_ERR_ARG;
49 ctx->log_level = level;
51 return JAYLINK_OK;
54 /**
55 * Get the libjaylink log level.
57 * @param[in] ctx libjaylink context.
59 * @return The current log level on success, or a negative error code
60 * on failure. See #jaylink_log_level for a description of each
61 * individual log level.
63 JAYLINK_API int jaylink_log_get_level(const struct jaylink_context *ctx)
65 if (!ctx)
66 return JAYLINK_ERR_ARG;
68 return ctx->log_level;
71 /**
72 * Set the libjaylink log callback function.
74 * @param[in,out] ctx libjaylink context.
75 * @param[in] callback Callback function to use, or NULL to use the default log
76 * function.
77 * @param[in] user_data User data to be passed to the callback function.
79 * @retval JAYLINK_OK Success.
80 * @retval JAYLINK_ERR_ARG Invalid arguments.
82 JAYLINK_API int jaylink_log_set_callback(struct jaylink_context *ctx,
83 jaylink_log_callback callback, void *user_data)
85 if (!ctx)
86 return JAYLINK_ERR_ARG;
88 if (callback) {
89 ctx->log_callback = callback;
90 ctx->log_callback_data = user_data;
91 } else {
92 ctx->log_callback = &log_vprintf;
93 ctx->log_callback_data = NULL;
96 return JAYLINK_OK;
99 /**
100 * Set the libjaylink log domain.
102 * The log domain is a string which is used as prefix for all log messages to
103 * differentiate them from messages of other libraries.
105 * The maximum length of the log domain is #JAYLINK_LOG_DOMAIN_MAX_LENGTH bytes,
106 * excluding the trailing null-terminator. A log domain which exceeds this
107 * length will be silently truncated.
109 * @param[in,out] ctx libjaylink context.
110 * @param[in] domain Log domain to use. To set the default log domain, use
111 * #JAYLINK_LOG_DOMAIN_DEFAULT.
113 * @retval JAYLINK_OK Success.
114 * @retval JAYLINK_ERR Other error conditions.
115 * @retval JAYLINK_ERR_ARG Invalid arguments.
117 JAYLINK_API int jaylink_log_set_domain(struct jaylink_context *ctx,
118 char *domain)
120 int ret;
122 if (!ctx || !domain)
123 return JAYLINK_ERR_ARG;
125 ret = snprintf(ctx->log_domain, JAYLINK_LOG_DOMAIN_MAX_LENGTH + 1,
126 "%s", domain);
128 if (ret < 0)
129 return JAYLINK_ERR;
131 return JAYLINK_OK;
135 * Get the libjaylink log domain.
137 * @param[in] ctx libjaylink context.
139 * @return A string which contains the current log domain on success, or NULL
140 * on failure. The string is null-terminated and must not be free'd by
141 * the caller.
143 JAYLINK_API const char *jaylink_log_get_domain(
144 const struct jaylink_context *ctx)
146 if (!ctx)
147 return NULL;
149 return ctx->log_domain;
152 /** @private */
153 JAYLINK_PRIV int log_vprintf(const struct jaylink_context *ctx, int level,
154 const char *format, va_list args, void *user_data)
156 (void)user_data;
159 * Filter out messages with higher verbosity than the verbosity of the
160 * current log level.
162 if (level > ctx->log_level)
163 return 0;
165 if (ctx->log_domain[0] != '\0')
166 fprintf(stderr, "%s", ctx->log_domain);
168 vfprintf(stderr, format, args);
169 fprintf(stderr, "\n");
171 return 0;
174 /** @private */
175 JAYLINK_PRIV void log_err(const struct jaylink_context *ctx,
176 const char *format, ...)
178 va_list args;
180 if (!ctx)
181 return;
183 va_start(args, format);
184 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_ERROR, format, args,
185 ctx->log_callback_data);
186 va_end(args);
189 /** @private */
190 JAYLINK_PRIV void log_warn(const struct jaylink_context *ctx,
191 const char *format, ...)
193 va_list args;
195 if (!ctx)
196 return;
198 va_start(args, format);
199 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_WARNING, format, args,
200 ctx->log_callback_data);
201 va_end(args);
204 /** @private */
205 JAYLINK_PRIV void log_info(const struct jaylink_context *ctx,
206 const char *format, ...)
208 va_list args;
210 if (!ctx)
211 return;
213 va_start(args, format);
214 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_INFO, format, args,
215 ctx->log_callback_data);
216 va_end(args);
219 /** @private */
220 JAYLINK_PRIV void log_dbg(const struct jaylink_context *ctx,
221 const char *format, ...)
223 va_list args;
225 if (!ctx)
226 return;
228 va_start(args, format);
229 ctx->log_callback(ctx, JAYLINK_LOG_LEVEL_DEBUG, format, args,
230 ctx->log_callback_data);
231 va_end(args);