blob: c4f6c9e3e4c49c3d7413d66b2619718cefd4b934 [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
roman3f9b65c2023-06-05 14:26:58 +020019#include <stdarg.h>
Radek Krejcia3528022015-09-02 13:46:38 +020020
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__)
roman40672412023-05-04 11:10:22 +020054#define ERRARG(session, ARG) ERR(session, "Invalid argument %s (%s()).", #ARG, __func__)
55
roman3a95bb22023-10-26 11:07:17 +020056#define NC_CHECK_ERRMEM_RET(COND, RET) if ((COND)) {ERRMEM; return (RET);}
57#define NC_CHECK_ERRMEM_GOTO(COND, RET, GOTO) if ((COND)) {ERRMEM; RET; goto GOTO;}
58
roman40672412023-05-04 11:10:22 +020059#define GETMACRO1(_1, NAME, ...) NAME
60#define GETMACRO2(_1, _2, NAME, ...) NAME
61#define GETMACRO3(_1, _2, _3, NAME, ...) NAME
62#define GETMACRO4(_1, _2, _3, _4, NAME, ...) NAME
63#define GETMACRO5(_1, _2, _3, _4, _5, NAME, ...) NAME
64#define GETMACRO6(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
Roytak40cdba22023-10-04 14:05:47 +020065#define GETMACRO7(_1, _2, _3, _4, _5, _6, _7, NAME, ...) NAME
66#define GETMACRO8(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
roman40672412023-05-04 11:10:22 +020067
68#define NC_CHECK_ARG_RET1(session, ARG, RETVAL) if (!(ARG)) {ERRARG(session, ARG);return RETVAL;}
Roytak40cdba22023-10-04 14:05:47 +020069#define NC_CHECK_ARG_RET2(session, ARG1, ARG2, RETVAL)\
70 NC_CHECK_ARG_RET1(session, ARG1, RETVAL);\
71 NC_CHECK_ARG_RET1(session, ARG2, RETVAL)
72#define NC_CHECK_ARG_RET3(session, ARG1, ARG2, ARG3, RETVAL)\
73 NC_CHECK_ARG_RET2(session, ARG1, ARG2, RETVAL);\
74 NC_CHECK_ARG_RET1(session, ARG3, RETVAL)
75#define NC_CHECK_ARG_RET4(session, ARG1, ARG2, ARG3, ARG4, RETVAL)\
76 NC_CHECK_ARG_RET3(session, ARG1, ARG2, ARG3, RETVAL);\
roman40672412023-05-04 11:10:22 +020077 NC_CHECK_ARG_RET1(session, ARG4, RETVAL)
Roytak40cdba22023-10-04 14:05:47 +020078#define NC_CHECK_ARG_RET5(session, ARG1, ARG2, ARG3, ARG4, ARG5, RETVAL)\
79 NC_CHECK_ARG_RET4(session, ARG1, ARG2, ARG3, ARG4, RETVAL);\
roman40672412023-05-04 11:10:22 +020080 NC_CHECK_ARG_RET1(session, ARG5, RETVAL)
Roytak40cdba22023-10-04 14:05:47 +020081#define NC_CHECK_ARG_RET6(session, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, RETVAL)\
82 NC_CHECK_ARG_RET5(session, ARG1, ARG2, ARG3, ARG4, ARG5, RETVAL);\
83 NC_CHECK_ARG_RET1(session, ARG6, RETVAL)
84#define NC_CHECK_ARG_RET7(session, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, RETVAL)\
85 NC_CHECK_ARG_RET6(session, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, RETVAL);\
86 NC_CHECK_ARG_RET1(session, ARG7, RETVAL)
roman40672412023-05-04 11:10:22 +020087
88/**
89 * @brief Function's parameters checking macro
90 *
91 * @param session Session that is logged.
92 * @param ... Parameters of the function to check. The last parameter is the value that is returned on error.
93 */
Roytak40cdba22023-10-04 14:05:47 +020094#define NC_CHECK_ARG_RET(session, ...) GETMACRO8(__VA_ARGS__, NC_CHECK_ARG_RET7, NC_CHECK_ARG_RET6, NC_CHECK_ARG_RET5,\
95 NC_CHECK_ARG_RET4, NC_CHECK_ARG_RET3, NC_CHECK_ARG_RET2, NC_CHECK_ARG_RET1, DUMMY) (session, __VA_ARGS__)
Radek Krejci52b37022015-10-06 16:11:48 +020096
Radek Krejci5fe60cc2015-09-01 17:14:39 +020097#endif /* NC_LOG_PRIVATE_H_ */