| 1 | // LAF Base Library |
| 2 | // Copyright (C) 2019-2021 Igara Studio S.A. |
| 3 | // Copyright (c) 2001-2016 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef BASE_DEBUG_H_INCLUDED |
| 9 | #define BASE_DEBUG_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "base/base.h" |
| 13 | |
| 14 | int base_assert(const char* condition, const char* file, int lineNum) LAF_ANALYZER_NORETURN; |
| 15 | void base_trace(const char* msg, ...); |
| 16 | |
| 17 | #ifdef __cplusplus |
| 18 | #include <sstream> |
| 19 | #include <string> |
| 20 | |
| 21 | template<typename Arg> |
| 22 | void base_args_to_string_step(std::stringstream& s, Arg&& arg) { |
| 23 | s << std::forward<Arg>(arg); |
| 24 | } |
| 25 | |
| 26 | template<typename Arg, typename... Args> |
| 27 | void base_args_to_string_step(std::stringstream& s, Arg&& arg, Args&&... args) { |
| 28 | s << std::forward<Arg>(arg) << ' '; |
| 29 | base_args_to_string_step<Args...>(s, std::forward<Args>(args)...); |
| 30 | } |
| 31 | |
| 32 | template<typename... Args> |
| 33 | std::string base_args_to_string(Args&&... args) { |
| 34 | std::stringstream s; |
| 35 | base_args_to_string_step(s, std::forward<Args>(args)...); |
| 36 | return s.str(); |
| 37 | } |
| 38 | |
| 39 | template<typename... Args> |
| 40 | void base_trace_args(Args&&... args) { |
| 41 | std::string s = base_args_to_string(std::forward<Args>(args)...); |
| 42 | s.push_back('\n'); |
| 43 | base_trace(s.c_str()); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | #undef ASSERT |
| 48 | #undef TRACE |
| 49 | #undef TRACEARGS |
| 50 | |
| 51 | #ifdef _DEBUG |
| 52 | #if LAF_WINDOWS |
| 53 | #include <crtdbg.h> |
| 54 | #define base_break() _CrtDbgBreak() |
| 55 | #else |
| 56 | #include <signal.h> |
| 57 | #define base_break() raise(SIGTRAP) |
| 58 | #endif |
| 59 | |
| 60 | #define ASSERT(condition) { \ |
| 61 | if (!(condition)) { \ |
| 62 | if (base_assert(#condition, __FILE__, __LINE__)) { \ |
| 63 | base_break(); \ |
| 64 | } \ |
| 65 | } \ |
| 66 | } |
| 67 | |
| 68 | #define TRACE base_trace |
| 69 | #define TRACEARGS base_trace_args |
| 70 | #else |
| 71 | #define ASSERT(condition) |
| 72 | #define TRACE(...) |
| 73 | #define TRACEARGS(...) |
| 74 | #endif |
| 75 | |
| 76 | #endif |
| 77 | |