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