blob: 2873d18db1350c18c537687507e3c546b8343e65 [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
Radek Krejci5fe60cc2015-09-01 17:14:39 +020028#include "config.h"
Radek Krejcia3528022015-09-02 13:46:38 +020029#include "log_p.h"
Radek Krejci5fe60cc2015-09-01 17:14:39 +020030
31/**
32 * @brief libnetconf verbose level variable
33 */
34volatile uint8_t verbose_level = 0;
35
Michal Vasko206d3b12015-12-04 11:08:42 +010036void (*print_clb)(NC_VERB_LEVEL level, const char *msg);
37
Radek Krejci5fe60cc2015-09-01 17:14:39 +020038API void
39nc_verbosity(NC_VERB_LEVEL level)
40{
41 verbose_level = level;
Michal Vaskoa601f5c2015-12-08 14:33:03 +010042 ly_verb(level);
Radek Krejci5fe60cc2015-09-01 17:14:39 +020043}
44
45struct {
46 NC_VERB_LEVEL level;
47 const char *label;
48} verb[] = {
49 {NC_VERB_ERROR, "ERROR"},
50 {NC_VERB_WARNING, "WARNING"},
51 {NC_VERB_VERBOSE, "VERBOSE"},
52 {NC_VERB_DEBUG, "DEBUG"}
53};
54
55static void
56prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args)
57{
58#define PRV_MSG_SIZE 4096
59 char prv_msg[PRV_MSG_SIZE];
60
61 vsnprintf(prv_msg, PRV_MSG_SIZE - 1, format, args);
62 prv_msg[PRV_MSG_SIZE - 1] = '\0';
63
Michal Vasko206d3b12015-12-04 11:08:42 +010064 if (print_clb) {
65 print_clb(level, prv_msg);
66 } else {
67 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
68 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +020069
70#undef PRV_MSG_SIZE
71}
72
73void
74prv_printf(NC_VERB_LEVEL level, const char *format, ...)
75{
76 va_list ap;
77
78 va_start(ap, format);
79 prv_vprintf(level, format, ap);
80 va_end(ap);
81}
82
83API void
Michal Vasko206d3b12015-12-04 11:08:42 +010084nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *))
85{
86 print_clb = clb;
Michal Vaskoa601f5c2015-12-08 14:33:03 +010087 ly_set_log_clb((void (*)(LY_LOG_LEVEL, const char *))clb);
Michal Vasko206d3b12015-12-04 11:08:42 +010088}
89
90API void
Radek Krejci5fe60cc2015-09-01 17:14:39 +020091nc_verb_verbose(const char *format, ...)
92{
93 va_list argptr;
94 if (verbose_level >= NC_VERB_VERBOSE) {
95 va_start(argptr, format);
96 prv_vprintf(NC_VERB_VERBOSE, format, argptr);
97 va_end(argptr);
98 }
99}
100
101API void
102nc_verb_warning(const char *format, ...)
103{
104 va_list argptr;
105
106 if (verbose_level >= NC_VERB_WARNING) {
107 va_start(argptr, format);
108 prv_vprintf(NC_VERB_WARNING, format, argptr);
109 va_end(argptr);
110 }
111}
112
113API void
114nc_verb_error(const char *format, ...)
115{
116 va_list argptr;
117
118 va_start(argptr, format);
119 prv_vprintf(NC_VERB_ERROR, format, argptr);
120 va_end(argptr);
121}