JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
ErrorReporting.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatialziation **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatialziation
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatialziation is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22// Thanks Jorrit Rouwe and his JoltPhysics library for the boilerplate below
23// https://github.com/jrouwe/JoltPhysics
24
25#ifndef JPL_ERROR_REPORTING_DEFINED
26#define JPL_ERROR_REPORTING_DEFINED
27
28#include "Core.h"
29
30#ifdef JPL_TAGGED_LOGGING
31#include <string_view>
32#else
33#include <format>
34#endif
35#include <source_location>
36
37namespace JPL
38{
40#ifdef JPL_TAGGED_LOGGING
41 using TaggedTraceFunction = void (*)(std::string_view tag, std::string_view message);
42 JPL_EXPORT extern TaggedTraceFunction JPLTraceTaggedTrace;
43 JPL_EXPORT extern TaggedTraceFunction JPLTraceTaggedInfo;
44 JPL_EXPORT extern TaggedTraceFunction JPLTraceTaggedWarn;
45 JPL_EXPORT extern TaggedTraceFunction JPLTraceTaggedError;
46#else
47 using TraceFunction = void (*)(const char* message);
49#endif
50
51// Always turn on asserts in Debug mode
52#if defined(JPL_DEBUG)
53#ifndef JPL_ENABLE_ASSERTS
54#define JPL_ENABLE_ASSERTS
55#endif
56#if !defined (JPL_TEST) && !defined(JPL_ENABLE_ENSURE)
57#define JPL_ENABLE_ENSURE
58#endif
59#endif
60
61#if defined(JPL_ENABLE_ASSERTS) || defined(JPL_ENABLE_ENSURE)
63using AssertFailedFunction = bool(*)(const char* inExpression, const char* inMessage, const std::source_location);
65
66// Helper functions to pass message on to failed function
67inline bool AssertFailedParamHelper(const char* inExpression, const std::source_location location)
68{
69 return AssertFailed(inExpression, nullptr, location);
70}
71
72inline bool AssertFailedParamHelper(const char* inExpression, const std::source_location location, const char* inMessage)
73{
74 return AssertFailed(inExpression, inMessage, location);
75}
76#endif
77
78#ifdef JPL_ENABLE_ASSERTS
80#define JPL_ASSERT(inExpression, ...) do { if (!(inExpression) && ::JPL::AssertFailedParamHelper(#inExpression, std::source_location::current(), ##__VA_ARGS__)) JPL_BREAKPOINT; } while (false)
81
82#define JPL_IF_ENABLE_ASSERTS(...) __VA_ARGS__
83#else
84#define JPL_ASSERT(...) ((void)0)
85
86#define JPL_IF_ENABLE_ASSERTS(...)
87#endif // JPL_ENABLE_ASSERTS
88
89
91#ifndef JPL_ENSURE
92#ifdef JPL_ENABLE_ENSURE
93
94#define JPL_ENSURE(inExpression, ...) [&]{ if(!(inExpression) && ::JPL::AssertFailedParamHelper(#inExpression, std::source_location::current(), ##__VA_ARGS__)) { JPL_BREAKPOINT; } return (inExpression); }()
95#else
96#define JPL_ENSURE(inExpression, ...) (inExpression)
97#endif
98#endif // !JPL_ENSURE
99
100#ifndef JPL_TRACE_TAG
101 #ifdef JPL_TAGGED_LOGGING
102 #define JPL_TRACE_TAG(tag, message) JPLTraceTaggedTrace(tag, message)
103 #else
104 #define JPL_TRACE_TAG(tag, message) SpatialTrace(std::format("[{}]: Trace: {}", tag, message).c_str())
105 #endif
106#endif // !JPL_TRACE_TAG
107
108#ifndef JPL_INFO_TAG
109 #ifdef JPL_TAGGED_LOGGING
110 #define JPL_INFO_TAG(tag, message) JPLTraceTaggedInfo(tag, message)
111 #else
112 #define JPL_INFO_TAG(tag, message) SpatialTrace(std::format("[{}]: Info: {}", tag, message).c_str())
113 #endif
114#endif // !JPL_INFO_TAG
115
116#ifndef JPL_WARN_TAG
117 #ifdef JPL_TAGGED_LOGGING
118 #define JPL_WARN_TAG(tag, message) JPLTraceTaggedWarn(tag, message)
119 #else
120 #define JPL_WARN_TAG(tag, message) SpatialTrace(std::format("[{}]: Warning: {}", tag, message).c_str())
121 #endif
122#endif // !JPL_WARN_TAG
123
124#ifndef JPL_ERROR_TAG
125 #ifdef JPL_TAGGED_LOGGING
126 #define JPL_ERROR_TAG(tag, message) JPLTraceTaggedError(tag, message)
127 #else
128 #define JPL_ERROR_TAG(tag, message) SpatialTrace(std::format("[{}]: Error: {}", tag, message).c_str())
129 #endif
130#endif // !JPL_ERROR_TAG
131
132} // namespace JPL
133
134#endif // JPL_ERROR_REPORTING_DEFINED
#define JPL_EXPORT
Definition Core.h:235
Definition AcousticMaterial.h:36
bool AssertFailedParamHelper(const char *inExpression, const std::source_location location)
Definition ErrorReporting.h:67
bool(*)(const char *inExpression, const char *inMessage, const std::source_location) AssertFailedFunction
Function called when an assertion fails. This function should return true if a breakpoint needs to be...
Definition ErrorReporting.h:63
void(*)(const char *message) TraceFunction
Trace function, needs to be overridden by application. This should output a line of text to the log /...
Definition ErrorReporting.h:47
JPL_EXPORT TraceFunction SpatialTrace
JPL_EXPORT AssertFailedFunction AssertFailed