Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/Platforms/Basic/interface/Errors.h Source File

Diligent Engine API Reference

Errors.h
1 /* Copyright 2015-2018 Egor Yusov
2 *
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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12 *
13 * In no event and under no legal theory, whether in tort (including negligence),
14 * contract, or otherwise, unless required by applicable law (such as deliberate
15 * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16 * liable for any damages, including any direct, indirect, special, incidental,
17 * or consequential damages of any character arising as a result of this License or
18 * out of the use or inability to use the software (including but not limited to damages
19 * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20 * all other commercial damages or losses), even if such Contributor has been advised
21 * of the possibility of such damages.
22 */
23
24 #pragma once
25
26 #include <stdexcept>
27
28 #include "BasicPlatformDebug.h"
29 #include "FormatMessage.h"
30 #include "BasicFileSystem.h"
31
32 template<bool>
33 void ThrowIf(std::string &&)
34 {
35 }
36
37 template<>
38 inline void ThrowIf<true>(std::string &&msg)
39 {
40 throw std::runtime_error( std::move(msg) );
41 }
42
43 template<bool bThrowException, typename FirstArgType, typename... RestArgsType>
44 void LogError( const char *strFunctionName, const char *strFullFilePath, int Line, const FirstArgType& first, const RestArgsType&... RestArgs )
45 {
46 std::string FileName;
47 BasicFileSystem::SplitFilePath( strFullFilePath, nullptr, &FileName );
48 Diligent::MsgStream ss;
49 ss << "The following error occured in the " << strFunctionName << "() function (" << FileName << ", line " << Line << "):\n";
50 Diligent::FormatMsg( ss, first, RestArgs... );
51 auto strFullMessage = ss.str();
52 OutputDebugMessage( bThrowException ? BasicPlatformDebug::DebugMessageSeverity::FatalError : BasicPlatformDebug::DebugMessageSeverity::Error, strFullMessage.c_str() );
53 ThrowIf<bThrowException>(std::move(strFullMessage));
54 }
55
56 #define LOG_ERROR(...)\
57 do{ \
58 LogError<false>(__FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
59 }while(false)
60
61 #define LOG_ERROR_ONCE(...)\
62 do{ \
63 static bool IsFirstTime = true; \
64 if(IsFirstTime) \
65 { \
66 LogError<false>(__FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
67 IsFirstTime = false; \
68 } \
69 }while(false)
70
71 #define LOG_ERROR_AND_THROW(...)\
72 do{ \
73 LogError<true>(__FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__);\
74 }while(false)
75
76 #define LOG_DEBUG_MESSAGE(Severity, ...)\
77 do{ \
78 Diligent::MsgStream ss; \
79 Diligent::FormatMsg( ss, ##__VA_ARGS__ );\
80 OutputDebugMessage( Severity, ss.str().c_str() );\
81 }while(false)
82
83 #define LOG_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(BasicPlatformDebug::DebugMessageSeverity::Error, ##__VA_ARGS__)
84 #define LOG_WARNING_MESSAGE(...) LOG_DEBUG_MESSAGE(BasicPlatformDebug::DebugMessageSeverity::Warning, ##__VA_ARGS__)
85 #define LOG_INFO_MESSAGE(...) LOG_DEBUG_MESSAGE(BasicPlatformDebug::DebugMessageSeverity::Info, ##__VA_ARGS__)
86
87 #define LOG_DEBUG_MESSAGE_ONCE(Severity, ...)\
88 do{ \
89 static bool IsFirstTime = true; \
90 if(IsFirstTime) \
91 { \
92 LOG_DEBUG_MESSAGE(Severity, ##__VA_ARGS__);\
93 IsFirstTime = false; \
94 } \
95 }while(false)
96
97 #define LOG_ERROR_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(BasicPlatformDebug::DebugMessageSeverity::Error, ##__VA_ARGS__)
98 #define LOG_WARNING_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(BasicPlatformDebug::DebugMessageSeverity::Warning, ##__VA_ARGS__)
99 #define LOG_INFO_MESSAGE_ONCE(...) LOG_DEBUG_MESSAGE_ONCE(BasicPlatformDebug::DebugMessageSeverity::Info, ##__VA_ARGS__)