Diligent Engine  v.2.4.g
Errors.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
30 #include <stdexcept>
31 #include <string>
32 #include <iostream>
33 
34 #include "DebugOutput.h"
35 #include "FormatString.hpp"
36 
37 namespace Diligent
38 {
39 
40 template <bool>
41 void ThrowIf(std::string&&)
42 {
43 }
44 
45 template <>
46 inline void ThrowIf<true>(std::string&& msg)
47 {
48  throw std::runtime_error(std::move(msg));
49 }
50 
51 template <bool bThrowException, typename... ArgsType>
52 void LogError(bool IsFatal, const char* Function, const char* FullFilePath, int Line, const ArgsType&... Args)
53 {
54  std::string FileName(FullFilePath);
55 
56  auto LastSlashPos = FileName.find_last_of("/\\");
57  if (LastSlashPos != std::string::npos)
58  FileName.erase(0, LastSlashPos + 1);
59  auto Msg = FormatString(Args...);
60  if (DebugMessageCallback != nullptr)
61  {
62  DebugMessageCallback(IsFatal ? DEBUG_MESSAGE_SEVERITY_FATAL_ERROR : DEBUG_MESSAGE_SEVERITY_ERROR, Msg.c_str(), Function, FileName.c_str(), Line);
63  }
64  else
65  {
66  // No callback set - output to cerr
67  std::cerr << "Diligent Engine: " << (IsFatal ? "Fatal Error" : "Error") << " in " << Function << "() (" << FileName << ", " << Line << "): " << Msg << '\n';
68  }
69  ThrowIf<bThrowException>(std::move(Msg));
70 }
71 
72 } // namespace Diligent
73 
74 
75 
76 #define LOG_ERROR(...) \
77  do \
78  { \
79  Diligent::LogError<false>(/*IsFatal=*/false, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
80  } while (false)
81 
82 
83 #define LOG_FATAL_ERROR(...) \
84  do \
85  { \
86  Diligent::LogError<false>(/*IsFatal=*/true, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
87  } while (false)
88 
89 #define LOG_ERROR_ONCE(...) \
90  do \
91  { \
92  static bool IsFirstTime = true; \
93  if (IsFirstTime) \
94  { \
95  LOG_ERROR(##__VA_ARGS__); \
96  IsFirstTime = false; \
97  } \
98  } while (false)
99 
100 
101 #define LOG_ERROR_AND_THROW(...) \
102  do \
103  { \
104  Diligent::LogError<true>(/*IsFatal=*/false, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
105  } while (false)
106 
107 #define LOG_FATAL_ERROR_AND_THROW(...) \
108  do \
109  { \
110  Diligent::LogError<true>(/*IsFatal=*/true, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
111  } while (false)
112 
113 
114 #define LOG_DEBUG_MESSAGE(Severity, ...) \
115  do \
116  { \
117  auto _msg = Diligent::FormatString(__VA_ARGS__); \
118  if (Diligent::DebugMessageCallback != nullptr) Diligent::DebugMessageCallback(Severity, _msg.c_str(), nullptr, nullptr, 0); \
119  } while (false)
120 
121 #define LOG_FATAL_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_FATAL_ERROR, ##__VA_ARGS__)
122 #define LOG_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_ERROR, ##__VA_ARGS__)
123 #define LOG_WARNING_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_WARNING, ##__VA_ARGS__)
124 #define LOG_INFO_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DEBUG_MESSAGE_SEVERITY_INFO, ##__VA_ARGS__)
125 
126 
127 #define LOG_DEBUG_MESSAGE_ONCE(Severity, ...) \
128  do \
129  { \
130  static bool IsFirstTime = true; \
131  if (IsFirstTime) \
132  { \
133  LOG_DEBUG_MESSAGE(Severity, ##__VA_ARGS__); \
134  IsFirstTime = false; \
135  } \
136  } while (false)
137 
138 #define LOG_FATAL_ERROR_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_FATAL_ERROR, ##__VA_ARGS__)
139 #define LOG_ERROR_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_ERROR, ##__VA_ARGS__)
140 #define LOG_WARNING_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_WARNING, ##__VA_ARGS__)
141 #define LOG_INFO_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(Diligent::DEBUG_MESSAGE_SEVERITY_INFO, ##__VA_ARGS__)
142 
143 
144 #define CHECK(Expr, Severity, ...) \
145  do \
146  { \
147  if (!(Expr)) \
148  { \
149  LOG_DEBUG_MESSAGE(Severity, ##__VA_ARGS__); \
150  } \
151  } while (false)
152 
153 #define CHECK_FATAL_ERR(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_FATAL_ERROR, ##__VA_ARGS__)
154 #define CHECK_ERR(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_ERROR, ##__VA_ARGS__)
155 #define CHECK_WARN(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_WARNING, ##__VA_ARGS__)
156 #define CHECK_INFO(Expr, ...) CHECK(Expr, Diligent::DEBUG_MESSAGE_SEVERITY_INFO, ##__VA_ARGS__)
157 
158 #define CHECK_THROW(Expr, ...) \
159  do \
160  { \
161  if (!(Expr)) \
162  { \
163  LOG_ERROR_AND_THROW(##__VA_ARGS__); \
164  } \
165  } while (false)
Diligent::DEBUG_MESSAGE_SEVERITY_FATAL_ERROR
@ DEBUG_MESSAGE_SEVERITY_FATAL_ERROR
Fatal error - recovery is not possible.
Definition: DebugOutput.h:47
Diligent::LogError
void LogError(bool IsFatal, const char *Function, const char *FullFilePath, int Line, const ArgsType &... Args)
Definition: Errors.hpp:52
Diligent::DebugMessageCallback
DebugMessageCallbackType DebugMessageCallback
Definition: AndroidDebug.cpp:60
Diligent::DEBUG_MESSAGE_SEVERITY_ERROR
@ DEBUG_MESSAGE_SEVERITY_ERROR
Error, with potential recovery.
Definition: DebugOutput.h:44
FormatString.hpp
DebugOutput.h
Diligent::ThrowIf< true >
void ThrowIf< true >(std::string &&msg)
Definition: Errors.hpp:46
Diligent::ThrowIf
void ThrowIf(std::string &&)
Definition: Errors.hpp:41
Diligent::FormatString
std::string FormatString(const RestArgsType &... Args)
Definition: FormatString.hpp:55
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37