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 (*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] | 39 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 40 | API void |
| 41 | nc_verbosity(NC_VERB_LEVEL level) |
| 42 | { |
Václav Kubernát | a4474cf | 2022-01-17 12:37:22 +0100 | [diff] [blame] | 43 | ATOMIC_STORE_RELAXED(verbose_level, level); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 44 | ly_log_level((LY_LOG_LEVEL)level); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | struct { |
| 48 | NC_VERB_LEVEL level; |
| 49 | const char *label; |
| 50 | } verb[] = { |
bhart | b186eba | 2018-11-12 15:35:09 -0600 | [diff] [blame] | 51 | {NC_VERB_ERROR, "[ERR]"}, |
| 52 | {NC_VERB_WARNING, "[WRN]"}, |
| 53 | {NC_VERB_VERBOSE, "[INF]"}, |
Michal Vasko | 2733aad | 2020-04-16 09:01:52 +0200 | [diff] [blame] | 54 | {NC_VERB_DEBUG, "[DBG]"}, |
| 55 | {NC_VERB_DEBUG_LOWLVL, "[DBL]"} |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 58 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 59 | |
| 60 | API void |
| 61 | nc_libssh_thread_verbosity(int level) |
| 62 | { |
| 63 | ssh_set_log_level(level); |
| 64 | } |
| 65 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 66 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 67 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 68 | static void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 69 | 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] | 70 | { |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 71 | #define PRV_MSG_INIT_SIZE 256 |
| 72 | va_list args2; |
| 73 | char *prv_msg; |
| 74 | void *mem; |
| 75 | int req_len; |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 76 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 77 | prv_msg = malloc(PRV_MSG_INIT_SIZE); |
| 78 | if (!prv_msg) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | va_copy(args2, args); |
| 83 | |
| 84 | req_len = vsnprintf(prv_msg, PRV_MSG_INIT_SIZE - 1, format, args); |
| 85 | if (req_len == -1) { |
| 86 | goto cleanup; |
| 87 | } else if (req_len >= PRV_MSG_INIT_SIZE - 1) { |
| 88 | /* the length is not enough */ |
| 89 | ++req_len; |
| 90 | mem = realloc(prv_msg, req_len); |
| 91 | if (!mem) { |
| 92 | goto cleanup; |
| 93 | } |
| 94 | prv_msg = mem; |
| 95 | |
| 96 | /* now print the full message */ |
| 97 | req_len = vsnprintf(prv_msg, req_len, format, args2); |
| 98 | if (req_len == -1) { |
| 99 | goto cleanup; |
| 100 | } |
| 101 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 102 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 103 | if (print_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 104 | print_clb(session, level, prv_msg); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 105 | } else if (session && session->id) { |
Michal Vasko | 1637471 | 2024-04-26 14:13:00 +0200 | [diff] [blame] | 106 | fprintf(stderr, "Session %" PRIu32 " %s: %s\n", session->id, verb[level].label, prv_msg); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 107 | } else { |
| 108 | fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg); |
| 109 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 110 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 111 | cleanup: |
| 112 | free(prv_msg); |
| 113 | #undef PRV_MSG_INIT_SIZE |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 117 | 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] | 118 | { |
| 119 | va_list ap; |
| 120 | |
| 121 | va_start(ap, format); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 122 | prv_vprintf(session, level, format, ap); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 123 | va_end(ap); |
| 124 | } |
| 125 | |
| 126 | API void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 127 | nc_set_print_clb_session(void (*clb)(const struct nc_session *, NC_VERB_LEVEL, const char *)) |
| 128 | { |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 129 | print_clb = clb; |
| 130 | } |