aboutsummaryrefslogtreecommitdiff
path: root/src/base/log/Logger.cpp
blob: 4332ba753a336661fe8ffa714a93c5ed4e1455aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "cru/base/log/Logger.h"
#include "cru/base/log/StdioLogTarget.h"

#include <algorithm>
#include <condition_variable>
#include <ctime>
#include <format>
#include <memory>
#include <mutex>

#ifdef CRU_PLATFORM_WINDOWS
#include "cru/base/platform/win/DebugLogTarget.h"
#endif

namespace cru::log {
Logger *Logger::GetInstance() {
  static Logger logger;

  logger.AddLogTarget(std::make_unique<StdioLogTarget>());

#ifdef CRU_PLATFORM_WINDOWS
  logger.AddLogTarget(std::make_unique<platform::win::WinDebugLogTarget>());
#endif

  return &logger;
}

void Logger::AddLogTarget(std::unique_ptr<ILogTarget> target) {
  std::lock_guard<std::mutex> lock(target_list_mutex_);
  target_list_.push_back(std::move(target));
}

void Logger::RemoveLogTarget(ILogTarget *target) {
  std::lock_guard<std::mutex> lock(target_list_mutex_);
  target_list_.erase(
      std::remove_if(target_list_.begin(), target_list_.end(),
                     [target](const auto &t) { return t.get() == target; }),
      target_list_.end());
}

namespace {
const char *LogLevelToString(LogLevel level) {
  switch (level) {
    case LogLevel::Debug:
      return "DEBUG";
    case LogLevel::Info:
      return "INFO";
    case LogLevel::Warn:
      return "WARN";
    case LogLevel::Error:
      return "ERROR";
    default:
      std::terminate();
  }
}

std::string GetLogTime() {
  auto time = std::time(nullptr);
  auto calendar = std::localtime(&time);
  return std::format("{}:{}:{}", calendar->tm_hour, calendar->tm_min,
                     calendar->tm_sec);
}

std::string MakeLogFinalMessage(const LogInfo &log_info) {
  return std::format("[{}] {} {}: {}\n", GetLogTime(),
                     LogLevelToString(log_info.level), log_info.tag,
                     log_info.message);
}
}  // namespace

Logger::Logger() : log_stop_(false), log_thread_(&Logger::LogThreadRun, this) {}

Logger::~Logger() {
  {
    std::unique_lock lock(log_queue_mutex_);
    log_stop_ = true;
    log_queue_condition_variable_.notify_one();
  }
  log_thread_.join();
}

void Logger::Log(LogInfo log_info) {
#ifndef CRU_DEBUG
  if (log_info.level == LogLevel::Debug) {
    return;
  }
#endif

  std::unique_lock lock(log_queue_mutex_);
  log_queue_.push_back(std::move(log_info));
  log_queue_condition_variable_.notify_one();
}
void Logger::LogThreadRun() {
  while (true) {
    std::list<LogInfo> queue;
    bool stop = false;
    std::vector<ILogTarget *> target_list;

    {
      std::unique_lock lock(log_queue_mutex_);
      log_queue_condition_variable_.wait(
          lock, [this] { return !log_queue_.empty() || log_stop_; });
      std::swap(queue, log_queue_);
      stop = log_stop_;
    }

    {
      std::lock_guard<std::mutex> lock_guard(target_list_mutex_);
      for (const auto &target : target_list_) {
        target_list.push_back(target.get());
      }
    }

    for (const auto &target : target_list) {
      for (auto &log_info : queue) {
        target->Write(log_info.level, MakeLogFinalMessage(log_info));
      }
    }

    // TODO: Should still wait for queue to be cleared.
    if (stop) return;
  }
}

LoggerCppStream::LoggerCppStream(Logger *logger, LogLevel level,
                                 std::string tag)
    : logger_(logger), level_(level), tag_(std::move(tag)) {}

LoggerCppStream LoggerCppStream::WithLevel(LogLevel level) const {
  return LoggerCppStream(this->logger_, level, this->tag_);
}

LoggerCppStream LoggerCppStream::WithTag(std::string tag) const {
  return LoggerCppStream(this->logger_, this->level_, std::move(tag));
}

void LoggerCppStream::Consume(std::string_view str) {
  this->logger_->Log(this->level_, this->tag_, std::string(str));
}

}  // namespace cru::log