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