Add build support for OSX.
[libjaylink.git] / libjaylink / log.c
blobca84df9f0790949ccf34fec1267585be5dcf4392
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 3 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 /** @private */
72 static void log_vprintf(struct jaylink_context *ctx, int level,
73 const char *format, va_list args)
76 * Filter out messages with higher verbosity than the verbosity of the
77 * current log level.
79 if (level > ctx->log_level)
80 return;
82 fprintf(stderr, "jaylink: ");
83 vfprintf(stderr, format, args);
84 fprintf(stderr, "\n");
87 /** @private */
88 JAYLINK_PRIV void log_err(struct jaylink_context *ctx, const char *format, ...)
90 va_list args;
92 if (!ctx)
93 return;
95 va_start(args, format);
96 log_vprintf(ctx, JAYLINK_LOG_LEVEL_ERROR, format, args);
97 va_end(args);
100 /** @private */
101 JAYLINK_PRIV void log_warn(struct jaylink_context *ctx, const char *format, ...)
103 va_list args;
105 if (!ctx)
106 return;
108 va_start(args, format);
109 log_vprintf(ctx, JAYLINK_LOG_LEVEL_WARNING, format, args);
110 va_end(args);
113 /** @private */
114 JAYLINK_PRIV void log_info(struct jaylink_context *ctx, const char *format, ...)
116 va_list args;
118 if (!ctx)
119 return;
121 va_start(args, format);
122 log_vprintf(ctx, JAYLINK_LOG_LEVEL_INFO, format, args);
123 va_end(args);
126 /** @private */
127 JAYLINK_PRIV void log_dbg(struct jaylink_context *ctx, const char *format, ...)
129 va_list args;
131 if (!ctx)
132 return;
134 va_start(args, format);
135 log_vprintf(ctx, JAYLINK_LOG_LEVEL_DEBUG, format, args);
136 va_end(args);