blob: 47fa7fe531d91321dda25886de0adf8577a7633c [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__)
romand82caf12024-03-05 14:21:39 +010052#define ERRINITSRV ERR(NULL, "%s: server not initialized.", __func__)
Michal Vasko05532772021-06-03 12:12:38 +020053#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
romand82caf12024-03-05 14:21:39 +010056#define NC_CHECK_SRV_INIT_RET(RET) if (!ATOMIC_LOAD_RELAXED(server_opts.new_session_id)) {ERRINITSRV; return (RET);}
roman3a95bb22023-10-26 11:07:17 +020057#define NC_CHECK_ERRMEM_RET(COND, RET) if ((COND)) {ERRMEM; return (RET);}
58#define NC_CHECK_ERRMEM_GOTO(COND, RET, GOTO) if ((COND)) {ERRMEM; RET; goto GOTO;}
59
roman40672412023-05-04 11:10:22 +020060#define GETMACRO1(_1, NAME, ...) NAME
61#define GETMACRO2(_1, _2, NAME, ...) NAME
62#define GETMACRO3(_1, _2, _3, NAME, ...) NAME
63#define GETMACRO4(_1, _2, _3, _4, NAME, ...) NAME
64#define GETMACRO5(_1, _2, _3, _4, _5, NAME, ...) NAME
65#define GETMACRO6(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
Roytak40cdba22023-10-04 14:05:47 +020066#define GETMACRO7(_1, _2, _3, _4, _5, _6, _7, NAME, ...) NAME
67#define GETMACRO8(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
roman40672412023-05-04 11:10:22 +020068
69#define NC_CHECK_ARG_RET1(session, ARG, RETVAL) if (!(ARG)) {ERRARG(session, ARG);return RETVAL;}
Roytak40cdba22023-10-04 14:05:47 +020070#define NC_CHECK_ARG_RET2(session, ARG1, ARG2, RETVAL)\
71 NC_CHECK_ARG_RET1(session, ARG1, RETVAL);\
72 NC_CHECK_ARG_RET1(session, ARG2, RETVAL)
73#define NC_CHECK_ARG_RET3(session, ARG1, ARG2, ARG3, RETVAL)\
74 NC_CHECK_ARG_RET2(session, ARG1, ARG2, RETVAL);\
75 NC_CHECK_ARG_RET1(session, ARG3, RETVAL)
76#define NC_CHECK_ARG_RET4(session, ARG1, ARG2, ARG3, ARG4, RETVAL)\
77 NC_CHECK_ARG_RET3(session, ARG1, ARG2, ARG3, RETVAL);\
roman40672412023-05-04 11:10:22 +020078 NC_CHECK_ARG_RET1(session, ARG4, RETVAL)
Roytak40cdba22023-10-04 14:05:47 +020079#define NC_CHECK_ARG_RET5(session, ARG1, ARG2, ARG3, ARG4, ARG5, RETVAL)\
80 NC_CHECK_ARG_RET4(session, ARG1, ARG2, ARG3, ARG4, RETVAL);\
roman40672412023-05-04 11:10:22 +020081 NC_CHECK_ARG_RET1(session, ARG5, RETVAL)
Roytak40cdba22023-10-04 14:05:47 +020082#define NC_CHECK_ARG_RET6(session, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, RETVAL)\
83 NC_CHECK_ARG_RET5(session, ARG1, ARG2, ARG3, ARG4, ARG5, RETVAL);\
84 NC_CHECK_ARG_RET1(session, ARG6, RETVAL)
85#define NC_CHECK_ARG_RET7(session, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, RETVAL)\
86 NC_CHECK_ARG_RET6(session, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, RETVAL);\
87 NC_CHECK_ARG_RET1(session, ARG7, RETVAL)
roman40672412023-05-04 11:10:22 +020088
89/**
90 * @brief Function's parameters checking macro
91 *
92 * @param session Session that is logged.
93 * @param ... Parameters of the function to check. The last parameter is the value that is returned on error.
94 */
Roytak40cdba22023-10-04 14:05:47 +020095#define NC_CHECK_ARG_RET(session, ...) GETMACRO8(__VA_ARGS__, NC_CHECK_ARG_RET7, NC_CHECK_ARG_RET6, NC_CHECK_ARG_RET5,\
96 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 +020097
Radek Krejci5fe60cc2015-09-01 17:14:39 +020098#endif /* NC_LOG_PRIVATE_H_ */