Compare commits
No commits in common. "9628a75710ffb365e8e51355786b46f16da66c71" and "e4b164f29a78555506efaa299f633ac295c740c3" have entirely different histories.
9628a75710
...
e4b164f29a
@ -7,10 +7,6 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
include(CMakePackageConfigHelpers)
|
||||
include(CMakeDependentOption)
|
||||
|
||||
function(toAbsolutePath SETVAR PATHS)
|
||||
set(PATHS ${PATHS} ${ARGN})
|
||||
set(RET_VAL "")
|
||||
@ -32,42 +28,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
message(STATUS "Targeting Release build")
|
||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
message(STATUS "Targeting Debug build")
|
||||
set(NB_DEBUG_BUILD ON)
|
||||
set(NB_LOGGING ON)
|
||||
set(NB_BUILD_TESTS ON)
|
||||
set(NB_BUILD_DOCS ON)
|
||||
add_compile_definitions(_NB_BUILD_DEBUG)
|
||||
endif()
|
||||
|
||||
cmake_dependent_option(NB_LOGGING
|
||||
"Creates a default logger and automatically logs with code locations."
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NB_BUILD_TESTS
|
||||
"Build unit tests"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NB_BUILD_DOCS
|
||||
"Build documentation"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NBENGINE_INSTALL
|
||||
"Install NBEngine"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NB_MONITOR_OPENGL_CALLS
|
||||
"Monitors every OpenGL call for errors"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||
message(STATUS "Building for Windows")
|
||||
set(NB_TARGET_WINDOWS ON)
|
||||
@ -89,6 +55,7 @@ if(NB_BUILD_TESTS)
|
||||
FetchContent_MakeAvailable(gtest)
|
||||
endif()
|
||||
set(GTEST_COLOR ON)
|
||||
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
|
||||
endif()
|
||||
|
||||
if (NB_LOGGING)
|
||||
@ -97,16 +64,19 @@ if (NB_LOGGING)
|
||||
add_compile_definitions(_NB_CODE_ERROR_LOCATIONS)
|
||||
endif()
|
||||
|
||||
if(NB_TARGET_WINDOWS)
|
||||
if (NB_TARGET_WINDOWS)
|
||||
add_compile_definitions(_NB_TARGET_WINDOWS)
|
||||
endif()
|
||||
if(NB_TARGET_LINUX)
|
||||
elseif (NB_TARGET_LINUX)
|
||||
add_compile_definitions(_NB_TARGET_LINUX)
|
||||
endif()
|
||||
|
||||
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
||||
# External Dep paths
|
||||
set(GLFW_PATH ../glfw/)
|
||||
set(GLAD_PATH ../glad/)
|
||||
get_filename_component(GLFW_PATH ${GLFW_PATH} ABSOLUTE)
|
||||
get_filename_component(GLAD_PATH ${GLAD_PATH} ABSOLUTE)
|
||||
|
||||
add_subdirectory(./engine ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
|
||||
add_subdirectory(./engine)
|
||||
|
||||
# Toggle
|
||||
set(NB_REBUILD_DOCS)
|
||||
@ -114,24 +84,3 @@ set(NB_REBUILD_DOCS)
|
||||
if (NB_BUILD_DOCS)
|
||||
add_subdirectory(./docs)
|
||||
endif()
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
configure_package_config_file(
|
||||
"NBEngineConfig.cmake.in"
|
||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfig.cmake"
|
||||
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/cmake
|
||||
PATH_VARS CMAKE_INSTALL_LIBDIR
|
||||
)
|
||||
write_basic_package_version_file(
|
||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfigVersion.cmake"
|
||||
COMPATIBILITY AnyNewerVersion
|
||||
)
|
||||
|
||||
if (NBENGINE_INSTALL)
|
||||
include(GNUInstallDirs)
|
||||
install(FILES
|
||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfig.cmake"
|
||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfigVersion.cmake"
|
||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
||||
)
|
||||
endif()
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
@PACKAGE_INIT@
|
||||
set(NBENGINE_PACKAGES
|
||||
NBCore
|
||||
NBGraphics
|
||||
)
|
||||
foreach(NBENGINE_LIB ${NBENGINE_PACKAGES})
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/${NBENGINE_LIB}Config.cmake")
|
||||
endforeach()
|
||||
BIN
awesomeface.png
BIN
awesomeface.png
Binary file not shown.
|
Before Width: | Height: | Size: 58 KiB |
@ -1,30 +1,18 @@
|
||||
include_directories(./.)
|
||||
|
||||
add_subdirectory(./NBCore)
|
||||
|
||||
get_target_property(NBCORE_INTERFACE_INCLUDES NBCore INTERFACE_INCLUDE_DIRECTORIES)
|
||||
include_directories(${NBCORE_INTERFACE_INCLUDES})
|
||||
|
||||
#add_subdirectory(./NBEvents)
|
||||
add_subdirectory(./NBEvents)
|
||||
add_subdirectory(./NBGraphics)
|
||||
add_subdirectory(./NBData)
|
||||
|
||||
if (NB_CORE_SOURCE OR NB_EVENTS_SOURCE OR NB_GRAPHICS_SOURCE OR NB_DATA_SOURCE)
|
||||
if (NB_CORE_SOURCE OR NB_EVENTS_SOURCE OR NB_GRAPHICS_SOURCE)
|
||||
set(NB_SOURCE_FILES "")
|
||||
list(APPEND NB_SOURCE_FILES
|
||||
${NB_CORE_SOURCE}
|
||||
${NB_GRAPHICS_SOURCE}
|
||||
${NB_EVENTS_SOURCE}
|
||||
${NB_DATA_SOURCE}
|
||||
)
|
||||
list(APPEND NB_SOURCE_FILES ${NB_CORE_SOURCE} ${NB_GRAPHICS_SOURCE} ${NB_EVENTS_SOURCE})
|
||||
set(NB_SOURCE_FILES ${NB_SOURCE_FILES} PARENT_SCOPE)
|
||||
endif()
|
||||
|
||||
if (NB_CORE_INCLUDE OR NB_EVENTS_INCLUDE OR NB_GRAPHICS_INCLUDE OR NB_DATA_INCLUDE)
|
||||
|
||||
if (NB_CORE_INCLUDE OR NB_EVENTS_INCLUDE OR NB_GRAPHICS_INCLUDE)
|
||||
set(NB_INCLUDE_FILES "")
|
||||
list(APPEND NB_INCLUDE_FILES
|
||||
${NB_CORE_INCLUDE}
|
||||
${NB_GRAPHICS_INCLUDE}
|
||||
${NB_EVENTS_INCLUDE}
|
||||
${NB_DATA_INCLUDE}
|
||||
)
|
||||
list(APPEND NB_INCLUDE_FILES ${NB_CORE_INCLUDE} ${NB_GRAPHICS_INCLUDE} ${NB_EVENTS_INCLUDE})
|
||||
set(NB_INCLUDE_FILES ${NB_INCLUDE_FILES} PARENT_SCOPE)
|
||||
endif()
|
||||
|
||||
@ -1,13 +1,3 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(NBCore VERSION 0.1)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
toAbsolutePath(NB_CORE_SOURCE
|
||||
./src/ErrorsImpl.cpp
|
||||
./src/Logger.cpp
|
||||
@ -15,72 +5,28 @@ toAbsolutePath(NB_CORE_SOURCE
|
||||
./src/StringUtils.cpp
|
||||
./src/Utils.cpp
|
||||
)
|
||||
|
||||
toAbsolutePath(NB_CORE_INCLUDE
|
||||
./include/NBCore/ANSITerm.hpp
|
||||
./include/NBCore/DataSink.hpp
|
||||
./include/NBCore/Errors.hpp
|
||||
./include/NBCore/ErrorsImpl.hpp
|
||||
./include/NBCore/Logger.hpp
|
||||
./include/NBCore/Printer.hpp
|
||||
./include/NBCore/Processes.hpp
|
||||
./include/NBCore/StringUtils.hpp
|
||||
./include/NBCore/ThreadsafeQueue.hpp
|
||||
./include/NBCore/Types.hpp
|
||||
./include/NBCore/TypeTraits.hpp
|
||||
./include/NBCore/Utils.hpp
|
||||
./ANSITerm.hpp
|
||||
./DataSink.hpp
|
||||
./Errors.hpp
|
||||
./ErrorsImpl.hpp
|
||||
./Logger.hpp
|
||||
./Processes.hpp
|
||||
./StringUtils.hpp
|
||||
./ThreadsafeQueue.hpp
|
||||
./Types.hpp
|
||||
./TypeTraits.hpp
|
||||
./Utils.hpp
|
||||
)
|
||||
|
||||
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
||||
set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
|
||||
|
||||
add_library(NBCore ${NB_CORE_SOURCE})
|
||||
add_library(NBEngine::Core ALIAS NBCore)
|
||||
target_include_directories(NBCore
|
||||
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
||||
PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
export(
|
||||
TARGETS NBCore
|
||||
FILE "${CMAKE_BINARY_DIR}/cmake/NBCoreTargets.cmake"
|
||||
NAMESPACE NBEngine::
|
||||
)
|
||||
configure_package_config_file(
|
||||
"NBCoreConfig.cmake.in"
|
||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfig.cmake"
|
||||
INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
||||
PATH_VARS CMAKE_INSTALL_LIBDIR
|
||||
)
|
||||
write_basic_package_version_file(
|
||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfigVersion.cmake"
|
||||
COMPATIBILITY AnyNewerVersion
|
||||
)
|
||||
|
||||
if (NBENGINE_INSTALL)
|
||||
message("Installing NBCore to ${CMAKE_INSTALL_PREFIX}")
|
||||
install(
|
||||
TARGETS NBCore
|
||||
EXPORT NBCoreTargets
|
||||
LIBRARY DESTINATION lib
|
||||
ARCHIVE DESTINATION lib
|
||||
INCLUDES DESTINATION include
|
||||
)
|
||||
install(
|
||||
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/NBCore"
|
||||
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
|
||||
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
|
||||
)
|
||||
install(
|
||||
EXPORT NBCoreTargets
|
||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
||||
NAMESPACE NBEngine::
|
||||
)
|
||||
install(FILES
|
||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfig.cmake"
|
||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfigVersion.cmake"
|
||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/CMake"
|
||||
)
|
||||
endif()
|
||||
target_include_directories(NBCore PUBLIC ./.)
|
||||
|
||||
if (NB_BUILD_TESTS)
|
||||
add_subdirectory(./tests )
|
||||
add_subdirectory(./tests)
|
||||
endif()
|
||||
134
engine/NBCore/DataSink.hpp
Normal file
134
engine/NBCore/DataSink.hpp
Normal file
@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
#ifndef _NB_DATASINK
|
||||
#define _NB_DATASINK
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename DataType>
|
||||
class DataSink {
|
||||
public:
|
||||
DataSink(const DataSink&) = delete;
|
||||
DataSink(DataSink&&) = delete;
|
||||
DataSink& operator=(const DataSink&) = delete;
|
||||
|
||||
virtual bool isRunning() const noexcept {
|
||||
return _running;
|
||||
}
|
||||
virtual bool stop() noexcept = 0;
|
||||
virtual bool run() = 0;
|
||||
virtual bool in(const DataType&) = 0;
|
||||
|
||||
protected:
|
||||
DataSink() = default;
|
||||
|
||||
std::atomic<bool> _running;
|
||||
};
|
||||
|
||||
template<typename DataType, typename BufferType, typename ProcessorType>
|
||||
class BufferedDataProcessor : public DataSink<DataType> {
|
||||
using Base = DataSink<DataType>;
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
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 {
|
||||
return type_ptr->count();
|
||||
}
|
||||
void push(const DataType& val) {
|
||||
type_ptr->push(val);
|
||||
}
|
||||
DataType pop() {
|
||||
return type_ptr->pop();
|
||||
}
|
||||
void flush() {
|
||||
type_ptr->flush();
|
||||
}
|
||||
void clear() {
|
||||
type_ptr->clear();
|
||||
}
|
||||
bool process(const DataType& val) {
|
||||
return type_ptr->process(val);
|
||||
}
|
||||
|
||||
using Base::_running;
|
||||
|
||||
BufferType _buffer;
|
||||
|
||||
private:
|
||||
ProcessorType* const type_ptr = static_cast<ProcessorType*>(this);
|
||||
|
||||
};
|
||||
|
||||
template<typename DataType, typename ProcessorType>
|
||||
class MultithreadedDataProcessor
|
||||
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
||||
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
||||
public:
|
||||
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
||||
|
||||
bool isRunning() const noexcept override {
|
||||
return this->_running;
|
||||
}
|
||||
bool run() override {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
_runningThread = std::make_shared<std::thread>([&]{
|
||||
while(type_ptr->isRunning()) {
|
||||
flush();
|
||||
}
|
||||
});
|
||||
}
|
||||
return this->isRunning();
|
||||
}
|
||||
bool stop() noexcept override {
|
||||
if (type_ptr->isRunning()) {
|
||||
this->_running = false;
|
||||
_runningThread->join();
|
||||
_runningThread = nullptr;
|
||||
}
|
||||
return !type_ptr->isRunning();
|
||||
}
|
||||
|
||||
protected:
|
||||
using Base::Base;
|
||||
|
||||
unsigned int count() const {
|
||||
return this->_buffer.size();
|
||||
}
|
||||
void push(const DataType& val) {
|
||||
this->_buffer.push(val);
|
||||
};
|
||||
DataType pop() {
|
||||
std::shared_ptr<DataType> event;
|
||||
this->_buffer.pop(event);
|
||||
this->process(*event);
|
||||
return *event;
|
||||
}
|
||||
void flush() {
|
||||
while(type_ptr->count()) {
|
||||
pop();
|
||||
}
|
||||
}
|
||||
void clear() {
|
||||
this->_buffer.empty();
|
||||
}
|
||||
|
||||
std::shared_ptr<std::thread> _runningThread;
|
||||
|
||||
private:
|
||||
ProcessorType* type_ptr = static_cast<ProcessorType*>(this);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_DATASINK
|
||||
43
engine/NBCore/Errors.hpp
Normal file
43
engine/NBCore/Errors.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#ifndef _NB_ERROR
|
||||
#define _NB_ERROR
|
||||
|
||||
#include "ErrorsImpl.hpp"
|
||||
#include "Logger.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_AUTOLOG
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
#ifndef LOG
|
||||
#define LOG(args...) nb::logger.log(args, __FILE__, __LINE__)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) nb::logger.warn(args, __FILE__, __LINE__)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args...) nb::logger.error(args, __FILE__, __LINE__)
|
||||
#endif // ERROR
|
||||
#else
|
||||
#ifndef LOG
|
||||
#define LOG(args...) nb::logger.log(args)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) nb::logger.warn(args)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args...) nb::logger.error(args)
|
||||
#endif // ERROR
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // _NB_AUTOLOG
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef _NB_AUTOLOG
|
||||
#define THROW(args...) ERROR(args); nb::logger.stop(); throw args
|
||||
#else
|
||||
#define THROW(args...) throw args
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // THROW
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_ERROR
|
||||
@ -6,13 +6,17 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <NBCore/StringUtils.hpp>
|
||||
#include <NBCore/Utils.hpp>
|
||||
#include "StringUtils.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
using ErrorCodeMap = ConstantMap<unsigned int, std::string>;
|
||||
typedef std::unordered_map<unsigned int, std::string> ErrorCodeMap;
|
||||
|
||||
template <class ErrorType=NoneType>
|
||||
class Error;
|
||||
|
||||
class ErrorBase {
|
||||
protected:
|
||||
@ -28,11 +32,11 @@ class ErrorBase {
|
||||
virtual std::string what() const noexcept {
|
||||
std::string ret = msg;
|
||||
if (trace) {
|
||||
std::string trace_msg = trace->what();
|
||||
ret += NEWLINE + indent_strblock(
|
||||
std::string trace_msg = msg;
|
||||
ret += nb::NEWLINE + indent_strblock(
|
||||
trace_msg,
|
||||
TABOVER,
|
||||
TABOVER+"Trace: "
|
||||
nb::TABOVER,
|
||||
nb::TABOVER+"Trace: "
|
||||
);
|
||||
}
|
||||
return ret;
|
||||
@ -40,19 +44,19 @@ class ErrorBase {
|
||||
|
||||
protected:
|
||||
ErrorBase(
|
||||
unsigned int code_,
|
||||
std::string msg_,
|
||||
std::string type_,
|
||||
std::shared_ptr<ErrorBase> trace_
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
type(type_),
|
||||
trace{trace_}
|
||||
{}
|
||||
unsigned int code_,
|
||||
std::string msg_,
|
||||
std::string type_,
|
||||
std::shared_ptr<ErrorBase> trace_
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
type(type_),
|
||||
trace{trace_}
|
||||
{}
|
||||
};
|
||||
|
||||
template <class ErrorType=NoneType>
|
||||
template <class ErrorType>
|
||||
class Error : public ErrorBase {
|
||||
private:
|
||||
void inline check_asserts() {
|
||||
@ -69,15 +73,16 @@ class Error : public ErrorBase {
|
||||
);
|
||||
}
|
||||
|
||||
public:
|
||||
protected:
|
||||
using ErrorBase::ErrorBase;
|
||||
|
||||
public:
|
||||
Error(
|
||||
unsigned int code_,
|
||||
const ErrorBase& trace_
|
||||
) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages[code_],
|
||||
ErrorType::ErrorMessages.at(code_),
|
||||
ErrorType::type,
|
||||
std::make_shared<ErrorBase>(trace_)
|
||||
) { check_asserts(); }
|
||||
@ -86,7 +91,7 @@ class Error : public ErrorBase {
|
||||
std::string msg_,
|
||||
const ErrorBase& trace_
|
||||
) noexcept : ErrorBase(
|
||||
ErrorType::Codes::UNDEFINED,
|
||||
0,
|
||||
msg_,
|
||||
ErrorType::type,
|
||||
std::make_shared<ErrorBase>(trace_)
|
||||
@ -94,13 +99,13 @@ class Error : public ErrorBase {
|
||||
|
||||
Error(unsigned int code_) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages[code_],
|
||||
ErrorType::ErrorMessages.at(code_),
|
||||
ErrorType::type,
|
||||
nullptr
|
||||
) { check_asserts(); }
|
||||
|
||||
Error(std::string msg_) noexcept : ErrorBase(
|
||||
ErrorType::Codes::UNDEFINED,
|
||||
0,
|
||||
msg_,
|
||||
ErrorType::type,
|
||||
nullptr
|
||||
@ -112,14 +117,14 @@ class Error : public ErrorBase {
|
||||
const ErrorBase& trace_
|
||||
) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages[code_] + " (" + note_ + ")",
|
||||
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
||||
ErrorType::type,
|
||||
std::make_shared<ErrorBase>(trace_)
|
||||
) { check_asserts(); }
|
||||
|
||||
Error(unsigned int code_, const std::string& note_) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages[code_] + " (" + note_ + ")",
|
||||
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
||||
ErrorType::type,
|
||||
nullptr
|
||||
) { check_asserts(); }
|
||||
@ -131,41 +136,40 @@ class Error : public ErrorBase {
|
||||
using ErrorBase::type;
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
class Error<NoneType> : public Error<Error<NoneType>> {
|
||||
protected:
|
||||
using Base = Error<Error<NoneType>>;
|
||||
|
||||
public:
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::trace;
|
||||
using Base::Base;
|
||||
enum Codes : unsigned int {
|
||||
STANDARD, UNDEFINED, OUT_OF_RANGE, VALUE_ERROR, OVERWRITE_ERROR
|
||||
};
|
||||
inline static const std::string type = "nb::Error";
|
||||
inline static const ErrorCodeMap ErrorMessages = {
|
||||
{STANDARD, "std::exception"},
|
||||
{UNDEFINED, "Error"},
|
||||
{OUT_OF_RANGE, "Out-of-range error"},
|
||||
{VALUE_ERROR, "Invalid value"},
|
||||
{OVERWRITE_ERROR, "Attempting to overwrite managed data"}
|
||||
|
||||
};
|
||||
|
||||
Error(unsigned int code_=1) noexcept : Base(code_) {}
|
||||
|
||||
Error(const std::exception& err) : Base(
|
||||
STANDARD,
|
||||
Codes::STANDARD,
|
||||
std::string(err.what()),
|
||||
"std::exception",
|
||||
nullptr
|
||||
) {}
|
||||
|
||||
// fix
|
||||
Error(std::string msg_) noexcept : Base(
|
||||
Codes::UNDEFINED,
|
||||
msg_
|
||||
) {}
|
||||
|
||||
enum Codes : unsigned int {
|
||||
STANDARD, UNDEFINED
|
||||
};
|
||||
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
template<typename... Args>
|
||||
Error(Args...) -> Error<NoneType>;
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_ERRORS_IMPL
|
||||
176
engine/NBCore/Logger.hpp
Normal file
176
engine/NBCore/Logger.hpp
Normal file
@ -0,0 +1,176 @@
|
||||
#pragma once
|
||||
#ifndef _NB_LOGGER
|
||||
#define _NB_LOGGER
|
||||
|
||||
#include <chrono>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "DataSink.hpp"
|
||||
#include "ErrorsImpl.hpp"
|
||||
#include "Processes.hpp"
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
class ErrorBase;
|
||||
|
||||
typedef std::chrono::time_point<
|
||||
std::chrono::system_clock,
|
||||
std::chrono::nanoseconds
|
||||
> LoggerTimePoint;
|
||||
|
||||
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
||||
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
||||
|
||||
template<typename LogType, typename Logger, typename ST=std::ostream*>
|
||||
class LoggerBase
|
||||
: public MultithreadedDataProcessor<LogType, Logger>{
|
||||
using StreamType = ST;
|
||||
using LoggerType = Logger;
|
||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||
|
||||
public:
|
||||
bool run() override {
|
||||
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
||||
this->_running = true;
|
||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||
while(static_cast<LoggerType*>(this)->isRunning()) {
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
}
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
});
|
||||
}
|
||||
return static_cast<LoggerType*>(this)->isRunning();
|
||||
}
|
||||
using Base::flush;
|
||||
|
||||
protected:
|
||||
LoggerBase() = default;
|
||||
|
||||
StreamType _ostream;
|
||||
};
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
const std::string file="";
|
||||
const unsigned int line=0;
|
||||
};
|
||||
|
||||
template <typename LT>
|
||||
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
|
||||
using StreamType = std::vector<std::ostream*>;
|
||||
using LoggerType = LT;
|
||||
using Base = LoggerBase<LogEvent, LoggerType, StreamType>;
|
||||
public:
|
||||
|
||||
template<typename... ST>
|
||||
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
|
||||
|
||||
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
||||
|
||||
template<typename U>
|
||||
void log(
|
||||
U val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) { static_cast<LoggerType*>(this)->write_message(val, 0x00, file, line); }
|
||||
|
||||
template<typename U>
|
||||
void warn(
|
||||
U val,
|
||||
uint8_t lvl=0x01,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) { static_cast<LoggerType*>(this)->write_message(val, lvl, file, line); }
|
||||
|
||||
void error(
|
||||
const ErrorBase& val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->write_message(val, 0xFF, file, line);
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
}
|
||||
|
||||
protected:
|
||||
std::vector<std::ostream*> _ostream;
|
||||
void write_message(
|
||||
std::string msg,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->push(LogEvent{
|
||||
std::chrono::system_clock::now(),
|
||||
lvl,
|
||||
msg,
|
||||
std::this_thread::get_id(),
|
||||
GetPID(),
|
||||
file,
|
||||
line
|
||||
});
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
void write_message(
|
||||
char const(&msg) [N],
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->write_message(std::string(msg), lvl, file, line);
|
||||
}
|
||||
|
||||
void write_message(
|
||||
const ErrorBase& err,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->write_message(err.what(), lvl, file, line);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
using LoggerType = DefaultDebugLogger;
|
||||
using Base = DebugLogger<DefaultDebugLogger>;
|
||||
|
||||
template <typename... Ts>
|
||||
struct LogRow;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
~DefaultDebugLogger() { stop(); }
|
||||
|
||||
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
|
||||
|
||||
template <typename STR, typename... Ts>
|
||||
friend STR& operator<<(STR&, const DefaultDebugLogger::LogRow<Ts...>&);
|
||||
|
||||
protected:
|
||||
using Base::_ostream;
|
||||
|
||||
bool process(const LogEvent& msg);
|
||||
};
|
||||
|
||||
extern const bool LOGGER_RUNNING;
|
||||
|
||||
#ifndef _NB_NO_LOGGER
|
||||
extern DefaultDebugLogger logger;
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
// Taking Charge of Adult ADHD by Russell Barkley
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_LOGGER
|
||||
@ -1,2 +0,0 @@
|
||||
@PACKAGE_INIT@
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/NBCoreTargets.cmake")
|
||||
@ -2,24 +2,43 @@
|
||||
#ifndef _NB_STRING_UTILS
|
||||
#define _NB_STRING_UTILS
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <NBCore/TypeTraits.hpp>
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
inline static const std::string NEWLINE = "\n";
|
||||
inline static const std::wstring WNEWLINE = L"\n";
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
inline const std::string NEWLINE = "\n";
|
||||
inline const std::wstring WNEWLINE = L"\n";
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
const std::string TABOVER = " ";
|
||||
|
||||
// std::wstring str_to_wstr(std::string in);
|
||||
|
||||
// std::string wstr_to_str(std::wstring in);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(const Stream& s, Args&&... args);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(const Stream& s, Args&&... args) {
|
||||
(s << ... << args);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
||||
|
||||
template<typename... Args>
|
||||
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
||||
|
||||
template <typename T = char>
|
||||
std::basic_string<T> find_and_replace(
|
||||
ExplicitType_t<std::basic_string<T>> original,
|
||||
@ -33,9 +33,6 @@ namespace detail {
|
||||
|
||||
struct NoneType{};
|
||||
|
||||
template<typename T, unsigned int Arg>
|
||||
using Const_t = std::integral_constant<T, Arg>;
|
||||
|
||||
template <
|
||||
template <class...> class Op,
|
||||
class... Args
|
||||
@ -90,15 +87,6 @@ struct RunAndOutput {
|
||||
}
|
||||
};
|
||||
|
||||
template<typename A, typename B>
|
||||
using SubtractType = decltype(std::declval<A>() - std::declval<B>());
|
||||
|
||||
template<typename A, typename B>
|
||||
using MultiplyType = decltype(std::declval<A>() * std::declval<B>());
|
||||
|
||||
template<typename A, typename B>
|
||||
using AdditionType = decltype(std::declval<A>() + std::declval<B>());
|
||||
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>&, Func) {}
|
||||
@ -121,41 +109,6 @@ ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
||||
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
|
||||
}
|
||||
|
||||
template<typename T, typename... Pack>
|
||||
struct Find;
|
||||
|
||||
template<typename T, typename U, typename... Pack>
|
||||
struct Find<T, U, Pack...> {
|
||||
protected:
|
||||
using here = Find<T, U>;
|
||||
static constexpr unsigned int downstream = std::conditional_t<
|
||||
here::value,
|
||||
Const_t<unsigned int, 0>,
|
||||
Find<T, Pack...>
|
||||
>::value;
|
||||
|
||||
public:
|
||||
static constexpr unsigned int value = std::conditional_t<
|
||||
here::value,
|
||||
Const_t<unsigned int, 1>,
|
||||
std::conditional_t<
|
||||
downstream,
|
||||
Const_t<unsigned int, 1+downstream>,
|
||||
Const_t<unsigned int, 0>
|
||||
>
|
||||
>::value;
|
||||
};
|
||||
|
||||
template<typename T, typename U>
|
||||
struct Find<T, U> {
|
||||
public:
|
||||
static constexpr unsigned int value = std::conditional_t<
|
||||
std::is_base_of<T, U>::value,
|
||||
std::integral_constant<unsigned int, 1>,
|
||||
std::integral_constant<unsigned int, 0>
|
||||
>::value;
|
||||
};
|
||||
|
||||
template <typename T, typename Pack1, typename... Pack>
|
||||
struct RefPackToPtrVec {
|
||||
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
||||
@ -2,27 +2,13 @@
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
// #include <NBCore/Errors.hpp>
|
||||
#include "Errors.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename A, typename B>
|
||||
class ConstantMap : public std::unordered_map<A, B> {
|
||||
using Base = std::unordered_map<A, B>;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::at;
|
||||
const B& operator[](const A& key) const {
|
||||
return at(key);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using SharedVector = std::vector<std::shared_ptr<T>>;
|
||||
|
||||
@ -31,7 +17,7 @@ using RValueVector = std::vector<T&&>;
|
||||
|
||||
using ByteVector = std::vector<uint8_t>;
|
||||
|
||||
/* class ObjectManagerError : public Error<ObjectManagerError> {
|
||||
class ObjectManagerError : public Error<ObjectManagerError> {
|
||||
using Base = Error<ObjectManagerError>;
|
||||
|
||||
public:
|
||||
@ -43,7 +29,7 @@ using ByteVector = std::vector<uint8_t>;
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
}; */
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
ByteVector vectorToBytes(const std::vector<T>& vec) {
|
||||
@ -68,10 +54,7 @@ ByteVector concatVectorBytes(const std::vector<T>& vec1, const std::vector<S>& v
|
||||
template<typename T>
|
||||
std::vector<T> bytesToVector(const ByteVector& vec) {
|
||||
if (vec.size() % sizeof(T) != 0) {
|
||||
/* THROW(Error<NoneType>(
|
||||
std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.")
|
||||
)); */
|
||||
throw "byyyyeeee";
|
||||
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
|
||||
}
|
||||
unsigned int num_elmts = vec.size() / sizeof(T);
|
||||
std::vector<T> ret(num_elmts);
|
||||
@ -80,7 +63,7 @@ std::vector<T> bytesToVector(const ByteVector& vec) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* template <typename T>
|
||||
template <typename T>
|
||||
class ThreadsafeObjectLock;
|
||||
|
||||
template <typename T>
|
||||
@ -129,7 +112,7 @@ class ThreadsafeObjectLock {
|
||||
_manager->_mutex.lock();
|
||||
}
|
||||
ThreadsafeObject<T>* const _manager;
|
||||
}; */
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_CORE_TYPES
|
||||
@ -1,184 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_DATASINK
|
||||
#define _NB_DATASINK
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
#include <NBCore/ThreadSafeQueue.hpp>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename DataType>
|
||||
class DataSink {
|
||||
public:
|
||||
DataSink(const DataSink&) = delete;
|
||||
DataSink(DataSink&&) = delete;
|
||||
DataSink& operator=(const DataSink&) = delete;
|
||||
|
||||
virtual bool isRunning() const noexcept {
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
return _running.load(std::memory_order_acquire);
|
||||
}
|
||||
virtual bool stop() noexcept = 0;
|
||||
virtual bool run() = 0;
|
||||
virtual bool in(const DataType&) = 0;
|
||||
|
||||
protected:
|
||||
DataSink() {
|
||||
_running.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
std::atomic<bool> _running;
|
||||
};
|
||||
|
||||
template<typename DataType, typename SinkTypes=DataSink<DataType>>
|
||||
class MultiSink : public DataSink<DataType> {
|
||||
protected:
|
||||
using Base = DataSink<DataType>;
|
||||
using SinkPtr = std::shared_ptr<SinkTypes>;
|
||||
using Base::_running;
|
||||
std::vector<SinkPtr> _sinks;
|
||||
|
||||
public:
|
||||
MultiSink(std::vector<SinkPtr> sinks={}) : _sinks(sinks) {}
|
||||
virtual void addSink(SinkPtr sink) {
|
||||
_sinks.push_back(sink);
|
||||
}
|
||||
virtual std::vector<SinkPtr>& getSinks() { return _sinks; }
|
||||
bool isRunning() const noexcept override {
|
||||
return Base::isRunning();
|
||||
}
|
||||
bool stop() noexcept override {
|
||||
_running.store(false, std::memory_order_release);
|
||||
for (auto& sink : _sinks) {
|
||||
sink->stop();
|
||||
}
|
||||
return isRunning();
|
||||
}
|
||||
bool run() override {
|
||||
_running.store(true, std::memory_order_release);
|
||||
for (auto& sink : _sinks) {
|
||||
sink->run();
|
||||
}
|
||||
return isRunning();
|
||||
}
|
||||
bool in(const DataType& data) override {
|
||||
if (isRunning()) {
|
||||
bool success = true;
|
||||
for (auto& sink : _sinks) {
|
||||
success &= sink->in(data);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename DataType, typename BufferType, typename ProcessorType>
|
||||
class BufferedDataProcessor : public DataSink<DataType> {
|
||||
private:
|
||||
ProcessorType* const type_ptr = static_cast<ProcessorType*>(this);
|
||||
|
||||
protected:
|
||||
using Base = DataSink<DataType>;
|
||||
using Base::_running;
|
||||
BufferType _buffer;
|
||||
virtual unsigned int count() const {
|
||||
return type_ptr->count();
|
||||
}
|
||||
virtual bool pop(std::shared_ptr<DataType> ret) {
|
||||
return type_ptr->pop(ret);
|
||||
}
|
||||
virtual void flush() {
|
||||
type_ptr->flush();
|
||||
}
|
||||
virtual void clear() {
|
||||
type_ptr->clear();
|
||||
}
|
||||
virtual bool process(const DataType& val) = 0;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
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); }
|
||||
|
||||
};
|
||||
|
||||
template<typename DataType, typename ProcessorType>
|
||||
class MultithreadedDataProcessor
|
||||
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
||||
private:
|
||||
ProcessorType* const type_ptr = static_cast<ProcessorType*>(this);
|
||||
std::mutex _pause;
|
||||
|
||||
protected:
|
||||
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
||||
using Base::Base;
|
||||
using Base::process;
|
||||
using Base::_running;
|
||||
std::shared_ptr<std::thread> _runningThread;
|
||||
virtual unsigned int count() const override {
|
||||
return type_ptr->_buffer.size();
|
||||
}
|
||||
virtual bool pop(std::shared_ptr<DataType> ret=nullptr) override {
|
||||
type_ptr->_buffer.pop(ret);
|
||||
return this->process(*ret);
|
||||
}
|
||||
virtual bool popBlock(std::shared_ptr<DataType> ret=nullptr) {
|
||||
ret = type_ptr->_buffer.popBlock();
|
||||
return this->process(*ret);
|
||||
}
|
||||
virtual void flush() override {
|
||||
while(type_ptr->count()) {
|
||||
type_ptr->pop();
|
||||
}
|
||||
}
|
||||
virtual void clear() override {
|
||||
type_ptr->_buffer.empty();
|
||||
}
|
||||
virtual std::unique_lock<std::mutex> pause() {
|
||||
return std::move(std::unique_lock<std::mutex>(_pause));
|
||||
}
|
||||
|
||||
public:
|
||||
using Base::isRunning;
|
||||
virtual ~MultithreadedDataProcessor() { type_ptr->stop(); }
|
||||
|
||||
bool run() override {
|
||||
if (!type_ptr->isRunning()) {
|
||||
_running.store(true, std::memory_order_release);
|
||||
_runningThread = std::make_shared<std::thread>([&]{
|
||||
while(type_ptr->isRunning()) {
|
||||
std::lock_guard<std::mutex> lock(_pause);
|
||||
type_ptr->flush();
|
||||
}
|
||||
type_ptr->flush();
|
||||
type_ptr->stop();
|
||||
});
|
||||
}
|
||||
return type_ptr->isRunning();
|
||||
}
|
||||
bool stop() noexcept override {
|
||||
if (type_ptr->isRunning()) {
|
||||
_running.store(false, std::memory_order_release);
|
||||
if (_runningThread) {
|
||||
_runningThread->join();
|
||||
_runningThread = nullptr;
|
||||
}
|
||||
type_ptr->flush();
|
||||
}
|
||||
return !type_ptr->isRunning();
|
||||
}
|
||||
bool in(const DataType& val) override {
|
||||
type_ptr->_buffer.push(val);
|
||||
return type_ptr->isRunning();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_DATASINK
|
||||
@ -1,65 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_ERROR
|
||||
#define _NB_ERROR
|
||||
|
||||
#include <NBCore/ErrorsImpl.hpp>
|
||||
#include <NBCore/Logger.hpp>
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_AUTOLOG
|
||||
#ifndef LOG_W_CODE_LOC
|
||||
#define LOG_W_CODE_LOC(file, line, args...) nb::logger.msg(args, file, line)
|
||||
#endif // LOG_W_CODE_LOC
|
||||
#ifndef WARN_W_CODE_LOC
|
||||
#define WARN_W_CODE_LOC(file, line, args...) nb::logger.warn(args, file, line)
|
||||
#endif // WARN_W_CODE_LOC
|
||||
#ifndef ERROR_W_CODE_LOC
|
||||
#define ERROR_W_CODE_LOC(file, line, args...) nb::logger.error(args, file, line)
|
||||
#endif // ERROR_W_CODE_LOC
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
#ifndef LOG
|
||||
#define LOG(arg) LOG_W_CODE_LOC(__FILE__, __LINE__, arg)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) WARN_W_CODE_LOC(__FILE__, __LINE__, args)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(arg) ERROR_W_CODE_LOC(__FILE__, __LINE__, arg)
|
||||
#endif // ERROR
|
||||
#else
|
||||
#ifndef LOG
|
||||
#define LOG(args) nb::logger.msg(args)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) nb::logger.warn(args)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args) nb::logger.error(args)
|
||||
#endif // ERROR
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#ifndef THROW_W_CODE_LOC
|
||||
#define THROW_W_CODE_LOC(file, line, args...) ERROR_W_CODE_LOC(file, line, args); throw args
|
||||
#endif // THROW_W_CODE_LOC
|
||||
#else
|
||||
#ifndef LOG
|
||||
#define LOG(args)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args)
|
||||
#endif // ERROR
|
||||
#endif // _NB_AUTOLOG
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef _NB_AUTOLOG
|
||||
#define THROW(args...) THROW_W_CODE_LOC(__FILE__, __LINE__, args)
|
||||
#else
|
||||
#define THROW(args...) throw args
|
||||
#endif // _NB_AUTOLOG
|
||||
#endif // THROW
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_ERROR
|
||||
@ -1,250 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_LOGGER
|
||||
#define _NB_LOGGER
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <NBCore/DataSink.hpp>
|
||||
#include <NBCore/ErrorsImpl.hpp>
|
||||
#include <NBCore/Printer.hpp>
|
||||
#include <NBCore/Processes.hpp>
|
||||
#include <NBCore/ThreadSafeQueue.hpp>
|
||||
#include <NBCore/TypeTraits.hpp>
|
||||
|
||||
namespace nb {
|
||||
|
||||
class ErrorBase;
|
||||
|
||||
typedef std::chrono::time_point<
|
||||
std::chrono::system_clock,
|
||||
std::chrono::nanoseconds
|
||||
> LoggerTimePoint;
|
||||
|
||||
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
||||
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
||||
|
||||
template<typename LogType, typename Logger>
|
||||
class LoggerBase : public MultithreadedDataProcessor<LogType, Logger>{
|
||||
private:
|
||||
Logger* const type_ptr = static_cast<LoggerType*>(this);
|
||||
|
||||
protected:
|
||||
using LoggerType = Logger;
|
||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||
using SinkType = DataSink<LogType>;
|
||||
using Base::process;
|
||||
std::shared_ptr<SinkType> _logsink;
|
||||
LoggerBase(std::shared_ptr<SinkType> sink) : _logsink(sink) {}
|
||||
|
||||
public:
|
||||
using Base::flush;
|
||||
bool run() override {
|
||||
_logsink->run();
|
||||
return Base::run();
|
||||
}
|
||||
using Base::pause;
|
||||
bool stop() noexcept override {
|
||||
auto ret = Base::stop();
|
||||
_logsink->stop();
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
const std::string file="";
|
||||
const unsigned int line=0;
|
||||
};
|
||||
|
||||
class LogEventHandler : public DataSink<LogEvent> {
|
||||
protected:
|
||||
using Base = DataSink<LogEvent>;
|
||||
using Base::_running;
|
||||
std::atomic<uint8_t> _logLevel;
|
||||
|
||||
public:
|
||||
using Base::isRunning;
|
||||
LogEventHandler(uint8_t logging_level=0x01);
|
||||
virtual uint8_t logLevel(uint8_t);
|
||||
virtual uint8_t logLevel() const;
|
||||
bool stop() noexcept override;
|
||||
bool run() override;
|
||||
};
|
||||
|
||||
template <typename LT>
|
||||
class DebugLogger : public LoggerBase<LogEvent, LT>{
|
||||
private:
|
||||
LT* const type_ptr = static_cast<LT*>(this);
|
||||
|
||||
protected:
|
||||
using LoggerType = LT;
|
||||
using Base = LoggerBase<LogEvent, LoggerType>;
|
||||
using SinkType = LogEventHandler;
|
||||
using SinkPtr = std::shared_ptr<SinkType>;
|
||||
using Distributor = MultiSink<LogEvent, LogEventHandler>;
|
||||
using Base::_logsink;
|
||||
using Base::process;
|
||||
std::atomic<uint8_t> _loglvl;
|
||||
virtual void write_message(
|
||||
std::string msg,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
type_ptr->in(LogEvent{
|
||||
std::chrono::system_clock::now(),
|
||||
lvl,
|
||||
msg,
|
||||
std::this_thread::get_id(),
|
||||
GetPID(),
|
||||
file,
|
||||
line
|
||||
});
|
||||
}
|
||||
template <size_t N>
|
||||
void write_message(
|
||||
char const(&msg) [N],
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
this->write_message(std::string(msg), lvl, file, line);
|
||||
}
|
||||
void write_message(
|
||||
const ErrorBase& err,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
this->write_message(err.what(), lvl, file, line);
|
||||
}
|
||||
template<typename U>
|
||||
std::enable_if_t<std::is_integral_v<U>, void> write_message(
|
||||
const U& val,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
this->write_message(std::to_string(val), lvl, file, line);
|
||||
}
|
||||
|
||||
public:
|
||||
DebugLogger(std::vector<SinkPtr> sinks={}) : Base(
|
||||
std::static_pointer_cast<DataSink<LogEvent>>(std::make_shared<Distributor>())
|
||||
) {
|
||||
for (auto sink : sinks) {
|
||||
addLogHandler(sink);
|
||||
}
|
||||
}
|
||||
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
||||
void addLogHandler(SinkPtr sink) {
|
||||
auto multsink = std::static_pointer_cast<Distributor>(_logsink);
|
||||
multsink->addSink(sink);
|
||||
}
|
||||
uint8_t minimalLogLevel() const {
|
||||
return _loglvl.load(std::memory_order_acquire);
|
||||
}
|
||||
uint8_t minimalLogLevel(uint8_t level) {
|
||||
_loglvl.store(level, std::memory_order_release);
|
||||
auto multsink = std::static_pointer_cast<Distributor>(_logsink);
|
||||
for (auto& sink : multsink->getSinks()) {
|
||||
if (sink->logLevel() < level) {
|
||||
sink->logLevel(level);
|
||||
}
|
||||
}
|
||||
return minimalLogLevel();
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void msg(
|
||||
U val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) { this->write_message(val, 0x00, file, line); }
|
||||
|
||||
template<typename U>
|
||||
void warn(
|
||||
U val,
|
||||
uint8_t lvl=0x01,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) { this->write_message(val, lvl, file, line); }
|
||||
|
||||
void error(
|
||||
const ErrorBase& val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
this->write_message(val, 0xFF, file, line);
|
||||
auto lock = this->pause();
|
||||
this->flush();
|
||||
}
|
||||
|
||||
void error(
|
||||
const std::string& val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
this->write_message(Error<>(val), 0xFF, file, line);
|
||||
auto lock = this->pause();
|
||||
this->flush();
|
||||
}
|
||||
};
|
||||
|
||||
class DefaultTerminalLogEventPrinter : public LogEventHandler {
|
||||
protected:
|
||||
using Base = LogEventHandler;
|
||||
using Base::_running;
|
||||
|
||||
public:
|
||||
using Base::isRunning;
|
||||
using Base::logLevel;
|
||||
using Base::stop;
|
||||
using Base::run;
|
||||
using Base::Base;
|
||||
bool in(const LogEvent&) override;
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
protected:
|
||||
using LoggerType = DefaultDebugLogger;
|
||||
using Base = DebugLogger<DefaultDebugLogger>;
|
||||
using MultiLogSink = MultiSink<LogEvent>;
|
||||
using SinkPtr = std::shared_ptr<LogEventHandler>;
|
||||
using Base::_logsink;
|
||||
using Base::write_message;
|
||||
virtual bool process(const LogEvent& msg) override;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::msg;
|
||||
using Base::warn;
|
||||
using Base::error;
|
||||
using Base::addLogHandler;
|
||||
using Base::minimalLogLevel;
|
||||
DefaultDebugLogger(std::vector<SinkPtr> sinks={}) : Base(sinks) {}
|
||||
~DefaultDebugLogger() { stop(); }
|
||||
|
||||
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
|
||||
};
|
||||
|
||||
extern const bool LOGGER_RUNNING;
|
||||
|
||||
extern const bool LOGGER_RUNNING;
|
||||
#ifdef _NB_AUTOLOG
|
||||
extern DefaultDebugLogger logger;
|
||||
#endif // _NB_AUTOLOG
|
||||
|
||||
// Taking Charge of Adult ADHD by Russell Barkley
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_LOGGER
|
||||
@ -1,52 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_PRINTER
|
||||
#define _NB_PRINTER
|
||||
|
||||
#include <condition_variable>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename STREAM, typename PrinterType>
|
||||
class Printer {
|
||||
private:
|
||||
PrinterType* const type_ptr = static_cast<PrinterType*>(this);
|
||||
|
||||
protected:
|
||||
STREAM& _stream;
|
||||
Printer(STREAM* const stream_) : _stream(stream_) {}
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
PrinterType& operator<<(const T& val_) {
|
||||
(*type_ptr) << val_;
|
||||
return type_ptr;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class Terminal : public Printer<decltype(std::cout), Terminal> {
|
||||
private:
|
||||
Terminal* const type_ptr = static_cast<Terminal*>(this);
|
||||
using MutexLock = std::lock_guard<std::mutex>;
|
||||
|
||||
protected:
|
||||
using Base = Printer<decltype(std::cout), Terminal>;
|
||||
using Base::_stream;
|
||||
std::mutex _mutex;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
template<typename T>
|
||||
Terminal& operator<<(const T& val_) {
|
||||
MutexLock lock(_mutex);
|
||||
_stream << val_;
|
||||
return *type_ptr;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_PRINTER
|
||||
@ -1,11 +1,16 @@
|
||||
#include <NBCore/ErrorsImpl.hpp>
|
||||
#include "ErrorsImpl.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
const std::string Error<>::type = "nb::Error";
|
||||
const ErrorCodeMap Error<>::ErrorMessages = {
|
||||
{Error::Codes::STANDARD, "std::exception"},
|
||||
{Error::Codes::UNDEFINED, "Error"}
|
||||
};
|
||||
|
||||
//ErrorBase::
|
||||
|
||||
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
|
||||
: ErrorBase(Error(exception_)) {}
|
||||
|
||||
} // namespace nb
|
||||
|
||||
: ErrorBase(Error<NoneType>(exception_)) {}
|
||||
|
||||
}
|
||||
|
||||
@ -1,82 +1,38 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <NBCore/Logger.hpp>
|
||||
#include <NBCore/StringUtils.hpp>
|
||||
#include "Logger.hpp"
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& logger_) {
|
||||
logger_.run();
|
||||
return logger_.isRunning();
|
||||
}
|
||||
#ifndef _NB_NO_LOGGER
|
||||
nb::DefaultDebugLogger logger(std::cout);
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
#ifdef _NB_AUTOLOG
|
||||
DefaultDebugLogger logger({std::static_pointer_cast<LogEventHandler>(
|
||||
std::make_shared<DefaultTerminalLogEventPrinter>(0x0)
|
||||
)});
|
||||
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||
#else
|
||||
const bool LOGGER_RUNNING = false;
|
||||
#endif // _NB_AUTOLOG
|
||||
|
||||
// class LogEventHandler
|
||||
|
||||
LogEventHandler::LogEventHandler(uint8_t logging_level) {
|
||||
_logLevel.store(logging_level, std::memory_order_release);
|
||||
}
|
||||
|
||||
bool LogEventHandler::stop() noexcept {
|
||||
_running.store(false, std::memory_order_release);
|
||||
return isRunning();
|
||||
}
|
||||
|
||||
bool LogEventHandler::run() {
|
||||
_running.store(true, std::memory_order_release);
|
||||
return isRunning();
|
||||
}
|
||||
|
||||
uint8_t LogEventHandler::logLevel(uint8_t val) {
|
||||
_logLevel.store(val, std::memory_order_release);
|
||||
return logLevel();
|
||||
}
|
||||
uint8_t LogEventHandler::logLevel() const {
|
||||
return _logLevel.load();
|
||||
}
|
||||
|
||||
// class DefaultTerminalLogEventPrinter
|
||||
|
||||
bool DefaultTerminalLogEventPrinter::in(const LogEvent& msg) {
|
||||
if (!isRunning()) {
|
||||
return false;
|
||||
}
|
||||
if (msg.lvl < logLevel()) {
|
||||
return false;
|
||||
}
|
||||
bool nb::DefaultDebugLogger::process(const LogEvent& msg) {
|
||||
constexpr size_t level_field_width = 5;
|
||||
constexpr size_t msg_prompt_length = level_field_width+7;
|
||||
std::stringstream formatted_stream;
|
||||
formatted_stream << "[";
|
||||
formatted_stream.width(level_field_width);
|
||||
formatted_stream << std::to_string(msg.lvl) << "] :: ";
|
||||
std::string fileloc="";
|
||||
if (!msg.file.empty()) {
|
||||
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << "[";
|
||||
os->width(level_field_width);
|
||||
*os << std::to_string(msg.lvl) << "] :: ";
|
||||
std::string fileloc="";
|
||||
if (!msg.file.empty()) {
|
||||
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
||||
}
|
||||
*os << indent_strblock(
|
||||
fileloc + msg.msg,
|
||||
std::string(20 - msg_prompt_length, ' '),
|
||||
""
|
||||
) << nb::NEWLINE;
|
||||
}
|
||||
formatted_stream << indent_strblock(
|
||||
fileloc + msg.msg,
|
||||
std::string(20 - msg_prompt_length, ' '),
|
||||
""
|
||||
) << nb::NEWLINE;
|
||||
std::string formatted_string = formatted_stream.str();
|
||||
std::cout << formatted_string << std::flush;
|
||||
return true;
|
||||
}
|
||||
|
||||
// class DefaultDebugLogger
|
||||
|
||||
bool DefaultDebugLogger::process(const LogEvent& msg) {
|
||||
return _logsink->in(msg);
|
||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
||||
return log.run();
|
||||
}
|
||||
|
||||
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||
|
||||
} // namespace nb
|
||||
@ -6,7 +6,7 @@
|
||||
#include <unistd.h>
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
#include <NBCore/Processes.hpp>
|
||||
#include "Processes.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#include <NBCore/StringUtils.hpp>
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
/* std::string nb::wstr_to_str(std::wstring in) {
|
||||
std::size_t wstrlen = in.length();
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#include <NBCore/Utils.hpp>
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
/* using ObjectManagerCodes = ObjectManagerError::Codes;
|
||||
using ObjectManagerCodes = ObjectManagerError::Codes;
|
||||
const std::string ObjectManagerError::type = "nb::ObjectManagerError";
|
||||
const ErrorCodeMap ObjectManagerError::ErrorMessages({
|
||||
{ObjectManagerCodes::UNDEFINED, "Error"},
|
||||
@ -22,6 +22,6 @@ const ErrorCodeMap ObjectManagerError::ErrorMessages({
|
||||
ObjectManagerCodes::BAD_THREAD,
|
||||
"Attempting operation from a bad thread"
|
||||
}
|
||||
}); */
|
||||
});
|
||||
|
||||
}; // namespace nb
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
#include "Errors.hpp"
|
||||
#include <string>
|
||||
|
||||
using namespace nb;
|
||||
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
#define _NB_AUTOLOG
|
||||
#include <exception>
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
#include <NBCore/Logger.hpp>
|
||||
#include "Errors.hpp"
|
||||
#include "Logger.hpp"
|
||||
|
||||
class TestError : public nb::Error<TestError> {
|
||||
using Base = Error<TestError>;
|
||||
@ -30,19 +29,17 @@ const nb::ErrorCodeMap TestError::ErrorMessages{
|
||||
};
|
||||
|
||||
int main() {
|
||||
while(!nb::LOGGER_RUNNING) {}
|
||||
nb::logger.minimalLogLevel(0x0);
|
||||
nb::logger.msg("Whoop!");
|
||||
nb::logger.log("Whoop!");
|
||||
try {
|
||||
THROW(TestError(0, TestError(1, TestError(2, TestError(3, TestError(2))))));
|
||||
} catch (TestError e){
|
||||
nb::logger.msg("nb::Error was thrown!");
|
||||
nb::logger.log("nb::Error was thrown!");
|
||||
}
|
||||
|
||||
try {
|
||||
THROW(std::exception());
|
||||
} catch (std::exception e){
|
||||
nb::logger.msg("std::exception was thrown!");
|
||||
nb::logger.log("std::exception was thrown!");
|
||||
}
|
||||
ERROR("hello there!");
|
||||
return 0;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <NBCore/Processes.hpp>
|
||||
#include "Processes.hpp"
|
||||
#include <Windows.h>
|
||||
|
||||
TEST(ProcessesTest, GetPID) {
|
||||
|
||||
@ -3,8 +3,8 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include <NBCore/TypeTraits.hpp>
|
||||
#include <NBCore/StringUtils.hpp>
|
||||
#include "TypeTraits.hpp"
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#include <NBCore/Events.hpp>
|
||||
#include "Events.hpp"
|
||||
|
||||
namespace NB {
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user