Skip to content

Commit

Permalink
Merge pull request #61 from nodec-project/feature/issue60
Browse files Browse the repository at this point in the history
Feature/issue60
  • Loading branch information
ContentsViewer authored Nov 2, 2023
2 parents 32bef7f + 1f5cc97 commit cde0074
Show file tree
Hide file tree
Showing 22 changed files with 1,038 additions and 459 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Run tests
working-directory: build
run: |
ctest -C Debug --output-on-failure
ctest -C Debug --verbose
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0)
project(nodec LANGUAGES CXX)

add_library(${PROJECT_NAME} STATIC
src/nodec/logging.cpp
# src/nodec/logging.cpp
src/nodec/unicode.cpp
)

Expand Down
207 changes: 207 additions & 0 deletions docs/logging.drawio

Large diffs are not rendered by default.

188 changes: 187 additions & 1 deletion docs/signals.drawio

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions include/nodec/concurrent/thread_pool_executor.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef NODEC__CONCURRENT__THREAD_POOL_EXECUTOR_HPP_
#define NODEC__CONCURRENT__THREAD_POOL_EXECUTOR_HPP_

#include <nodec/logging.hpp>

#include <atomic>
#include <cstdint>
#include <functional>
Expand Down
159 changes: 0 additions & 159 deletions include/nodec/logging.hpp

This file was deleted.

54 changes: 54 additions & 0 deletions include/nodec/logging/formatters/simple_formatter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef NODEC__LOGGING__SIMPLE_FORMATTER_HPP_
#define NODEC__LOGGING__SIMPLE_FORMATTER_HPP_

#include <sstream>

#include "../log_record.hpp"

namespace nodec {
namespace logging {
namespace formatters {

struct SimpleFormatter {
std::string operator()(const LogRecord &record) {
std::ostringstream oss;

switch (record.level) {
case nodec::logging::Level::Unset:
oss << "[UNSET]";
break;
case nodec::logging::Level::Debug:
oss << "[DEBUG]";
break;
case nodec::logging::Level::Info:
oss << "[INFO] ";
break;
case nodec::logging::Level::Warn:
oss << "[WARN] ";
break;
case nodec::logging::Level::Error:
oss << "[ERROR]";
break;
case nodec::logging::Level::Fatal:
oss << "[FATAL]";
break;
default:
oss << "[???] ";
break;
}

if (!record.name.empty()) {
oss << " [" << record.name << "]";
}

oss << " - " << record.message << "\n";
oss << "(" << record.file << " line " << record.line << ")\n";

return oss.str();
}
};

} // namespace formatters
} // namespace logging
} // namespace nodec
#endif
38 changes: 38 additions & 0 deletions include/nodec/logging/handlers/stdout_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef NODEC__LOGGING__HANDLERS__STDOUT_HANDLER_HPP_
#define NODEC__LOGGING__HANDLERS__STDOUT_HANDLER_HPP_

#include <iostream>
#include <mutex>

#include "../log_record.hpp"

namespace nodec {
namespace logging {
namespace handlers {

struct ConsoleMutex {
static std::mutex &get() {
static std::mutex mutex;
return mutex;
}
};

template<class Formatter>
class StdoutHandler {
private:
public:
StdoutHandler(Formatter formatter = Formatter{})
: formatter_(formatter) {}

void operator()(const LogRecord &record) {
std::lock_guard<std::mutex> lock(ConsoleMutex::get());
std::cout << formatter_(record);
}

private:
Formatter formatter_;
};
} // namespace handlers
} // namespace logging
} // namespace nodec
#endif
38 changes: 38 additions & 0 deletions include/nodec/logging/level.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef NODEC__LOGGING__LEVEL_HPP_
#define NODEC__LOGGING__LEVEL_HPP_

#include <ostream>

namespace nodec {
namespace logging {

enum class Level {
Unset = 0, //! The unset log level
Debug = 10, //! The debug log level
Info = 20, //! The info log level
Warn = 30, //! The warn log level
Error = 40, //! The error log level
Fatal = 50 //! The fatal log level
};

inline std::ostream &operator<<(std::ostream &stream, const nodec::logging::Level &level) {
switch (level) {
case nodec::logging::Level::Unset:
return stream << "Unset";
case nodec::logging::Level::Debug:
return stream << "Debug";
case nodec::logging::Level::Info:
return stream << "Info";
case nodec::logging::Level::Warn:
return stream << "Warn";
case nodec::logging::Level::Error:
return stream << "Error";
case nodec::logging::Level::Fatal:
return stream << "Fatal";
}
return stream << "Unknown";
}

} // namespace logging
} // namespace nodec
#endif
25 changes: 25 additions & 0 deletions include/nodec/logging/log_record.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef NODEC__LOGGING__LOG_RECORD_HPP_
#define NODEC__LOGGING__LOG_RECORD_HPP_

#include <cstddef>
#include <string>

#include "level.hpp"

namespace nodec {
namespace logging {

struct LogRecord {
LogRecord(const std::string &name, Level level, const std::string &message, const char *file, std::size_t line)
: name(name), level(level), message(message), file(file), line(line) {}

const std::string &name;
Level level;
const std::string &message;
const char *file;
std::size_t line;
};

} // namespace logging
} // namespace nodec
#endif
Loading

0 comments on commit cde0074

Please sign in to comment.