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 
35 
36  private1 = 1ull << 31,
37  private32 = 1ull << 63
38  };
39 }
40 
50 {
51 public:
52  logger_interface() = default;
53  virtual ~logger_interface() = default;
54 
55  logger_interface(logger_interface const&) = delete;
56  logger_interface& operator=(logger_interface const&) = delete;
57 
58 
60  virtual void do_log(logmsg::type t, std::wstring && msg) = 0;
61 
63  template<typename String, typename...Args>
64  void log(logmsg::type t, String&& fmt, Args&& ...args)
65  {
66  if (should_log(t)) {
67  std::wstring formatted = fz::to_wstring(fz::sprintf(std::forward<String>(fmt), std::forward<Args>(args)...));
68  do_log(t, std::move(formatted));
69  }
70  }
71 
73  template<typename String>
74  void log_raw(logmsg::type t, String&& msg)
75  {
76  if (should_log(t)) {
77  std::wstring formatted = fz::to_wstring(std::forward<String>(msg));
78  do_log(t, std::move(formatted));
79  }
80  }
81 
82  bool should_log(logmsg::type t) const {
83  return level_ & t;
84  }
85 
88  level_ = t;
89  }
90 
92  void set(logmsg::type t, bool flag) {
93  if (flag) {
94  enable(t);
95  }
96  else {
97  disable(t);
98  }
99  }
100 
103  level_ |= t;
104  }
105 
108  level_ &= ~t;
109  }
110 
111 protected:
112  std::atomic<uint64_t> level_{logmsg::status | logmsg::error | logmsg::command | logmsg::reply};
113 };
114 }
115 
116 #endif
117 
fz::logger_interface::set_all
void set_all(logmsg::type t)
Sets which message types should be logged.
Definition: logger.hpp:87
format.hpp
Header for the sprintf string formatting function.
fz::logmsg::reply
@ reply
Replies, aimed at the users.
Definition: logger.hpp:27
fz::logmsg::error
@ error
Error messages aimed at the user.
Definition: logger.hpp:21
fz::logger_interface::enable
void enable(logmsg::type t)
Enables logging for the passed message types.
Definition: logger.hpp:102
fz::logmsg::debug_warning
@ debug_warning
Debug messages aimed at developers.
Definition: logger.hpp:30
fz::logmsg::command
@ command
Commands, aimed at the users.
Definition: logger.hpp:24
fz::logmsg::type
type
Definition: logger.hpp:16
fz::sprintf
std::string sprintf(std::string_view const &fmt, Args &&... args)
A simple type-safe sprintf replacement.
Definition: format.hpp:407
fz::logger_interface::disable
void disable(logmsg::type t)
Disables logging for the passed message types.
Definition: logger.hpp:107
fz::logger_interface
Abstract interface for logging strings.
Definition: logger.hpp:50
fz::to_wstring
std::wstring to_wstring(std::string_view const &in)
Converts from std::string in system encoding into std::wstring.
fz::logger_interface::log
void log(logmsg::type t, String &&fmt, Args &&...args)
The.
Definition: logger.hpp:64
fz::logger_interface::log_raw
void log_raw(logmsg::type t, String &&msg)
Logs the raw string, it is not treated as format string.
Definition: logger.hpp:74
fz::logger_interface::set
void set(logmsg::type t, bool flag)
Sets whether the given types should be logged.
Definition: logger.hpp:92
fz::logmsg::status
@ status
Generic status messages aimed at the user.
Definition: logger.hpp:18
fz
The namespace used by libfilezilla.
Definition: apply.hpp:17
fz::logger_interface::do_log
virtual void do_log(logmsg::type t, std::wstring &&msg)=0
The one thing you need to override.