Error logging almost working!
This commit is contained in:
parent
a5dc639958
commit
85241b4aa5
@ -61,6 +61,7 @@ endif()
|
||||
if (NB_LOGGING)
|
||||
message(STATUS "Building with automatic logging")
|
||||
add_compile_definitions(_NB_AUTOLOG)
|
||||
add_compile_definitions(_NB_CODE_ERROR_LOCATIONS)
|
||||
endif()
|
||||
|
||||
if (NB_TARGET_WINDOWS)
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
toAbsolutePath(NB_CORE_SOURCE
|
||||
./src/Errors.cpp
|
||||
# ./src/Logger.cpp
|
||||
./src/Logger.cpp
|
||||
./src/Processes.cpp
|
||||
./src/StringUtils.cpp
|
||||
./src/Utils.cpp
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#ifndef _NB_DATASINK
|
||||
#define _NB_DATASINK
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
@ -34,9 +35,9 @@ class BufferedDataProcessor : public DataSink<DataType> {
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
bool stop() noexcept { return type_ptr->stop(); }
|
||||
bool run() { return type_ptr->run(); }
|
||||
bool in(const DataType& val) { return type_ptr->in(val); }
|
||||
virtual bool stop() noexcept override { return type_ptr->stop(); }
|
||||
virtual bool run() override { return type_ptr->run(); }
|
||||
virtual bool in(const DataType& val) override { return type_ptr->in(val); }
|
||||
|
||||
protected:
|
||||
unsigned int count() const {
|
||||
|
||||
@ -178,7 +178,7 @@ protected:
|
||||
) noexcept : ErrorBase(
|
||||
0,
|
||||
msg_,
|
||||
nullptr,
|
||||
static_cast<std::exception*>(nullptr),
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
@ -199,7 +199,7 @@ class Error : public ErrorBase<ErrorType> {
|
||||
using Base = ErrorBase<ErrorType>;
|
||||
public:
|
||||
template <typename... Args>
|
||||
Error(Args... T) : Base(T...) {}
|
||||
Error(Args... args) : Base(args...) {}
|
||||
|
||||
using Base::str;
|
||||
using Base::what;
|
||||
@ -219,6 +219,9 @@ class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
||||
public:
|
||||
using Base::Base;
|
||||
Error() : Base(0) {}
|
||||
|
||||
template <typename... Args>
|
||||
Error(Args... args) : Base(args...) {}
|
||||
|
||||
enum Codes : unsigned int {
|
||||
GENERAL, UNDEFINED, BADERRORCODE
|
||||
|
||||
@ -8,22 +8,6 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#ifndef THROW_WITH_INFO
|
||||
#ifdef CODE_ERROR_LOCATIONS
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__)
|
||||
#endif // CODE_ERROR_LOCATIONS
|
||||
#endif // THROW_WITH_INFO
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef LOGGING
|
||||
#define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
|
||||
#endif // LOGGING
|
||||
#endif // THROW
|
||||
|
||||
#include "DataSink.hpp"
|
||||
#include "Processes.hpp"
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
@ -87,7 +71,7 @@ public:
|
||||
|
||||
~DebugLogger() { type_ptr->stop(); }
|
||||
|
||||
void log(const std::string& msg, const uint8_t& lvl=0xFF) {
|
||||
void log(const std::string& msg, const uint8_t& lvl=0x00) {
|
||||
type_ptr->push(LogEvent{
|
||||
std::chrono::system_clock::now(),
|
||||
lvl,
|
||||
@ -98,12 +82,12 @@ public:
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) {
|
||||
void log(char const(&msg) [N], const uint8_t& lvl=0x00) {
|
||||
type_ptr->log(std::string(msg), lvl);
|
||||
}
|
||||
|
||||
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
|
||||
type_ptr->log(prepend_strblock(err.what(), " "), lvl);
|
||||
type_ptr->log(err.what(), lvl);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
@ -139,17 +123,31 @@ public:
|
||||
protected:
|
||||
using Base::_ostream;
|
||||
|
||||
bool process(const LogEvent& msg) {
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << msg.lvl << "\t|\t" << msg.msg << "\n";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool process(const LogEvent& msg);
|
||||
};
|
||||
|
||||
#ifndef _NB_NO_LOGGER
|
||||
extern DefaultDebugLogger logger;
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
// Taking Charge of Adult ADHD by Russell Barkley
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#ifndef CONSTRUCT_ERROR
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
#define CONSTRUCT_ERROR(type, ...) type(# __VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define CONSTRUCT_ERROR(type, ...) type(__VA_ARGS__)
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // CONSTRUCT_ERROR
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef _NB_AUTOLOG
|
||||
#define THROW(type, ...) {auto x = CONSTRUCT_ERROR(type, ## __VA_ARGS__); nb::logger.error(x); throw x;}
|
||||
#else
|
||||
#define THROW(type, ...) throw CONSTRUCT_ERROR(type, __VA_ARGS__)
|
||||
#endif // _NB_AUTOLOG
|
||||
#endif // THROW
|
||||
|
||||
#endif // _NB_LOGGER
|
||||
@ -1,10 +1,24 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Logger.hpp"
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifndef _NB_NO_LOGGER
|
||||
nb::DefaultDebugLogger logger(std::cout);
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
bool nb::DefaultDebugLogger::process(const LogEvent& msg) {
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << indent_strblock(
|
||||
msg.msg,
|
||||
std::string(10, ' '),
|
||||
"[ "+std::to_string(msg.lvl)+" ] : "
|
||||
) << nb::NEWLINE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
||||
return log.run();
|
||||
|
||||
@ -12,5 +12,13 @@ if (NB_BUILD_TESTS)
|
||||
NBCore
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
add_executable(LoggerTest
|
||||
./testLogger.cpp
|
||||
)
|
||||
target_link_libraries(LoggerTest
|
||||
NBCore
|
||||
)
|
||||
|
||||
gtest_discover_tests(TestCore)
|
||||
endif()
|
||||
17
engine/NBCore/tests/testLogger.cpp
Normal file
17
engine/NBCore/tests/testLogger.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include <exception>
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include "Logger.hpp"
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
nb::logger.log("Whoop!");
|
||||
try {
|
||||
THROW(nb::Error, "Hiii!");
|
||||
} catch (nb::Error<nb::NoneType> e){
|
||||
nb::logger.log("Winner winner chicken dinner!");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user