Line data Source code
1 : /**
2 : Copyright (c) 2016-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef STAPPLER_CORE_UTILS_SPLOG_H_
25 : #define STAPPLER_CORE_UTILS_SPLOG_H_
26 :
27 : #include "SPString.h"
28 : #include "SPStringView.h"
29 : #include "SPRef.h"
30 :
31 : namespace STAPPLER_VERSIONIZED stappler::log {
32 :
33 : enum LogType {
34 : Verbose,
35 : Debug,
36 : Info,
37 : Warn,
38 : Error,
39 : Fatal,
40 : Default = Debug,
41 : };
42 :
43 : struct CustomLog {
44 : union VA {
45 : StringView text;
46 : struct {
47 : const char *format = nullptr;
48 : va_list args;
49 : } format;
50 :
51 3864 : VA() { }
52 : };
53 :
54 : enum Type {
55 : Text,
56 : Format
57 : };
58 :
59 : using log_fn = bool (*) (LogType, StringView , Type, VA &);
60 :
61 : CustomLog(log_fn fn);
62 : ~CustomLog();
63 :
64 : CustomLog(const CustomLog &) = delete;
65 : CustomLog& operator=(const CustomLog &) = delete;
66 :
67 : CustomLog(CustomLog &&);
68 : CustomLog& operator=(CustomLog &&);
69 :
70 : log_fn fn;
71 : Rc<RefBase<memory::StandartInterface>> manager;
72 : };
73 :
74 : // log is suppressed if bit is set
75 : // only default logger is affected
76 : void setLogFilterMask(std::bitset<6> &&);
77 : std::bitset<6> getlogFilterMask();
78 :
79 : void format(LogType, const StringView &tag, const char *, ...) SPPRINTF(3, 4);
80 : void text(LogType, const StringView &tag, const StringView &);
81 :
82 : template <typename ... Args>
83 1017 : void verbose(const StringView &tag, Args && ... args) {
84 1017 : text(LogType::Verbose, tag, StringView(mem_std::toString(std::forward<Args>(args)...)));
85 1017 : }
86 :
87 : template <typename ... Args>
88 1257 : void debug(const StringView &tag, Args && ... args) {
89 1257 : text(LogType::Debug, tag, StringView(mem_std::toString(std::forward<Args>(args)...)));
90 1257 : }
91 :
92 : template <typename ... Args>
93 21 : void info(const StringView &tag, Args && ... args) {
94 21 : text(LogType::Info, tag, StringView(mem_std::toString(std::forward<Args>(args)...)));
95 21 : }
96 :
97 : template <typename ... Args>
98 138 : void warn(const StringView &tag, Args && ... args) {
99 138 : text(LogType::Warn, tag, StringView(mem_std::toString(std::forward<Args>(args)...)));
100 138 : }
101 :
102 : template <typename ... Args>
103 660 : void error(const StringView &tag, Args && ... args) {
104 660 : text(LogType::Error, tag, StringView(mem_std::toString(std::forward<Args>(args)...)));
105 660 : }
106 :
107 : template <typename ... Args>
108 : void fatal(const StringView &tag, Args && ... args) {
109 : text(LogType::Fatal, tag, StringView(mem_std::toString(std::forward<Args>(args)...)));
110 : }
111 :
112 : #if DEBUG
113 : #define SPASSERT(cond, msg) do { \
114 : if (!(cond)) { \
115 : if (strlen(msg)) { STAPPLER_VERSIONIZED_NAMESPACE::log::format(STAPPLER_VERSIONIZED_NAMESPACE::log::LogType::Fatal, "Assert", "%s", msg);} \
116 : assert(cond); \
117 : } \
118 : } while (0)
119 : #else
120 : #define SPASSERT(cond, msg)
121 : #endif
122 :
123 : }
124 :
125 : #endif /* STAPPLER_CORE_UTILS_SPLOG_H_ */
|