blob: 3cc7b98f597e0b58bf7791260186feea185d5820 [file] [log] [blame]
Radek Krejci5fe60cc2015-09-01 17:14:39 +02001/**
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 Krejci5fe60cc2015-09-01 17:14:39 +020024#include <stdio.h>
25
Michal Vaskoa601f5c2015-12-08 14:33:03 +010026#include <libyang/libyang.h>
27
Michal Vasko1a38c862016-01-15 15:50:07 +010028#include "libnetconf.h"
Radek Krejci5fe60cc2015-09-01 17:14:39 +020029
30/**
31 * @brief libnetconf verbose level variable
32 */
33volatile uint8_t verbose_level = 0;
34
Michal Vasko206d3b12015-12-04 11:08:42 +010035void (*print_clb)(NC_VERB_LEVEL level, const char *msg);
36
Radek Krejci5fe60cc2015-09-01 17:14:39 +020037API void
38nc_verbosity(NC_VERB_LEVEL level)
39{
40 verbose_level = level;
Michal Vaskocefedf22016-01-20 10:08:12 +010041 ly_verb((LY_LOG_LEVEL)level);
Radek Krejci5fe60cc2015-09-01 17:14:39 +020042}
43
44struct {
45 NC_VERB_LEVEL level;
46 const char *label;
47} verb[] = {
48 {NC_VERB_ERROR, "ERROR"},
49 {NC_VERB_WARNING, "WARNING"},
50 {NC_VERB_VERBOSE, "VERBOSE"},
51 {NC_VERB_DEBUG, "DEBUG"}
52};
53
54static void
55prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args)
56{
57#define PRV_MSG_SIZE 4096
58 char prv_msg[PRV_MSG_SIZE];
59
60 vsnprintf(prv_msg, PRV_MSG_SIZE - 1, format, args);
61 prv_msg[PRV_MSG_SIZE - 1] = '\0';
62
Michal Vasko206d3b12015-12-04 11:08:42 +010063 if (print_clb) {
64 print_clb(level, prv_msg);
65 } else {
66 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
67 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +020068
69#undef PRV_MSG_SIZE
70}
71
72void
73prv_printf(NC_VERB_LEVEL level, const char *format, ...)
74{
75 va_list ap;
76
77 va_start(ap, format);
78 prv_vprintf(level, format, ap);
79 va_end(ap);
80}
81
82API void
Michal Vasko206d3b12015-12-04 11:08:42 +010083nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *))
84{
85 print_clb = clb;
Michal Vaskoa601f5c2015-12-08 14:33:03 +010086 ly_set_log_clb((void (*)(LY_LOG_LEVEL, const char *))clb);
Michal Vasko206d3b12015-12-04 11:08:42 +010087}