Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file common.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief common internal definitions for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef LY_COMMON_H_ |
| 16 | #define LY_COMMON_H_ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "libyang.h" |
Radek Krejci | 2c22f12 | 2018-09-05 15:08:03 +0200 | [diff] [blame] | 22 | #include "config.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 23 | |
| 24 | #if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__ |
| 25 | # define THREAD_LOCAL _Thread_local |
| 26 | #elif defined __GNUC__ || \ |
| 27 | defined __SUNPRO_C || \ |
| 28 | defined __xlC__ |
| 29 | # define THREAD_LOCAL __thread |
| 30 | #else |
| 31 | # error "Cannot define THREAD_LOCAL" |
| 32 | #endif |
| 33 | |
| 34 | #define GETMACRO2(_1, _2, NAME, ...) NAME |
| 35 | #define GETMACRO3(_1, _2, _3, NAME, ...) NAME |
| 36 | #define GETMACRO4(_1, _2, _3, _4, NAME, ...) NAME |
| 37 | |
| 38 | /* |
| 39 | * logger |
| 40 | */ |
| 41 | |
| 42 | /* internal logging options */ |
| 43 | enum int_log_opts { |
| 44 | ILO_LOG = 0, /* log normally */ |
| 45 | ILO_STORE, /* only store any messages, they will be processed higher on stack */ |
| 46 | ILO_IGNORE, /* completely ignore messages */ |
| 47 | ILO_ERR2WRN, /* change errors to warnings */ |
| 48 | }; |
| 49 | |
| 50 | extern THREAD_LOCAL enum int_log_opts log_opt; |
| 51 | extern volatile uint8_t ly_log_level; |
| 52 | extern volatile uint8_t ly_log_opts; |
| 53 | |
| 54 | void ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...); |
| 55 | |
| 56 | #define LOGERR(ctx, errno, str, args...) ly_log(ctx, LY_LLERR, errno, str, ##args) |
| 57 | #define LOGWRN(ctx, str, args...) ly_log(ctx, LY_LLWRN, 0, str, ##args) |
| 58 | #define LOGVRB(str, args...) ly_log(NULL, LY_LLVRB, 0, str, ##args) |
| 59 | |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 60 | #ifdef NDEBUG |
| 61 | # define LOGDBG(dbg_group, str, args...) |
| 62 | #else |
| 63 | void ly_log_dbg(int group, const char *format, ...); |
| 64 | # define LOGDBG(dbg_group, str, args...) ly_log_dbg(dbg_group, str, ##args); |
| 65 | #endif |
| 66 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 67 | #define LOGMEM(CTX) LOGERR(CTX, LY_EMEM, "Memory allocation failed (%s()).", __func__) |
| 68 | #define LOGINT(CTX) LOGERR(CTX, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__) |
| 69 | #define LOGARG(CTX, ARG) LOGERR(CTX, LY_EINVAL, "Invalid argument %s (%s()).", #ARG, __func__) |
| 70 | |
| 71 | /* |
| 72 | * Common code to check return value and perform appropriate action. |
| 73 | */ |
| 74 | #define LY_CHECK_GOTO(COND, GOTO) if (COND) {goto GOTO;} |
| 75 | #define LY_CHECK_ERR_GOTO(COND, ERR, GOTO) if (COND) {ERR; goto GOTO;} |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 76 | #define LY_CHECK_RET(COND, RETVAL) if (COND) {return RETVAL;} |
| 77 | #define LY_CHECK_ERR_RET(COND, ERR, RETVAL) if (COND) {ERR; return RETVAL;} |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 78 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 79 | #define LY_CHECK_ARG_GOTO1(CTX, ARG, GOTO) if (!ARG) {LOGARG(CTX, ARG);goto GOTO;} |
| 80 | #define LY_CHECK_ARG_GOTO2(CTX, ARG1, ARG2, GOTO) LY_CHECK_ARG_GOTO1(CTX, ARG1, GOTO);LY_CHECK_ARG_GOTO1(CTX, ARG2, GOTO) |
| 81 | #define LY_CHECK_ARG_GOTO3(CTX, ARG1, ARG2, ARG3, GOTO) LY_CHECK_ARG_GOTO2(CTX, ARG1, ARG2, GOTO);LY_CHECK_ARG_GOTO1(CTX, ARG3, GOTO) |
| 82 | #define LY_CHECK_ARG_GOTO(CTX, ...) GETMACRO4(__VA_ARGS__, LY_CHECK_ARG_GOTO3, LY_CHECK_ARG_GOTO2, LY_CHECK_ARG_GOTO1)(CTX, __VA_ARGS__) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 83 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 84 | #define LY_CHECK_ARG_RET1(CTX, ARG, RETVAL) if (!ARG) {LOGARG(CTX, ARG);return RETVAL;} |
| 85 | #define LY_CHECK_ARG_RET2(CTX, ARG1, ARG2, RETVAL) LY_CHECK_ARG_RET1(CTX, ARG1, RETVAL);LY_CHECK_ARG_RET1(CTX, ARG2, RETVAL) |
| 86 | #define LY_CHECK_ARG_RET3(CTX, ARG1, ARG2, ARG3, RETVAL) LY_CHECK_ARG_RET2(CTX, ARG1, ARG2, RETVAL);LY_CHECK_ARG_RET1(CTX, ARG3, RETVAL) |
| 87 | #define LY_CHECK_ARG_RET(CTX, ...) GETMACRO4(__VA_ARGS__, LY_CHECK_ARG_RET3, LY_CHECK_ARG_RET2, LY_CHECK_ARG_RET1)(CTX, __VA_ARGS__) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * If the compiler supports attribute to mark objects as hidden, mark all |
| 91 | * objects as hidden and export only objects explicitly marked to be part of |
| 92 | * the public API. |
| 93 | */ |
| 94 | #define API __attribute__((visibility("default"))) |
| 95 | |
| 96 | #endif /* LY_COMMON_H_ */ |