summaryrefslogtreecommitdiff
path: root/common/aitt_internal.h
blob: feef42b17cfed8601b07a54a23029bca580194f2 (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
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (c) 2021-2022 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <libgen.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <cassert>
#include <cerrno>
#include <cstring>

#include "aitt_internal_definitions.h"
#include "aitt_platform.h"

#if defined(SYS_gettid)
#define GETTID() syscall(SYS_gettid)
#else  // SYS_gettid
#define GETTID() 0lu
#endif  // SYS_gettid

#define AITT_ERRMSG_LEN 80

#if (_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE
#define AITT_STRERROR_R(errno, buf, buflen)          \
    do {                                             \
        int ret = strerror_r(errno, buf, buflen);    \
        if (ret != 0) {                              \
            assert(ret == 0 && "strerror_r failed"); \
        }                                            \
    } while (0)
#else
#define AITT_STRERROR_R(errno, buf, buflen)                   \
    do {                                                      \
        const char *errstr = strerror_r(errno, buf, buflen);  \
        if (errstr == nullptr) {                              \
            assert(errstr != nullptr && "strerror_r failed"); \
        }                                                     \
    } while (0)
#endif

#ifdef _LOG_WITH_TIMESTAMP
#include "aitt_internal_profiling.h"
#else
#define DBG(fmt, ...) PLATFORM_LOGD("[%lu] " fmt, GETTID(), ##__VA_ARGS__)

#define INFO(fmt, ...) PLATFORM_LOGI("[%lu] " fmt, GETTID(), ##__VA_ARGS__)
#define ERR(fmt, ...) PLATFORM_LOGE("[%lu] \033[31m" fmt "\033[0m", GETTID(), ##__VA_ARGS__)
#define ERR_CODE(_aitt_errno, fmt, ...)                                                 \
    do {                                                                                \
        char errMsg[AITT_ERRMSG_LEN] = {'\0'};                                          \
        int _errno = (_aitt_errno);                                                     \
        AITT_STRERROR_R(_errno, errMsg, sizeof(errMsg));                                \
        PLATFORM_LOGE("[%lu] (%d:%s) \033[31m" fmt "\033[0m", GETTID(), _errno, errMsg, \
              ##__VA_ARGS__);                                                           \
    } while (0)
#endif

#define RET_IF(expr)            \
    do {                        \
        if (expr) {             \
            ERR("(%s)", #expr); \
            return;             \
        }                       \
    } while (0)

#define RETV_IF(expr, val)      \
    do {                        \
        if (expr) {             \
            ERR("(%s)", #expr); \
            return (val);       \
        }                       \
    } while (0)

#define RETVM_IF(expr, val, fmt, arg...) \
    do {                                 \
        if (expr) {                      \
            ERR(fmt, ##arg);             \
            return (val);                \
        }                                \
    } while (0)