|
1 | 1 | #include "Logging.h"
|
2 |
| -#include <spdlog/spdlog.h> |
3 | 2 |
|
4 |
| -Techstorm::Logger::Logger() |
| 3 | + |
| 4 | + |
| 5 | +void Techstorm::Logger::log(const std::string& message, ELogLevel level, const std::source_location& location) |
5 | 6 | {
|
| 7 | + std::string locFileName = location.file_name(); |
| 8 | + |
| 9 | + // shorten the filename to only the last part of it |
| 10 | + locFileName = locFileName.substr(locFileName.find_last_of("/\\") + 1); |
| 11 | + |
| 12 | + |
| 13 | + int locLine = location.line(); |
| 14 | + |
| 15 | + std::string finalMessage = "[" + locFileName + ":" + std::to_string(locLine) + "] " + message; |
| 16 | + |
| 17 | + switch (level) |
| 18 | + { |
| 19 | + case Techstorm::ELogLevel::TRACE: |
| 20 | + mLogger->trace(finalMessage); |
| 21 | + break; |
| 22 | + case Techstorm::ELogLevel::DEBUG: |
| 23 | + mLogger->debug(finalMessage); |
| 24 | + break; |
| 25 | + case Techstorm::ELogLevel::INFO: |
| 26 | + mLogger->info(finalMessage); |
| 27 | + break; |
| 28 | + case Techstorm::ELogLevel::WARNING: |
| 29 | + mLogger->warn(finalMessage); |
| 30 | + break; |
| 31 | + case Techstorm::ELogLevel::ERROR: |
| 32 | + mLogger->error(finalMessage); |
| 33 | + break; |
| 34 | + case Techstorm::ELogLevel::FATAL: |
| 35 | + mLogger->critical(finalMessage); |
| 36 | + break; |
| 37 | + case Techstorm::ELogLevel::NONE: |
| 38 | + break; |
| 39 | + default: |
| 40 | +#ifdef RELEASE |
| 41 | + // if on release, log this as an error |
| 42 | + std::string failMsg = "Failed to log the message: " + finalMessage; |
| 43 | + failMsg << "Because the log level " << level << " is not supported!"; |
| 44 | + mLogger->error(failMsg); |
| 45 | + |
| 46 | +#else |
| 47 | + mLogger->trace(finalMessage); |
| 48 | +#endif |
| 49 | + break; |
| 50 | + } |
6 | 51 | }
|
7 | 52 |
|
8 |
| -void Techstorm::Logger::Log(const std::string& message, const std::source_location& location) |
| 53 | +void Techstorm::TerminalSink::init() |
9 | 54 | {
|
| 55 | + mColorSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); |
| 56 | + mColorSink->set_level(spdlog::level::warn); |
| 57 | + mColorSink->set_pattern(mFormat); |
| 58 | +} |
| 59 | + |
| 60 | +std::string Techstorm::TerminalSink::getFormat() |
| 61 | +{ |
| 62 | + return this->mFormat; |
| 63 | +} |
| 64 | + |
| 65 | +std::string Techstorm::CreateLogFileName() { |
| 66 | + // Filename should follow this format: YYYY-MM-DD_HH-MM-SS.log |
| 67 | + time_t now = time(0); |
| 68 | + struct tm tstruct; |
| 69 | + localtime_s(&tstruct, &now); |
| 70 | + char buf[80]; |
| 71 | + strftime(buf, sizeof(buf), "%Y-%m-%d_%H-%M-%S", &tstruct); |
| 72 | + |
| 73 | + std::string name = std::string(TS_TEMP_DIR); |
| 74 | + name += "logs/"; |
| 75 | + name += std::string(buf); |
| 76 | + name += ".log"; |
| 77 | + |
| 78 | + return name; |
| 79 | +} |
| 80 | + |
| 81 | +spdlog::level::level_enum Techstorm::GetSpdlogLevel(ELogLevel level) { |
| 82 | + return static_cast<spdlog::level::level_enum>(level); |
| 83 | +} |
| 84 | + |
| 85 | +void Techstorm::Log(const std::string& message, ELogLevel level, const std::source_location& location) { |
| 86 | + Logger::Instance().log(message, level, location); |
10 | 87 | }
|
0 commit comments