Skip to content

Commit

Permalink
replaced 3rd-party logger with a custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
K1ngst0m committed Oct 26, 2023
1 parent 62f70d7 commit b404b61
Show file tree
Hide file tree
Showing 49 changed files with 94 additions and 7,186 deletions.
1 change: 0 additions & 1 deletion cmake/AphExternal.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
add_subdirectory(${APH_EXTERNAL_DIR})
add_subdirectory(${APH_EXTERNAL_DIR}/mimalloc EXCLUDE_FROM_ALL)
add_subdirectory(${APH_EXTERNAL_DIR}/glfw EXCLUDE_FROM_ALL)
add_subdirectory(${APH_EXTERNAL_DIR}/reckless EXCLUDE_FROM_ALL)
add_subdirectory(${APH_EXTERNAL_DIR}/volk EXCLUDE_FROM_ALL)
add_subdirectory(${APH_EXTERNAL_DIR}/spirv-cross EXCLUDE_FROM_ALL)
add_subdirectory(${APH_EXTERNAL_DIR}/shaderc EXCLUDE_FROM_ALL)
Expand Down
7 changes: 1 addition & 6 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ add_library(engine STATIC ${base_src})
aph_compiler_options(engine)

target_include_directories(engine SYSTEM PUBLIC
${APH_EXTERNAL_DIR}/reckless/reckless/include
${APH_EXTERNAL_DIR}/volk
${APH_EXTERNAL_DIR}/glm

PRIVATE
${APH_ENGINE_DIR}
${APH_EXTERNAL_DIR}/glm
${APH_EXTERNAL_DIR}/tinyimageformat
${APH_EXTERNAL_DIR}/backward-cpp/include
${APH_EXTERNAL_DIR}/mmgr
Expand All @@ -50,13 +49,9 @@ target_link_libraries(engine PRIVATE
${xcb_libraries}
${cmake_thread_libs_init}
volk
reckless
# imgui
shaderc_shared
spirv-cross-core
slang
dl
dw
# mmgr
# spirv-cross-reflect
)
24 changes: 24 additions & 0 deletions engine/common/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,28 @@

namespace aph
{
void Logger::flush()
{
std::lock_guard<std::mutex> lock(mutex);
if(file_stream.is_open())
{
file_stream.flush();
}
std::cout.flush();
}
Logger::Logger() : log_level(Level::Debug), file_stream("log.txt", std::ofstream::app)
{
if(!file_stream.is_open())
{
std::cerr << "Failed to open log file." << std::endl;
}
}
std::string Logger::getCurrentTime()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "[%Y-%m-%d %H:%M:%S]");
return oss.str();
}
} // namespace aph
114 changes: 69 additions & 45 deletions engine/common/logger.h
Original file line number Diff line number Diff line change
@@ -1,77 +1,101 @@
#ifndef LOGGER_H_
#define LOGGER_H_

#include <reckless/severity_log.hpp>
#include <reckless/file_writer.hpp>
#include <reckless/stdout_writer.hpp>

namespace aph
{
class Logger
{
private:
using log_t = reckless::severity_log<reckless::indent<4>, // 4 spaces of indent
' ', // Field separator
reckless::severity_field // Show severity marker (D/I/W/E) first
>;
public:
enum class Level: uint8_t
{
Debug,
Info,
Warn,
Error,
None
};

Logger() : m_fileWriter{"log.txt"}, m_logger{&m_stdcoutWriter} {}
void setLogLevel(Level level) { log_level = level; }

Logger(Logger const&) = delete;
Logger& operator=(Logger const&) = delete;

public:
// Delete copy constructor and assignment operator
Logger(Logger const&) = delete;
Logger& operator=(Logger const&) = delete;
static Logger& Get()
{
static Logger instance;
return instance;
}

public:
void flush() {}
void flush();

template <typename... Args>
void debug(std::string_view fmt, Args&&... args);
void debug(std::string_view fmt, Args&&... args)
{
if(log_level <= Level::Debug)
log("D", fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void warn(std::string_view fmt, Args&&... args);
void warn(std::string_view fmt, Args&&... args)
{
if(log_level <= Level::Warn)
log("W", fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void info(std::string_view fmt, Args&&... args);
void info(std::string_view fmt, Args&&... args)
{
if(log_level <= Level::Info)
log("I", fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void error(std::string_view fmt, Args&&... args);
void error(std::string_view fmt, Args&&... args)
{
if(log_level <= Level::Error)
log("E", fmt, std::forward<Args>(args)...);
}

private:
reckless::file_writer m_fileWriter{"log.txt"};
reckless::stdout_writer m_stdcoutWriter{};
log_t m_logger;
};
Logger();

template <typename... Args>
void Logger::debug(std::string_view fmt, Args&&... args)
{
m_logger.debug(fmt.data(), std::forward<Args>(args)...);
}
// conversion for most types
template<typename T>
T to_format(const T& val) {
return val;
}

template <typename... Args>
void Logger::warn(std::string_view fmt, Args&&... args)
{
m_logger.warn(fmt.data(), std::forward<Args>(args)...);
}
template <typename... Args>
void Logger::info(std::string_view fmt, Args&&... args)
{
m_logger.info(fmt.data(), std::forward<Args>(args)...);
}
template <typename... Args>
void Logger::error(std::string_view fmt, Args&&... args)
{
m_logger.error(fmt.data(), std::forward<Args>(args)...);
}
} // namespace aph
// specialization for std::string
const char* to_format(const std::string& val) {
return val.c_str();
}

template <typename... Args>
void log(const char* level, std::string_view fmt, Args&&... args)
{
std::lock_guard<std::mutex> lock(mutex);

std::ostringstream ss;
ss << getCurrentTime() << " [" << level << "] ";
char buffer[512];
std::snprintf(buffer, sizeof(buffer), fmt.data(), to_format(args)...);
ss << buffer << "\n";

std::cout << ss.str();
if(file_stream.is_open())
{
file_stream << ss.str();
}
}

std::string getCurrentTime();

Level log_level;
std::ofstream file_stream;
std::mutex mutex;
};

} // namespace aph

#define LOG_FLUSH() \
do \
Expand Down
64 changes: 0 additions & 64 deletions external/reckless/CMakeLists.txt

This file was deleted.

20 changes: 0 additions & 20 deletions external/reckless/LICENSE.txt

This file was deleted.

1 change: 0 additions & 1 deletion external/reckless/reckless/Tuprules.lua

This file was deleted.

1 change: 0 additions & 1 deletion external/reckless/reckless/_Tuprules.tup

This file was deleted.

Loading

0 comments on commit b404b61

Please sign in to comment.