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 | |
| 26 | #include "config.h" |
Radek Krejci | a352802 | 2015-09-02 13:46:38 +0200 | [diff] [blame] | 27 | #include "log_p.h" |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * @brief libnetconf verbose level variable |
| 31 | */ |
| 32 | volatile uint8_t verbose_level = 0; |
| 33 | |
| 34 | API void |
| 35 | nc_verbosity(NC_VERB_LEVEL level) |
| 36 | { |
| 37 | verbose_level = level; |
| 38 | } |
| 39 | |
| 40 | struct { |
| 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 | |
| 50 | static void |
| 51 | prv_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 | |
| 65 | void |
| 66 | prv_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 | |
| 75 | API void |
| 76 | nc_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 | |
| 86 | API void |
| 87 | nc_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 | |
| 98 | API void |
| 99 | nc_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 | } |