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/>.
23 #include "libjaylink.h"
24 #include "libjaylink-internal.h"
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
)
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
;
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
)
66 return JAYLINK_ERR_ARG
;
68 return ctx
->log_level
;
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
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
)
86 return JAYLINK_ERR_ARG
;
89 ctx
->log_callback
= callback
;
90 ctx
->log_callback_data
= user_data
;
92 ctx
->log_callback
= &log_vprintf
;
93 ctx
->log_callback_data
= NULL
;
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
,
123 return JAYLINK_ERR_ARG
;
125 ret
= snprintf(ctx
->log_domain
, JAYLINK_LOG_DOMAIN_MAX_LENGTH
+ 1,
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
143 JAYLINK_API
const char *jaylink_log_get_domain(
144 const struct jaylink_context
*ctx
)
149 return ctx
->log_domain
;
153 JAYLINK_PRIV
int log_vprintf(const struct jaylink_context
*ctx
, int level
,
154 const char *format
, va_list args
, void *user_data
)
159 * Filter out messages with higher verbosity than the verbosity of the
162 if (level
> ctx
->log_level
)
165 if (ctx
->log_domain
[0] != '\0')
166 fprintf(stderr
, "%s", ctx
->log_domain
);
168 vfprintf(stderr
, format
, args
);
169 fprintf(stderr
, "\n");
175 JAYLINK_PRIV
void log_err(const struct jaylink_context
*ctx
,
176 const char *format
, ...)
183 va_start(args
, format
);
184 ctx
->log_callback(ctx
, JAYLINK_LOG_LEVEL_ERROR
, format
, args
,
185 ctx
->log_callback_data
);
190 JAYLINK_PRIV
void log_warn(const struct jaylink_context
*ctx
,
191 const char *format
, ...)
198 va_start(args
, format
);
199 ctx
->log_callback(ctx
, JAYLINK_LOG_LEVEL_WARNING
, format
, args
,
200 ctx
->log_callback_data
);
205 JAYLINK_PRIV
void log_info(const struct jaylink_context
*ctx
,
206 const char *format
, ...)
213 va_start(args
, format
);
214 ctx
->log_callback(ctx
, JAYLINK_LOG_LEVEL_INFO
, format
, args
,
215 ctx
->log_callback_data
);
220 JAYLINK_PRIV
void log_dbg(const struct jaylink_context
*ctx
,
221 const char *format
, ...)
228 va_start(args
, format
);
229 ctx
->log_callback(ctx
, JAYLINK_LOG_LEVEL_DEBUG
, format
, args
,
230 ctx
->log_callback_data
);