Skip to content

Commit 6d80ba6

Browse files
committed
log UPDATE new thread-specific logger callback
1 parent 9ea3790 commit 6d80ba6

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/log.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Michal Vasko <mvasko@cesnet.cz>
55
* @brief Logger routines implementations
66
*
7-
* Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
7+
* Copyright (c) 2015 - 2026 CESNET, z.s.p.o.
88
*
99
* This source code is licensed under BSD 3-Clause License (the "License").
1010
* You may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ ATOMIC_T ly_ll = (uint_fast32_t)LY_LLWRN;
4141
ATOMIC_T ly_log_opts = (uint_fast32_t)(LY_LOLOG | LY_LOSTORE_LAST);
4242
THREAD_LOCAL uint32_t *temp_ly_log_opts;
4343
static ly_log_clb log_clb;
44+
THREAD_LOCAL ly_log_clb temp_log_clb;
4445
THREAD_LOCAL char last_msg[LY_LAST_MSG_SIZE];
4546
#ifndef NDEBUG
4647
ATOMIC_T ly_ldbg_groups = 0;
@@ -300,6 +301,16 @@ ly_get_log_clb(void)
300301
return log_clb;
301302
}
302303

304+
LIBYANG_API_DEF ly_log_clb
305+
ly_temp_log_clb(ly_log_clb clb)
306+
{
307+
ly_log_clb prev_cb = temp_log_clb;
308+
309+
temp_log_clb = clb;
310+
311+
return prev_cb;
312+
}
313+
303314
void
304315
ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode, const char *path, const struct ly_in *in)
305316
{
@@ -582,7 +593,9 @@ log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE
582593

583594
/* if we are only storing errors internally, never print the message (yet) */
584595
if (lolog) {
585-
if (log_clb) {
596+
if (temp_log_clb) {
597+
temp_log_clb(level, msg, data_path, schema_path, line);
598+
} else if (log_clb) {
586599
log_clb(level, msg, data_path, schema_path, line);
587600
} else if (level <= ATOMIC_LOAD_RELAXED(ly_ll)) {
588601
fprintf(stderr, "libyang[%d]: ", level);

src/log.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Michal Vasko <mvasko@cesnet.cz>
55
* @brief Logger manipulation routines and error definitions.
66
*
7-
* Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
7+
* Copyright (c) 2015 - 2026 CESNET, z.s.p.o.
88
*
99
* This source code is licensed under BSD 3-Clause License (the "License").
1010
* You may not use this file except in compliance with the License.
@@ -127,7 +127,7 @@ LIBYANG_API_DECL LY_LOG_LEVEL ly_log_level(LY_LOG_LEVEL level);
127127
*/
128128

129129
/**
130-
* @brief Set logger options. Default is #LY_LOLOG | #LY_LOSTORE_LAST.
130+
* @brief Set global logger options. Default is #LY_LOLOG | #LY_LOSTORE_LAST.
131131
*
132132
* To get the current value, the function must be called twice resetting the level by the received value.
133133
*
@@ -137,7 +137,7 @@ LIBYANG_API_DECL LY_LOG_LEVEL ly_log_level(LY_LOG_LEVEL level);
137137
LIBYANG_API_DECL uint32_t ly_log_options(uint32_t opts);
138138

139139
/**
140-
* @brief Set temporary thread-safe logger options overwriting those set by ::ly_log_options().
140+
* @brief Set temporary thread-safe (thread-specific) logger options overwriting those set by ::ly_log_options().
141141
*
142142
* @param[in] opts Pointer to the temporary @ref logopts. If NULL, restores the effect of global logger options.
143143
* @return Previous temporary options.
@@ -187,7 +187,7 @@ typedef void (*ly_log_clb)(LY_LOG_LEVEL level, const char *msg, const char *data
187187
uint64_t line);
188188

189189
/**
190-
* @brief Set logger callback.
190+
* @brief Set global logger callback.
191191
*
192192
* Is not affected by global log level.
193193
*
@@ -196,12 +196,22 @@ typedef void (*ly_log_clb)(LY_LOG_LEVEL level, const char *msg, const char *data
196196
LIBYANG_API_DECL void ly_set_log_clb(ly_log_clb clb);
197197

198198
/**
199-
* @brief Get logger callback.
199+
* @brief Get global logger callback.
200200
*
201201
* @return Logger callback (can be NULL).
202202
*/
203203
LIBYANG_API_DECL ly_log_clb ly_get_log_clb(void);
204204

205+
/**
206+
* @brief Set temporary thread-safe (thread-specific) logger callback ovewriting that set by ::ly_set_log_clb.
207+
*
208+
* Is not affected by global log level.
209+
*
210+
* @param[in] clb Logging callback.
211+
* @return Previous temporary logging callback.
212+
*/
213+
LIBYANG_API_DECL ly_log_clb ly_temp_log_clb(ly_log_clb clb);
214+
205215
/** @} log */
206216

207217
/**

0 commit comments

Comments
 (0)