aboutsummaryrefslogtreecommitdiff
path: root/include/cru/base/log/Logger.h
blob: 3ed119e22392f4aea1e8134882735f4f49dbf3b3 (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
#pragma once
#include "../Base.h"

#include "../concurrent/ConcurrentQueue.h"

#include <format>  // IWYU pragma: keep
#include <memory>
#include <mutex>
#include <thread>
#include <utility>
#include <vector>

namespace cru::log {
enum class LogLevel { Debug, Info, Warn, Error };

struct CRU_BASE_API LogInfo {
  LogInfo(LogLevel level, std::string tag, std::string message)
      : level(level), tag(std::move(tag)), message(std::move(message)) {}

  LogLevel level;
  std::string tag;
  std::string message;
};

struct CRU_BASE_API ILogTarget : virtual Interface {
  // Write the string s. LogLevel is just a helper. It has no effect on the
  // content to write.
  virtual void Write(LogLevel level, std::string s) = 0;
};

class CRU_BASE_API Logger : public Object2 {
 public:
  static Logger* GetInstance();

 public:
  Logger();
  ~Logger() override;

 public:
  void AddLogTarget(std::unique_ptr<ILogTarget> source);
  void RemoveLogTarget(ILogTarget* source);

 public:
  void Log(LogLevel level, std::string tag, std::string message) {
    Log(LogInfo(level, std::move(tag), std::move(message)));
  }
  void Log(LogInfo log_info);

  template <typename... Args>
  void FormatLog(LogLevel level, std::string tag,
                 std::string_view message_format, Args&&... args) {
    // Clang is buggy in consteval, so we can't use std::format for now.
    Log(level, std::move(tag),
        std::vformat(message_format, std::make_format_args(args...)));
  }

 private:
  concurrent::ConcurrentQueue<LogInfo> log_queue_;

  std::mutex target_list_mutex_;
  std::vector<std::unique_ptr<ILogTarget>> target_list_;

  std::thread log_thread_;
};

class CRU_BASE_API LoggerCppStream : public Object2 {
 public:
  explicit LoggerCppStream(Logger* logger, LogLevel level, std::string tag);
  ~LoggerCppStream() override = default;

  LoggerCppStream WithLevel(LogLevel level) const;
  LoggerCppStream WithTag(std::string tag) const;

 private:
  void Consume(std::string_view str);

 public:
  LoggerCppStream& operator<<(std::string_view str) {
    this->Consume(str);
    return *this;
  }

  template <typename T>
  LoggerCppStream& operator<<(T&& arg) {
    this->Consume(ToString(std::forward<T>(arg)));
    return *this;
  }

 private:
  Logger* logger_;
  LogLevel level_;
  std::string tag_;
};

}  // namespace cru::log

#define CRU_LOG_TAG_DEBUG(...)                                          \
  cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Debug, \
                                             kLogTag, __VA_ARGS__)

#define CRU_LOG_TAG_INFO(...)                                          \
  cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Info, \
                                             kLogTag, __VA_ARGS__)

#define CRU_LOG_TAG_WARN(...)                                          \
  cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Warn, \
                                             kLogTag, __VA_ARGS__)

#define CRU_LOG_TAG_ERROR(...)                                          \
  cru::log::Logger::GetInstance()->FormatLog(cru::log::LogLevel::Error, \
                                             kLogTag, __VA_ARGS__)