diff --git a/core/error_macros.h b/core/error_macros.h new file mode 100644 index 0000000..8c28b3d --- /dev/null +++ b/core/error_macros.h @@ -0,0 +1,76 @@ +#ifndef ERROR_MACROS_H +#define ERROR_MACROS_H + +#include "core/log/logger.h" + +// Based on Godot Engine's error_macros.h +// MIT License +// Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. +// Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). + + +#ifdef _MSC_VER +#define GENERATE_TRAP \ + __debugbreak(); \ + /* Avoid warning about control paths */ \ + for (;;) { \ + } +#else +#define GENERATE_TRAP __builtin_trap(); +#endif + +#define LOG_TRACE(str) \ + Logger::log_trace(str); + +#define LOG_MSG(str) \ + Logger::log_message(str); + +#define LOG_WARN(str) \ + Logger::log_warning(str); + +#define LOG_ERR(str) \ + Logger::log_error(str); + +#define ERR_FAIL_INDEX(index, size) \ + if ((index < 0) || (index >= size)) {\ + Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, ""); \ + return;\ + } else\ + ((void)0)\ + +#define ERR_FAIL_INDEX_V(index, size, val) \ + if ((index < 0) || (index >= size)) {\ + Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, ""); \ + return val;\ + } else\ + ((void)0)\ + +#define ERR_FAIL_COND(cond) \ + if (cond) {\ + Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_FAIL_COND: \"" #cond "\" is true!"); \ + return;\ + } else\ + ((void)0)\ + +#define ERR_FAIL_COND_V(cond, val) \ + if (cond) {\ + Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "ERR_FAIL_COND: \"" #cond "\" is true!"); \ + return val;\ + } else\ + ((void)0)\ + +#define CRASH_INDEX(index, size) \ + if ((index < 0) || (index >= size)) {\ + Logger::_log_index_error(__FUNCTION__, __FILE__, __LINE__, index, size, "CRASH!"); \ + GENERATE_TRAP \ + } else\ + ((void)0)\ + +#define CRASH_COND(cond) \ + if (cond) {\ + Logger::_log_error(__FUNCTION__, __FILE__, __LINE__, "CRASH_COND: \"" #cond "\" is true!"); \ + GENERATE_TRAP \ + } else\ + ((void)0)\ + +#endif diff --git a/core/log/logger.cpp b/core/log/logger.cpp new file mode 100644 index 0000000..17e3922 --- /dev/null +++ b/core/log/logger.cpp @@ -0,0 +1,52 @@ +#include "logger.h" + +#include + +void Logger::log_trace(const String &str) +{ + log_trace(str.data()); +} +void Logger::log_trace(const char *str) +{ + printf("TRACE: %s\n", str); +} + +void Logger::log_message(const String &str) +{ + log_message(str.data()); +} +void Logger::log_message(const char *str) +{ + printf("MESSAGE: %s\n", str); +} + +void Logger::log_warning(const String &str) +{ + log_warning(str.data()); +} +void Logger::log_warning(const char *str) +{ + printf("WARNING: %s\n", str); +} + +void Logger::log_error(const String &str) +{ + log_error(str.data()); +} +void Logger::log_error(const char *str) +{ + printf("ERROR: %s\n", str); +} + +void Logger::_log_error(const char *p_function, const char *p_file, int p_line, const char *str) +{ + printf("!ERROR: (%s) %s:%d. %s\n", p_file, p_function, p_line, str); +} +void Logger::_log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str) +{ + printf("!ERROR: (%s) %s:%d :: %s. %s\n", p_file, p_function, p_line, str, p_msg); +} +void Logger::_log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str) +{ + printf("!INDEX ERROR: (%s) %s:%d :: index: %d/%d. %s\n", p_file, p_function, p_line, index, size, str); +} diff --git a/core/log/logger.h b/core/log/logger.h new file mode 100644 index 0000000..60a7b25 --- /dev/null +++ b/core/log/logger.h @@ -0,0 +1,26 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include "core/string.h" + +class Logger { +public: + static void log_trace(const String &str); + static void log_trace(const char *str); + + static void log_message(const String &str); + static void log_message(const char *str); + + static void log_warning(const String &str); + static void log_warning(const char *str); + + static void log_error(const String &str); + static void log_error(const char *str); + + static void _log_error(const char *p_function, const char *p_file, int p_line, const char *str); + static void _log_msg_error(const char *p_function, const char *p_file, int p_line, const char *p_msg, const char *str); + static void _log_index_error(const char *p_function, const char *p_file, int p_line, const int index, const int size, const char *str); +}; + + +#endif