Skip to content

Commit

Permalink
logger: add methods for simple use
Browse files Browse the repository at this point in the history
  • Loading branch information
ctapmex committed Oct 19, 2024
1 parent 84e5d8b commit 8c1d10c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
23 changes: 15 additions & 8 deletions src/colorer/common/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void format_log_string(std::ostream& out, std::string_view format, Args&&... arg

#include "unicode/unistr.h"
namespace details {
template <> inline
void ArgumentT<icu::UnicodeString>::print(std::ostream& out) const
template <>
inline void ArgumentT<icu::UnicodeString>::print(std::ostream& out) const
{
std::string result8;
mData.toUTF8String(result8);
Expand All @@ -66,13 +66,13 @@ void ArgumentT<icu::UnicodeString>::print(std::ostream& out) const
#else
#include "colorer/strings/legacy/strings.h"
namespace details {
template <> inline
void details::ArgumentT<UnicodeString>::print(std::ostream& out) const
template <>
inline void details::ArgumentT<UnicodeString>::print(std::ostream& out) const
{
std::string const result8 = mData.getChars();
out << result8;
}
}
} // namespace details
#endif

class Logger
Expand All @@ -83,13 +83,20 @@ class Logger
virtual ~Logger() = default;
virtual void log(LogLevel level, const char* filename_in, int line_in, const char* funcname_in,
const char* message) = 0;
virtual void flush() = 0;
};

class Log
{
public:

static void registerLogger(Logger& logger_) { logger = &logger_; }
static void removeLogger() { logger = nullptr; }
static void flush()
{
if (logger) {
logger->flush();
}
}

template <typename... Args>
static void log(const Logger::LogLevel level, const char* filename_in, const int line_in, const char* funcname_in,
Expand All @@ -106,7 +113,8 @@ class Log
static Logger* logger;
};

#define COLORER_LOGGER_PRINTF(level, ...) Log::log(level, __FILE__, __LINE__, static_cast<const char *>(__FUNCTION__), __VA_ARGS__)
#define COLORER_LOGGER_PRINTF(level, ...) \
Log::log(level, __FILE__, __LINE__, static_cast<const char*>(__FUNCTION__), __VA_ARGS__)
#define COLORER_LOG_ERROR(...) COLORER_LOGGER_PRINTF(Logger::LogLevel::LOG_ERROR, __VA_ARGS__)
#define COLORER_LOG_WARN(...) COLORER_LOGGER_PRINTF(Logger::LogLevel::LOG_WARN, __VA_ARGS__)
#define COLORER_LOG_INFO(...) COLORER_LOGGER_PRINTF(Logger::LogLevel::LOG_INFO, __VA_ARGS__)
Expand All @@ -119,5 +127,4 @@ class Log
#define COLORER_LOG_DEEPTRACE(...)
#endif


#endif // COLORER_LOGGER_H
28 changes: 25 additions & 3 deletions tools/colorer/SimpleLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define SIMPLELOGGER_H

#include <ctime>
#include <fstream>
#include <locale>
#include "colorer/common/Logger.h"

class SimpleLogger : public Logger
{
Expand All @@ -15,7 +17,16 @@ class SimpleLogger : public Logger
ofs.open(filename.c_str(), std::ofstream::out | std::ofstream::app);
}

~SimpleLogger() override { ofs.close(); }
SimpleLogger(const std::string& filename, const Logger::LogLevel log_level)
{
current_level = log_level;
ofs.open(filename.c_str(), std::ofstream::out | std::ofstream::app);
}

~SimpleLogger() override
{
ofs.close();
}

void log(Logger::LogLevel level, const char* /*filename_in*/, int /*line_in*/, const char* /*funcname_in*/, const char* message)
{
Expand All @@ -30,7 +41,7 @@ class SimpleLogger : public Logger
ofs << message << '\n';
}

Logger::LogLevel getLogLevel(const std::string& log_level)
static Logger::LogLevel getLogLevel(const std::string& log_level)
{
int i = 0;
for (auto it : LogLevelStr) {
Expand All @@ -40,11 +51,22 @@ class SimpleLogger : public Logger
i++;
}
if (log_level == "warn") {
current_level = Logger::LOG_WARN;
return Logger::LOG_WARN;
}
return Logger::LOG_OFF;
}

void setLogLevel(Logger::LogLevel level)
{
ofs.flush();
current_level = level;
}

void flush()
{
ofs.flush();
}

private:
std::ofstream ofs;
Logger::LogLevel current_level;
Expand Down

0 comments on commit 8c1d10c

Please sign in to comment.