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]"}, |
| 48 | {NC_VERB_DEBUG, "[DBG]"} |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 51 | #ifdef NC_ENABLED_SSH |
| 52 | |
| 53 | API void |
| 54 | nc_libssh_thread_verbosity(int level) |
| 55 | { |
| 56 | ssh_set_log_level(level); |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 61 | static void |
| 62 | prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args) |
| 63 | { |
| 64 | #define PRV_MSG_SIZE 4096 |
| 65 | char prv_msg[PRV_MSG_SIZE]; |
| 66 | |
| 67 | vsnprintf(prv_msg, PRV_MSG_SIZE - 1, format, args); |
| 68 | prv_msg[PRV_MSG_SIZE - 1] = '\0'; |
| 69 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 70 | if (print_clb) { |
| 71 | print_clb(level, prv_msg); |
| 72 | } else { |
| 73 | fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg); |
| 74 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 75 | |
| 76 | #undef PRV_MSG_SIZE |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | prv_printf(NC_VERB_LEVEL level, const char *format, ...) |
| 81 | { |
| 82 | va_list ap; |
| 83 | |
| 84 | va_start(ap, format); |
| 85 | prv_vprintf(level, format, ap); |
| 86 | va_end(ap); |
| 87 | } |
| 88 | |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 89 | static void |
| 90 | nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path)) |
| 91 | { |
Radek Krejci | 99087c7 | 2016-02-26 15:03:26 +0100 | [diff] [blame] | 92 | print_clb((NC_VERB_LEVEL)lvl, msg); |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 95 | API void |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 96 | nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *)) |
| 97 | { |
| 98 | print_clb = clb; |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 99 | ly_set_log_clb(nc_ly_log_clb, 0); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 100 | } |