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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdarg.h> |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | |
Michal Vasko | a601f5c | 2015-12-08 14:33:03 +0100 | [diff] [blame] | 26 | #include <libyang/libyang.h> |
| 27 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 28 | #include "libnetconf.h" |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * @brief libnetconf verbose level variable |
| 32 | */ |
| 33 | volatile uint8_t verbose_level = 0; |
| 34 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 35 | void (*print_clb)(NC_VERB_LEVEL level, const char *msg); |
| 36 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 37 | API void |
| 38 | nc_verbosity(NC_VERB_LEVEL level) |
| 39 | { |
| 40 | verbose_level = level; |
Michal Vasko | cefedf2 | 2016-01-20 10:08:12 +0100 | [diff] [blame] | 41 | ly_verb((LY_LOG_LEVEL)level); |
Michal Vasko | c6356ea | 2016-02-03 15:29:09 +0100 | [diff] [blame] | 42 | #ifdef ENABLE_SSH |
| 43 | ssh_set_log_level(level); |
| 44 | #endif |
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[] = { |
| 51 | {NC_VERB_ERROR, "ERROR"}, |
| 52 | {NC_VERB_WARNING, "WARNING"}, |
| 53 | {NC_VERB_VERBOSE, "VERBOSE"}, |
| 54 | {NC_VERB_DEBUG, "DEBUG"} |
| 55 | }; |
| 56 | |
| 57 | static void |
| 58 | prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args) |
| 59 | { |
| 60 | #define PRV_MSG_SIZE 4096 |
| 61 | char prv_msg[PRV_MSG_SIZE]; |
| 62 | |
| 63 | vsnprintf(prv_msg, PRV_MSG_SIZE - 1, format, args); |
| 64 | prv_msg[PRV_MSG_SIZE - 1] = '\0'; |
| 65 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 66 | if (print_clb) { |
| 67 | print_clb(level, prv_msg); |
| 68 | } else { |
| 69 | fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg); |
| 70 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 71 | |
| 72 | #undef PRV_MSG_SIZE |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | prv_printf(NC_VERB_LEVEL level, const char *format, ...) |
| 77 | { |
| 78 | va_list ap; |
| 79 | |
| 80 | va_start(ap, format); |
| 81 | prv_vprintf(level, format, ap); |
| 82 | va_end(ap); |
| 83 | } |
| 84 | |
| 85 | API void |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 86 | nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *)) |
| 87 | { |
| 88 | print_clb = clb; |
Michal Vasko | a601f5c | 2015-12-08 14:33:03 +0100 | [diff] [blame] | 89 | ly_set_log_clb((void (*)(LY_LOG_LEVEL, const char *))clb); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 90 | } |