WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <optional>
4#include <vector>
5#include <chrono>
6#include "field.h"
7#ifdef WEBCFACE_MESON
8#include "webcface-config.h"
9#else
10#include "webcface/common/webcface-config.h"
11#endif
12
14namespace message {
15struct LogLine;
16}
17
18namespace level {
20 trace = 0,
21 debug = 1,
22 info = 2,
23 warn = 3,
25 error = 4,
27};
28}
29
31 int level_ = 0;
32 std::chrono::system_clock::time_point time_;
34
35 LogLineData() = default;
36 LogLineData(int level, std::chrono::system_clock::time_point time,
37 const SharedString &message);
38
40 message::LogLine toMessage() const;
41};
42
43class LogLine : private LogLineData {
44 public:
45 LogLine(const LogLineData &ll) : LogLineData(ll) {}
46 int level() const { return level_; }
47 std::chrono::system_clock::time_point time() const { return time_; }
48 const std::string &message() const { return message_.decode(); };
49};
50class LogLineW : private LogLineData {
51 public:
52 LogLineW(const LogLineData &ll) : LogLineData(ll) {}
53 int level() const { return level_; }
54 std::chrono::system_clock::time_point time() const { return time_; }
55 const std::wstring &message() const { return message_.decodeW(); };
56};
57
65class WEBCFACE_DLL Log : protected Field {
66 public:
67 Log() = default;
68 Log(const Field &base);
72 Log(const Field &base, const SharedString &field)
73 : Log(Field{base, field}) {}
74
75 using Field::member;
76
86 static void WEBCFACE_CALL keepLines(int n);
87
94 const Log &
95 onChange(std::function<void WEBCFACE_CALL_FP(Log)> callback) const;
102 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
103 std::nullptr_t> = nullptr>
104 const Log &onChange(F callback) const {
105 return onChange(
106 [callback = std::move(callback)](const auto &) { callback(); });
107 }
115 template <typename T>
116 [[deprecated]] void appendListener(T &&callback) const {
117 onChange(std::forward<T>(callback));
118 }
119
125 const Log &request() const;
130 std::optional<std::vector<LogLine>> tryGet() const;
135 std::optional<std::vector<LogLineW>> tryGetW() const;
140 std::vector<LogLine> get() const {
141 return tryGet().value_or(std::vector<LogLine>{});
142 }
147 std::vector<LogLineW> getW() const {
148 return tryGetW().value_or(std::vector<LogLineW>{});
149 }
150
159 bool exists() const;
166 const Log &clear() const;
167
168 private:
169 const Log &append(LogLineData &&ll) const;
170
171 public:
179 const Log &append(int level, std::string_view message) const {
180 return append({level, std::chrono::system_clock::now(),
181 SharedString::encode(message)});
182 }
190 const Log &append(int level, std::chrono::system_clock::time_point time,
191 std::string_view message) const {
192 return append({level, time, SharedString::encode(message)});
193 }
201 const Log &append(int level, std::wstring_view message) const {
202 return append({level, std::chrono::system_clock::now(),
203 SharedString::encode(message)});
204 }
212 const Log &append(int level, std::chrono::system_clock::time_point time,
213 std::wstring_view message) const {
214 return append({level, time, SharedString::encode(message)});
215 }
216
221 template <typename T, typename std::enable_if_t<std::is_same_v<T, Log>,
222 std::nullptr_t> = nullptr>
223 bool operator==(const T &other) const {
224 return static_cast<Field>(*this) == static_cast<Field>(other);
225 }
226 template <typename T, typename std::enable_if_t<std::is_same_v<T, Log>,
227 std::nullptr_t> = nullptr>
228 bool operator!=(const T &other) const {
229 return static_cast<Field>(*this) != static_cast<Field>(other);
230 }
231};
Definition log.h:50
int level() const
Definition log.h:53
const std::wstring & message() const
Definition log.h:55
LogLineW(const LogLineData &ll)
Definition log.h:52
std::chrono::system_clock::time_point time() const
Definition log.h:54
Definition log.h:43
int level() const
Definition log.h:46
std::chrono::system_clock::time_point time() const
Definition log.h:47
LogLine(const LogLineData &ll)
Definition log.h:45
const std::string & message() const
Definition log.h:48
ログの送受信データを表すクラス
Definition log.h:65
Log(const Field &base, const SharedString &field)
Definition log.h:72
bool operator==(const T &other) const
Logの参照先を比較
Definition log.h:223
const Log & append(int level, std::chrono::system_clock::time_point time, std::string_view message) const
ログを1行追加
Definition log.h:190
bool operator!=(const T &other) const
Definition log.h:228
Log()=default
void appendListener(T &&callback) const
Definition log.h:116
const Log & append(int level, std::chrono::system_clock::time_point time, std::wstring_view message) const
ログを1行追加 (wstring)
Definition log.h:212
const Log & append(int level, std::string_view message) const
ログを1行追加
Definition log.h:179
std::vector< LogLine > get() const
ログを取得する
Definition log.h:140
std::vector< LogLineW > getW() const
ログを取得する (wstring)
Definition log.h:147
const Log & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition log.h:104
const Log & append(int level, std::wstring_view message) const
ログを1行追加 (wstring)
Definition log.h:201
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
LogLevelEnum
Definition log.h:19
@ warn
Definition log.h:23
@ trace
Definition log.h:20
@ debug
Definition log.h:21
@ critical
Definition log.h:26
@ warning
Definition log.h:24
@ info
Definition log.h:22
@ error
Definition log.h:25
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:70
Definition log.h:30
std::chrono::system_clock::time_point time_
Definition log.h:32
SharedString message_
Definition log.h:33
Definition log.h:13
#define WEBCFACE_DLL
Definition webcface-config.h:60
#define WEBCFACE_CALL
Definition webcface-config.h:97
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103
#define WEBCFACE_CALL_FP
Definition webcface-config.h:98