aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/Logger.hpp
blob: f76e46264779c3fed1a55bc455d3e1ca1650983b (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
#pragma once
#include "cru/common/Base.hpp"

#include <fmt/format.h>
#include <fmt/ostream.h>
#include <iostream>
#include <list>
#include <memory>
#include <string_view>

namespace cru::log {

enum class LogLevel { Debug, Info, Warn, Error };

struct ILogSource : 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_view s) = 0;
};

class StdioLogSource : public virtual ILogSource {
 public:
  StdioLogSource() = default;

  CRU_DELETE_COPY(StdioLogSource)
  CRU_DELETE_MOVE(StdioLogSource)

  ~StdioLogSource() override = default;

  void Write(LogLevel level, std::string_view s) override {
    // TODO: Emmm... Since it is buggy to use narrow char in UTF-8 on Windows. I
    // think this implementation might be broken. (However, I didn't test it.)
    // Maybe, I should detect Windows and use wide char (And I didn't test this
    // either) or other more complicated implementation. Currently, I settled
    // with this.
    if (level == LogLevel::Error) {
      std::cerr << s;
    } else {
      std::cout << s;
    }
  }
};

class Logger : public Object {
 public:
  static Logger* GetInstance();

 public:
  Logger() = default;

  CRU_DELETE_COPY(Logger)
  CRU_DELETE_MOVE(Logger)

  ~Logger() override = default;

 public:
  void AddSource(std::unique_ptr<ILogSource> source);
  void RemoveSource(ILogSource* source);

 public:
  void Log(LogLevel level, std::string_view s);
  void Log(LogLevel level, std::string_view tag, std::string_view s);

 public:
  std::list<std::unique_ptr<ILogSource>> sources_;
};

template <typename... TArgs>
void Debug([[maybe_unused]] TArgs&&... args) {
#ifdef CRU_DEBUG
  Logger::GetInstance()->Log(LogLevel::Debug,
                             fmt::format(std::forward<TArgs>(args)...));
#endif
}

template <typename... TArgs>
void Info(TArgs&&... args) {
  Logger::GetInstance()->Log(LogLevel::Info,
                             fmt::format(std::forward<TArgs>(args)...));
}

template <typename... TArgs>
void Warn(TArgs&&... args) {
  Logger::GetInstance()->Log(LogLevel::Warn,
                             fmt::format(std::forward<TArgs>(args)...));
}

template <typename... TArgs>
void Error(TArgs&&... args) {
  Logger::GetInstance()->Log(LogLevel::Error,
                             fmt::format(std::forward<TArgs>(args)...));
}

template <typename... TArgs>
void TagDebug([[maybe_unused]] std::string_view tag,
              [[maybe_unused]] TArgs&&... args) {
#ifdef CRU_DEBUG
  Logger::GetInstance()->Log(LogLevel::Debug, tag,
                             fmt::format(std::forward<TArgs>(args)...));
#endif
}

template <typename... TArgs>
void TagInfo(std::string_view tag, TArgs&&... args) {
  Logger::GetInstance()->Log(LogLevel::Info, tag,
                             fmt::format(std::forward<TArgs>(args)...));
}

template <typename... TArgs>
void TagWarn(std::string_view tag, TArgs&&... args) {
  Logger::GetInstance()->Log(LogLevel::Warn, tag,
                             fmt::format(std::forward<TArgs>(args)...));
}

template <typename... TArgs>
void TagError(std::string_view tag, TArgs&&... args) {
  Logger::GetInstance()->Log(LogLevel::Error, tag,
                             fmt::format(std::forward<TArgs>(args)...));
}
}  // namespace cru::log