Added error macros and a simple logger.

This commit is contained in:
Relintai 2021-10-31 23:34:08 +01:00
parent 3c1a60c898
commit 7ef1c76e4a
3 changed files with 154 additions and 0 deletions

76
core/error_macros.h Normal file
View File

@ -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

52
core/log/logger.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "logger.h"
#include <cstdio>
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);
}

26
core/log/logger.h Normal file
View File

@ -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