1 /*
2  * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HISTREAMER_FOUNDATION_ERROR_CODE_H
17 #define HISTREAMER_FOUNDATION_ERROR_CODE_H
18 
19 #include <cstdint>
20 
21 #ifdef WIN32
22 // Fix compile error: expected identifier before numeric constant
23 // also can add in plugin_loader.cpp before include log.h
24 #undef ERROR_INVALID_OPERATION
25 #undef ERROR_INVALID_STATE
26 #endif
27 
28 namespace OHOS {
29 namespace Media {
30 enum struct ErrorCode : int32_t {
31     END_OF_STREAM = 1,
32     SUCCESS = 0,
33     ERROR_UNKNOWN = INT32_MIN + 0,
34     ERROR_UNIMPLEMENTED = ERROR_UNKNOWN + 1,
35     ERROR_AGAIN = ERROR_UNKNOWN + 2,
36     ERROR_INVALID_PARAMETER_VALUE = ERROR_UNKNOWN + 3,
37     ERROR_INVALID_PARAMETER_TYPE = ERROR_UNKNOWN + 4,
38     ERROR_INVALID_OPERATION = ERROR_UNKNOWN + 5,
39     ERROR_UNSUPPORTED_FORMAT = ERROR_UNKNOWN + 6,
40     ERROR_NOT_EXISTED = ERROR_UNKNOWN + 7,
41     ERROR_TIMED_OUT = ERROR_UNKNOWN + 8,
42     ERROR_NO_MEMORY = ERROR_UNKNOWN + 9,
43     ERROR_INVALID_STATE = ERROR_UNKNOWN + 10,
44     ERROR_PERMISSION_DENIED = ERROR_UNKNOWN + 11,
45     ERROR_NO_NOTIFY = ERROR_UNKNOWN + 12,
46     ERROR_NULL_POINTER = ERROR_UNKNOWN + 13,
47 };
48 
49 const char* GetErrorName(ErrorCode code);
50 
51 #ifndef FAIL_RETURN
52 #define FAIL_RETURN(exec)                                                                                              \
53     do {                                                                                                               \
54         ErrorCode returnValue = (exec);                                                                                \
55         if (returnValue != ErrorCode::SUCCESS) {                                                                       \
56             MEDIA_LOG_E("FAIL_RETURN on ErrorCode(" PUBLIC_LOG_S ").", GetErrorName(returnValue));                     \
57             return returnValue;                                                                                        \
58         }                                                                                                              \
59     } while (0)
60 #endif
61 
62 #ifndef FAIL_RETURN_MSG_IMPL
63 #define FAIL_RETURN_MSG_IMPL(loglevel, exec, fmt, args...)                                                             \
64     do {                                                                                                               \
65         ErrorCode returnValue = (exec);                                                                                \
66         if (returnValue != ErrorCode::SUCCESS) {                                                                       \
67             loglevel(fmt, ##args);                                                                                     \
68             return returnValue;                                                                                        \
69         }                                                                                                              \
70     } while (0)
71 #endif
72 
73 #ifndef FAIL_RETURN_MSG
74 #define FAIL_RETURN_MSG(exec, fmt, args...) FAIL_RETURN_MSG_IMPL(MEDIA_LOG_E, exec, fmt, ##args)
75 #endif
76 
77 #ifndef FAIL_RETURN_MSG_W
78 #define FAIL_RETURN_MSG_W(exec, fmt, args...) FAIL_RETURN_MSG_IMPL(MEDIA_LOG_W, exec, fmt, ##args)
79 #endif
80 
81 #ifndef FAIL_LOG
82 #define FAIL_LOG(exec)                                                                                                 \
83     do {                                                                                                               \
84         ErrorCode returnValue = (exec);                                                                                \
85         if (returnValue != ErrorCode::SUCCESS) {                                                                       \
86             MEDIA_LOG_E("FAIL_LOG on ErrorCode(" PUBLIC_LOG_S ").", GetErrorName(returnValue));                        \
87         }                                                                                                              \
88     } while (0)
89 #endif
90 
91 #ifndef FAIL_LOG_MSG_IMPL
92 #define FAIL_LOG_MSG_IMPL(loglevel, exec, fmt, args...)                                                                \
93     do {                                                                                                               \
94         ErrorCode returnValue = (exec);                                                                                \
95         if (returnValue != ErrorCode::SUCCESS) {                                                                       \
96             loglevel(fmt, ##args);                                                                                     \
97         }                                                                                                              \
98     } while (0)
99 #endif
100 
101 #ifndef FAIL_LOG_MSG
102 #define FAIL_LOG_MSG(exec, fmt, args...) FAIL_LOG_MSG_IMPL(MEDIA_LOG_E, exec, fmt, ##args)
103 #endif
104 
105 #ifndef FAIL_LOG_MSG_W
106 #define FAIL_LOG_MSG_W(exec, fmt, args...) FAIL_LOG_MSG_IMPL(MEDIA_LOG_W, exec, fmt, ##args)
107 #endif
108 }  // namespace Media
109 }  // namespace OHOS
110 #endif  // HISTREAMER_FOUNDATION_ERROR_CODE_H
111