blob: 8bd57c0b31522b3a509e8a7c526ec57386c89bd4 [file] [log] [blame]
Radek Krejci5fe60cc2015-09-01 17:14:39 +02001/**
Michal Vaskoc446a382021-06-18 08:54:05 +02002 * @file log.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libnetconf2 logger
Radek Krejci5fe60cc2015-09-01 17:14:39 +02005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
Michal Vaskoc446a382021-06-18 08:54:05 +02007 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Radek Krejci5fe60cc2015-09-01 17:14:39 +02008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci5fe60cc2015-09-01 17:14:39 +020014 */
15
16#ifndef NC_LOG_PRIVATE_H_
17#define NC_LOG_PRIVATE_H_
18
Radek Krejcia3528022015-09-02 13:46:38 +020019#include <stdint.h>
20
Václav Kubernáta4474cf2022-01-17 12:37:22 +010021#include "compat.h"
Radek Krejcia3528022015-09-02 13:46:38 +020022#include "log.h"
23
Radek Krejci5fe60cc2015-09-01 17:14:39 +020024/*
25 * libnetconf's message printing
26 */
27
28/**
Michal Vasko05532772021-06-03 12:12:38 +020029 * @brief Internal printing function
30 *
31 * @param[in] session Optional NETCONF session that generated the message
Radek Krejci5fe60cc2015-09-01 17:14:39 +020032 * @param[in] level Verbose level
33 * @param[in] format Formatting string
34 */
Michal Vasko05532772021-06-03 12:12:38 +020035void prv_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...);
Radek Krejci5fe60cc2015-09-01 17:14:39 +020036
37/**
38 * @brief Verbose level variable
39 */
Václav Kubernáta4474cf2022-01-17 12:37:22 +010040extern ATOMIC_T verbose_level;
Radek Krejci5fe60cc2015-09-01 17:14:39 +020041
42/*
43 * Verbose printing macros
44 */
roman40672412023-05-04 11:10:22 +020045#define ERR(session, ...) prv_printf(session, NC_VERB_ERROR, __VA_ARGS__)
46#define WRN(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_WARNING){prv_printf(session, NC_VERB_WARNING, __VA_ARGS__);}
47#define VRB(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_VERBOSE){prv_printf(session, NC_VERB_VERBOSE, __VA_ARGS__);}
48#define DBG(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_DEBUG){prv_printf(session, NC_VERB_DEBUG, __VA_ARGS__);}
49#define DBL(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_DEBUG_LOWLVL){prv_printf(session, NC_VERB_DEBUG_LOWLVL, __VA_ARGS__);}
Radek Krejci5fe60cc2015-09-01 17:14:39 +020050
Michal Vasko05532772021-06-03 12:12:38 +020051#define ERRMEM ERR(NULL, "%s: memory reallocation failed (%s:%d).", __func__, __FILE__, __LINE__)
Michal Vasko05532772021-06-03 12:12:38 +020052#define ERRINIT ERR(NULL, "%s: libnetconf2 not initialized.", __func__)
53#define ERRINT ERR(NULL, "%s: internal error (%s:%d).", __func__, __FILE__, __LINE__)
romanc1d2b092023-02-02 08:58:27 +010054#define ERRNODE(name) ERR(NULL, "%s: missing node (%s) in the YANG data.", __func__, name)
roman40672412023-05-04 11:10:22 +020055#define ERRARG(session, ARG) ERR(session, "Invalid argument %s (%s()).", #ARG, __func__)
56
57#define GETMACRO1(_1, NAME, ...) NAME
58#define GETMACRO2(_1, _2, NAME, ...) NAME
59#define GETMACRO3(_1, _2, _3, NAME, ...) NAME
60#define GETMACRO4(_1, _2, _3, _4, NAME, ...) NAME
61#define GETMACRO5(_1, _2, _3, _4, _5, NAME, ...) NAME
62#define GETMACRO6(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
63
64#define NC_CHECK_ARG_RET1(session, ARG, RETVAL) if (!(ARG)) {ERRARG(session, ARG);return RETVAL;}
65#define NC_CHECK_ARG_RET2(session, ARG1, ARG2, RETVAL) NC_CHECK_ARG_RET1(session, ARG1, RETVAL);NC_CHECK_ARG_RET1(session, ARG2, RETVAL)
66#define NC_CHECK_ARG_RET3(session, ARG1, ARG2, ARG3, RETVAL) NC_CHECK_ARG_RET2(session, ARG1, ARG2, RETVAL);NC_CHECK_ARG_RET1(session, ARG3, RETVAL)
67#define NC_CHECK_ARG_RET4(session, ARG1, ARG2, ARG3, ARG4, RETVAL) NC_CHECK_ARG_RET3(session, ARG1, ARG2, ARG3, RETVAL);\
68 NC_CHECK_ARG_RET1(session, ARG4, RETVAL)
69#define NC_CHECK_ARG_RET5(session, ARG1, ARG2, ARG3, ARG4, ARG5, RETVAL) NC_CHECK_ARG_RET4(session, ARG1, ARG2, ARG3, ARG4, RETVAL);\
70 NC_CHECK_ARG_RET1(session, ARG5, RETVAL)
71
72/**
73 * @brief Function's parameters checking macro
74 *
75 * @param session Session that is logged.
76 * @param ... Parameters of the function to check. The last parameter is the value that is returned on error.
77 */
78#define NC_CHECK_ARG_RET(session, ...) GETMACRO6(__VA_ARGS__, NC_CHECK_ARG_RET5, NC_CHECK_ARG_RET4, NC_CHECK_ARG_RET3, \
79 NC_CHECK_ARG_RET2, NC_CHECK_ARG_RET1, DUMMY) (session, __VA_ARGS__)
Radek Krejci52b37022015-10-06 16:11:48 +020080
Radek Krejci5fe60cc2015-09-01 17:14:39 +020081#endif /* NC_LOG_PRIVATE_H_ */