libfilezilla
logger.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_LOGGER_HEADER
2 #define LIBFILEZILLA_LOGGER_HEADER
3 
8 #include "format.hpp"
9 
10 #include <atomic>
11 
12 namespace fz {
13 namespace logmsg
14 {
15  enum type : uint64_t
16  {
18  status = 1ull,
19 
21  error = 1ull << 1,
22 
24  command = 1ull << 2,
25 
27  reply = 1ull << 3,
28 
30  debug_warning = 1ull << 4,
31  debug_info = 1ull << 5,
32  debug_verbose = 1ull << 6,
33  debug_debug = 1ull << 7,
34 
37  custom1 = 1ull << 32,
38  custom32 = 1ull << 63
39  };
40 }
41 
50 class FZ_PUBLIC_SYMBOL logger_interface
51 {
52 public:
53  logger_interface() noexcept = default;
54  virtual ~logger_interface() = default;
55 
56  logger_interface(logger_interface const&) = delete;
57  logger_interface& operator=(logger_interface const&) = delete;
58 
59 
61  virtual void do_log(logmsg::type t, std::wstring && msg) = 0;
62 
68  template<typename String, typename...Args>
69  void log(logmsg::type t, String&& fmt, Args&& ...args)
70  {
71  if (should_log(t)) {
72  std::wstring formatted = fz::sprintf(fz::to_wstring(std::forward<String>(fmt)), args...);
73  do_log(t, std::move(formatted));
74  }
75  }
76 
82  template<typename String, typename...Args>
83  void log_u(logmsg::type t, String&& fmt, Args const& ...args)
84  {
85  if (should_log(t)) {
86  std::wstring formatted = fz::sprintf(fz::to_wstring(std::forward<String>(fmt)), assume_strings_are_utf8(args)...);
87  do_log(t, std::move(formatted));
88  }
89  }
90 
92  template<typename String>
93  void log_raw(logmsg::type t, String&& msg)
94  {
95  if (should_log(t)) {
96  std::wstring formatted = fz::to_wstring(std::forward<String>(msg));
97  do_log(t, std::move(formatted));
98  }
99  }
100 
102  bool should_log(logmsg::type t) const {
103  return level_ & t;
104  }
105 
107  logmsg::type levels() const {
108  return static_cast<logmsg::type>(level_.load());
109  }
110 
112  virtual void set_all(logmsg::type t) {
113  level_ = t;
114  }
115 
117  void set(logmsg::type t, bool flag) {
118  if (flag) {
119  enable(t);
120  }
121  else {
122  disable(t);
123  }
124  }
125 
127  virtual void enable(logmsg::type t) {
128  level_ |= t;
129  }
130 
132  virtual void disable(logmsg::type t) {
133  level_ &= ~t;
134  }
135 
136 protected:
137  std::atomic<uint64_t> level_{logmsg::status | logmsg::error | logmsg::command | logmsg::reply};
138 
139 private:
140  std::wstring assume_strings_are_utf8(std::string_view const& arg) {
141  return fz::to_wstring_from_utf8(arg);
142  }
143  std::wstring assume_strings_are_utf8(std::string const& arg) {
144  return fz::to_wstring_from_utf8(arg);
145  }
146  std::wstring assume_strings_are_utf8(char const* arg) {
147  return fz::to_wstring_from_utf8(arg);
148  }
149 
150  template <typename T>
151  T const& assume_strings_are_utf8(T const& arg) {
152  return arg;
153  }
154 };
155 
157 class FZ_PUBLIC_SYMBOL null_logger final : public logger_interface
158 {
159 public:
160  null_logger() noexcept
161  {
162  disable(levels());
163  }
164 
165  virtual void do_log(logmsg::type, std::wstring &&) override {}
166  virtual void set_all(logmsg::type) override {}
167  virtual void enable(logmsg::type) override {}
168 };
169 
170 null_logger FZ_PUBLIC_SYMBOL & get_null_logger();
171 
173 class FZ_PUBLIC_SYMBOL stdout_logger final : public logger_interface
174 {
175 public:
176  virtual void do_log(logmsg::type, std::wstring &&) override;
177 };
178 
179 
180 }
181 
182 #endif
A simple logger that writes to stdout.
Definition: logger.hpp:173
virtual void set_all(logmsg::type t)
Sets which message types should be logged.
Definition: logger.hpp:112
std::wstring to_wstring(std::string_view const &in)
Converts from std::string in system encoding into std::wstring.
virtual void enable(logmsg::type) override
Enables logging for the passed message types.
Definition: logger.hpp:167
Header for the sprintf string formatting function.
virtual void set_all(logmsg::type) override
Sets which message types should be logged.
Definition: logger.hpp:166
std::string sprintf(std::string_view const &fmt, Args &&...args)
A simple type-safe sprintf replacement.
Definition: format.hpp:456
logmsg::type levels() const
Returns all currently enabled log levels.
Definition: logger.hpp:107
std::wstring to_wstring_from_utf8(std::string_view const &in)
Converts from std::string in UTF-8 into std::wstring.
virtual void enable(logmsg::type t)
Enables logging for the passed message types.
Definition: logger.hpp:127
virtual void disable(logmsg::type t)
Disables logging for the passed message types.
Definition: logger.hpp:132
bool should_log(logmsg::type t) const
Is any of the passed log levels set.
Definition: logger.hpp:102
void log(logmsg::type t, String &&fmt, Args &&...args)
Definition: logger.hpp:69
void log_raw(logmsg::type t, String &&msg)
Logs the raw string, it is not treated as format string.
Definition: logger.hpp:93
void set(logmsg::type t, bool flag)
Sets whether the given types should be logged.
Definition: logger.hpp:117
The namespace used by libfilezilla.
Definition: apply.hpp:17
void log_u(logmsg::type t, String &&fmt, Args const &...args)
Definition: logger.hpp:83
virtual void do_log(logmsg::type, std::wstring &&) override
The one thing you need to override.
Definition: logger.hpp:165
A logger that does not log anything.
Definition: logger.hpp:157
Abstract interface for logging strings.
Definition: logger.hpp:50