blob: 4ea31e67aa04df2a8015f4c3b2da8c3aa8a761b8 [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>
24#include <stdint.h>
25#include <stdio.h>
26
27#include "config.h"
28#include "log.h"
29
30/**
31 * @brief libnetconf verbose level variable
32 */
33volatile uint8_t verbose_level = 0;
34
35API void
36nc_verbosity(NC_VERB_LEVEL level)
37{
38 verbose_level = level;
39}
40
41struct {
42 NC_VERB_LEVEL level;
43 const char *label;
44} verb[] = {
45 {NC_VERB_ERROR, "ERROR"},
46 {NC_VERB_WARNING, "WARNING"},
47 {NC_VERB_VERBOSE, "VERBOSE"},
48 {NC_VERB_DEBUG, "DEBUG"}
49};
50
51static void
52prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args)
53{
54#define PRV_MSG_SIZE 4096
55 char prv_msg[PRV_MSG_SIZE];
56
57 vsnprintf(prv_msg, PRV_MSG_SIZE - 1, format, args);
58 prv_msg[PRV_MSG_SIZE - 1] = '\0';
59
60 /* TODO: allow to set printer via callbacks */
61 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
62
63#undef PRV_MSG_SIZE
64}
65
66void
67prv_printf(NC_VERB_LEVEL level, const char *format, ...)
68{
69 va_list ap;
70
71 va_start(ap, format);
72 prv_vprintf(level, format, ap);
73 va_end(ap);
74}
75
76API void
77nc_verb_verbose(const char *format, ...)
78{
79 va_list argptr;
80 if (verbose_level >= NC_VERB_VERBOSE) {
81 va_start(argptr, format);
82 prv_vprintf(NC_VERB_VERBOSE, format, argptr);
83 va_end(argptr);
84 }
85}
86
87API void
88nc_verb_warning(const char *format, ...)
89{
90 va_list argptr;
91
92 if (verbose_level >= NC_VERB_WARNING) {
93 va_start(argptr, format);
94 prv_vprintf(NC_VERB_WARNING, format, argptr);
95 va_end(argptr);
96 }
97}
98
99API void
100nc_verb_error(const char *format, ...)
101{
102 va_list argptr;
103
104 va_start(argptr, format);
105 prv_vprintf(NC_VERB_ERROR, format, argptr);
106 va_end(argptr);
107}