libfilezilla
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
logger.hpp
1 #ifndef LIBFILEZILLA_LOGGER_HEADER
2 #define LIBFILEZILLA_LOGGER_HEADER
3 
4 #include "format.hpp"
5 
6 #include <atomic>
7 
8 namespace fz {
9 namespace logmsg
10 {
11  enum type : uint64_t
12  {
14  status = 1ull,
15 
17  error = 1ull << 1,
18 
20  command = 1ull << 2,
21 
23  reply = 1ull << 3,
24 
26  debug_warning = 1ull << 4,
27  debug_info = 1ull << 5,
28  debug_verbose = 1ull << 6,
29  debug_debug = 1ull << 7,
30 
31 
32  private1 = 1ull << 31,
33  private32 = 1ull << 63
34  };
35 }
36 
38 {
39 public:
40  logger_interface() = default;
41  virtual ~logger_interface() = default;
42 
43  logger_interface(logger_interface const&) = delete;
44  logger_interface& operator=(logger_interface const&) = delete;
45 
46 
48  virtual void do_log(logmsg::type t, std::wstring && msg) = 0;
49 
50  template<typename String, typename...Args>
51  void log(logmsg::type t, String&& fmt, Args&& ...args)
52  {
53  if (should_log(t)) {
54  std::wstring formatted = fz::to_wstring(fz::sprintf(std::forward<String>(fmt), std::forward<Args>(args)...));
55  do_log(t, std::move(formatted));
56  }
57  }
58 
59  template<typename String>
60  void log_raw(logmsg::type t, String&& msg)
61  {
62  if (should_log(t)) {
63  std::wstring formatted = fz::to_wstring(std::forward<String>(msg));
64  do_log(t, std::move(formatted));
65  }
66  }
67 
68  bool should_log(logmsg::type t) const {
69  return level_ & t;
70  }
71 
72  void set_all(logmsg::type t) {
73  level_ = t;
74  }
75 
76  void set(logmsg::type t, bool flag) {
77  if (flag) {
78  enable(t);
79  }
80  else {
81  disable(t);
82  }
83  }
84 
85  void enable(logmsg::type t) {
86  level_ |= t;
87  }
88  void disable(logmsg::type t) {
89  level_ &= ~t;
90  }
91 
92 
93 protected:
94  std::atomic<uint64_t> level_{logmsg::status | logmsg::error | logmsg::command | logmsg::reply};
95 };
96 }
97 
98 #endif
99 
std::wstring to_wstring(std::string_view const &in)
Converts from std::string in system encoding into std::wstring.
virtual void do_log(logmsg::type t, std::wstring &&msg)=0
The one thing you need to override.
Header for the sprintf string formatting function.
std::string sprintf(std::string_view const &fmt, Args &&...args)
A simple type-safe sprintf replacement.
Definition: format.hpp:406
The namespace used by libfilezilla.
Definition: apply.hpp:16
Definition: logger.hpp:37