summaryrefslogtreecommitdiffstats
path: root/src/mercury/src/util/mercury_util_error.h
blob: bcf51b70504aef5ca3f12a229cecd82ae0dac553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2013-2020 Argonne National Laboratory, Department of Energy,
 *                    UChicago Argonne, LLC and The HDF Group.
 * All rights reserved.
 *
 * The full copyright notice, including terms governing use, modification,
 * and redistribution, is contained in the COPYING file that can be
 * found at the root of the source code distribution tree.
 */

#ifndef MERCURY_UTIL_ERROR_H
#define MERCURY_UTIL_ERROR_H

#include "mercury_util_config.h"

/* Default error macro */
#include <mercury_log.h>
extern HG_UTIL_PRIVATE HG_LOG_OUTLET_DECL(hg_util);
#define HG_UTIL_LOG_ERROR(...)   HG_LOG_WRITE(hg_util, HG_LOG_LEVEL_ERROR, __VA_ARGS__)
#define HG_UTIL_LOG_WARNING(...) HG_LOG_WRITE(hg_util, HG_LOG_LEVEL_WARNING, __VA_ARGS__)
#ifdef HG_UTIL_HAS_DEBUG
#define HG_UTIL_LOG_DEBUG(...) HG_LOG_WRITE(hg_util, HG_LOG_LEVEL_DEBUG, __VA_ARGS__)
#else
#define HG_UTIL_LOG_DEBUG(...) (void)0
#endif

/* Branch predictor hints */
#ifndef _WIN32
#define likely(x)   __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x)   (x)
#define unlikely(x) (x)
#endif

/* Error macros */
#define HG_UTIL_GOTO_DONE(label, ret, ret_val)                                                               \
    do {                                                                                                     \
        ret = ret_val;                                                                                       \
        goto label;                                                                                          \
    } while (0)

#define HG_UTIL_GOTO_ERROR(label, ret, err_val, ...)                                                         \
    do {                                                                                                     \
        HG_UTIL_LOG_ERROR(__VA_ARGS__);                                                                      \
        ret = err_val;                                                                                       \
        goto label;                                                                                          \
    } while (0)

/* Check for cond, set ret to err_val and goto label */
#define HG_UTIL_CHECK_ERROR(cond, label, ret, err_val, ...)                                                  \
    do {                                                                                                     \
        if (unlikely(cond)) {                                                                                \
            HG_UTIL_LOG_ERROR(__VA_ARGS__);                                                                  \
            ret = err_val;                                                                                   \
            goto label;                                                                                      \
        }                                                                                                    \
    } while (0)

#define HG_UTIL_CHECK_ERROR_NORET(cond, label, ...)                                                          \
    do {                                                                                                     \
        if (unlikely(cond)) {                                                                                \
            HG_UTIL_LOG_ERROR(__VA_ARGS__);                                                                  \
            goto label;                                                                                      \
        }                                                                                                    \
    } while (0)

#define HG_UTIL_CHECK_ERROR_DONE(cond, ...)                                                                  \
    do {                                                                                                     \
        if (unlikely(cond)) {                                                                                \
            HG_UTIL_LOG_ERROR(__VA_ARGS__);                                                                  \
        }                                                                                                    \
    } while (0)

/* Check for cond and print warning */
#define HG_UTIL_CHECK_WARNING(cond, ...)                                                                     \
    do {                                                                                                     \
        if (unlikely(cond)) {                                                                                \
            HG_UTIL_LOG_WARNING(__VA_ARGS__);                                                                \
        }                                                                                                    \
    } while (0)

#endif /* MERCURY_UTIL_ERROR_H */