Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file log.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libnetconf2 - log functions |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 16 | #define _GNU_SOURCE /* pthread_rwlock_t */ |
| 17 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 18 | #include <stdarg.h> |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 21 | |
Michal Vasko | a601f5c | 2015-12-08 14:33:03 +0100 | [diff] [blame] | 22 | #include <libyang/libyang.h> |
| 23 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 24 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 25 | #include <libssh/libssh.h> |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 26 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 27 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 28 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 29 | #include "config.h" |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 30 | #include "log.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 31 | #include "session_p.h" |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * @brief libnetconf verbose level variable |
| 35 | */ |
Václav Kubernát | a4474cf | 2022-01-17 12:37:22 +0100 | [diff] [blame] | 36 | ATOMIC_T verbose_level = 0; |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 37 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 38 | void (*depr_print_clb)(NC_VERB_LEVEL level, const char *msg); |
| 39 | void (*print_clb)(const struct nc_session *session, NC_VERB_LEVEL level, const char *msg); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 40 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 41 | API void |
| 42 | nc_verbosity(NC_VERB_LEVEL level) |
| 43 | { |
Václav Kubernát | a4474cf | 2022-01-17 12:37:22 +0100 | [diff] [blame] | 44 | ATOMIC_STORE_RELAXED(verbose_level, level); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 45 | ly_log_level((LY_LOG_LEVEL)level); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | struct { |
| 49 | NC_VERB_LEVEL level; |
| 50 | const char *label; |
| 51 | } verb[] = { |
bhart | b186eba | 2018-11-12 15:35:09 -0600 | [diff] [blame] | 52 | {NC_VERB_ERROR, "[ERR]"}, |
| 53 | {NC_VERB_WARNING, "[WRN]"}, |
| 54 | {NC_VERB_VERBOSE, "[INF]"}, |
Michal Vasko | 2733aad | 2020-04-16 09:01:52 +0200 | [diff] [blame] | 55 | {NC_VERB_DEBUG, "[DBG]"}, |
| 56 | {NC_VERB_DEBUG_LOWLVL, "[DBL]"} |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 59 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 60 | |
| 61 | API void |
| 62 | nc_libssh_thread_verbosity(int level) |
| 63 | { |
| 64 | ssh_set_log_level(level); |
| 65 | } |
| 66 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 67 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 68 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 69 | static void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 70 | prv_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, va_list args) |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 71 | { |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 72 | #define PRV_MSG_INIT_SIZE 256 |
| 73 | va_list args2; |
| 74 | char *prv_msg; |
| 75 | void *mem; |
| 76 | int req_len; |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 77 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 78 | prv_msg = malloc(PRV_MSG_INIT_SIZE); |
| 79 | if (!prv_msg) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | va_copy(args2, args); |
| 84 | |
| 85 | req_len = vsnprintf(prv_msg, PRV_MSG_INIT_SIZE - 1, format, args); |
| 86 | if (req_len == -1) { |
| 87 | goto cleanup; |
| 88 | } else if (req_len >= PRV_MSG_INIT_SIZE - 1) { |
| 89 | /* the length is not enough */ |
| 90 | ++req_len; |
| 91 | mem = realloc(prv_msg, req_len); |
| 92 | if (!mem) { |
| 93 | goto cleanup; |
| 94 | } |
| 95 | prv_msg = mem; |
| 96 | |
| 97 | /* now print the full message */ |
| 98 | req_len = vsnprintf(prv_msg, req_len, format, args2); |
| 99 | if (req_len == -1) { |
| 100 | goto cleanup; |
| 101 | } |
| 102 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 103 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 104 | if (print_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 105 | print_clb(session, level, prv_msg); |
| 106 | } else if (depr_print_clb) { |
| 107 | depr_print_clb(level, prv_msg); |
| 108 | } else if (session && session->id) { |
| 109 | fprintf(stderr, "Session %u %s: %s\n", session->id, verb[level].label, prv_msg); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 110 | } else { |
| 111 | fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg); |
| 112 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 113 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 114 | cleanup: |
| 115 | free(prv_msg); |
| 116 | #undef PRV_MSG_INIT_SIZE |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 120 | prv_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...) |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 121 | { |
| 122 | va_list ap; |
| 123 | |
| 124 | va_start(ap, format); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 125 | prv_vprintf(session, level, format, ap); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 126 | va_end(ap); |
| 127 | } |
| 128 | |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 129 | static void |
| 130 | nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path)) |
| 131 | { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 132 | if (print_clb) { |
| 133 | print_clb(NULL, (NC_VERB_LEVEL)lvl, msg); |
| 134 | } else if (depr_print_clb) { |
| 135 | depr_print_clb((NC_VERB_LEVEL)lvl, msg); |
| 136 | } |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 139 | API void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 140 | nc_set_print_clb_session(void (*clb)(const struct nc_session *, NC_VERB_LEVEL, const char *)) |
| 141 | { |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 142 | print_clb = clb; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 143 | depr_print_clb = NULL; |
Michal Vasko | 6d8a972 | 2020-02-10 14:56:40 +0100 | [diff] [blame] | 144 | ly_set_log_clb(nc_ly_log_clb, 1); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 145 | } |