-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from nodec-project/feature/issue60
Feature/issue60
- Loading branch information
Showing
22 changed files
with
1,038 additions
and
459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.