7 #ifndef _FCITX_UTILS_LOG_H_ 8 #define _FCITX_UTILS_LOG_H_ 23 #include <type_traits> 24 #include <unordered_map> 25 #include <unordered_set> 28 #include <fcitx-utils/fcitxutils_export.h> 31 #include <fcitx-utils/macros.h> 33 #include <fcitx-utils/misc.h> 34 #include <fcitx-utils/tuplehelpers.h> 35 #include <source_location> 52 #define FCITX_SIMPLE_LOG(TYPE) \ 53 inline LogMessageBuilder &operator<<(TYPE v) { \ 65 bool checkLogLevel(
LogLevel l)
const;
67 void setLogLevel(std::underlying_type_t<LogLevel> l);
69 const std::string &name()
const;
73 static bool fatalWrapper2(
LogLevel l);
77 std::unique_ptr<LogCategoryPrivate> d_ptr;
80 class FCITXUTILS_EXPORT
Log {
83 static void setLogRule(
const std::string &rule);
96 static void setLogStream(std::ostream &stream);
103 static std::ostream &logStream();
121 <<
" states=" << key.states().toInteger() <<
")";
125 FCITX_SIMPLE_LOG(
char)
126 FCITX_SIMPLE_LOG(
bool)
127 FCITX_SIMPLE_LOG(
signed short)
128 FCITX_SIMPLE_LOG(
unsigned short)
129 FCITX_SIMPLE_LOG(
signed int)
130 FCITX_SIMPLE_LOG(
unsigned int)
131 FCITX_SIMPLE_LOG(
signed long)
132 FCITX_SIMPLE_LOG(
unsigned long)
133 FCITX_SIMPLE_LOG(
float)
134 FCITX_SIMPLE_LOG(
double)
135 FCITX_SIMPLE_LOG(
char *)
136 FCITX_SIMPLE_LOG(
const char *)
137 FCITX_SIMPLE_LOG(
const void *)
138 FCITX_SIMPLE_LOG(
long double)
139 FCITX_SIMPLE_LOG(
signed long long)
140 FCITX_SIMPLE_LOG(
unsigned long long)
143 template <
typename T>
146 template <
typename T>
148 *
this <<
"optional(has_value=" << opt.has_value() <<
" ";
149 if (opt.has_value()) {
156 template <
typename T>
158 *
this <<
"unique_ptr(" << ptr.get() <<
")";
162 template <
typename T>
165 printRange(vec.begin(), vec.end());
170 template <
typename T>
173 printRange(vec.begin(), vec.end());
178 template <
typename T>
181 printRange(lst.begin(), lst.end());
186 template <
typename K,
typename V>
188 *
this <<
"(" << pair.first <<
", " << pair.second <<
")";
192 template <
typename... Args>
196 printWithIndices(a, tuple);
201 template <
typename K,
typename V>
204 printRange(vec.begin(), vec.end());
209 template <
typename V>
212 printRange(vec.begin(), vec.end());
217 template <
typename K,
typename V>
220 printRange(vec.begin(), vec.end());
225 template <
typename V>
228 printRange(vec.begin(), vec.end());
233 template <
typename K,
typename V>
236 printRange(vec.begin(), vec.end());
241 template <
typename V>
244 printRange(vec.begin(), vec.end());
249 template <
typename K,
typename V>
252 printRange(vec.begin(), vec.end());
257 template <
typename V>
260 printRange(vec.begin(), vec.end());
266 template <
typename Iterator>
267 void printRange(Iterator begin, Iterator end) {
269 for (
auto &item : MakeIterRange(begin, end)) {
279 template <
typename... Args,
int... S>
281 const std::tuple<Args...> &tuple) {
282 using swallow =
int[];
285 (void(*
this << (S == 0 ?
"" :
", ") << std::get<S>(tuple)), 0)...};
291 template <
typename MetaStringFileName,
int N>
306 #define FCITX_LOGC_IF(CATEGORY, LEVEL, CONDITION) \ 307 for (bool fcitxLogEnabled = \ 308 (CONDITION) && CATEGORY().fatalWrapper(::fcitx::LogLevel::LEVEL); \ 310 fcitxLogEnabled = CATEGORY().fatalWrapper2(::fcitx::LogLevel::LEVEL)) \ 311 ::fcitx::LogMessageBuilderWrapper< \ 312 fcitx::MetaStringBasenameType<fcitxMakeMetaString( \ 313 std::source_location::current().file_name())>, \ 314 std::source_location::current().line()>(::fcitx::LogLevel::LEVEL) \ 317 #define FCITX_LOGC(CATEGORY, LEVEL) \ 318 for (bool fcitxLogEnabled = \ 319 CATEGORY().fatalWrapper(::fcitx::LogLevel::LEVEL); \ 321 fcitxLogEnabled = CATEGORY().fatalWrapper2(::fcitx::LogLevel::LEVEL)) \ 322 ::fcitx::LogMessageBuilderWrapper< \ 323 fcitx::MetaStringBasenameType<fcitxMakeMetaString( \ 324 std::source_location::current().file_name())>, \ 325 std::source_location::current().line()>(::fcitx::LogLevel::LEVEL) \ 328 #define FCITX_LOG(LEVEL) FCITX_LOGC(::fcitx::Log::defaultCategory, LEVEL) 330 #define FCITX_DEBUG() FCITX_LOG(Debug) 331 #define FCITX_WARN() FCITX_LOG(Warn) 332 #define FCITX_INFO() FCITX_LOG(Info) 333 #define FCITX_ERROR() FCITX_LOG(Error) 334 #define FCITX_FATAL() FCITX_LOG(Fatal) 336 #define FCITX_LOG_IF(LEVEL, CONDITION) \ 337 FCITX_LOGC_IF(::fcitx::Log::defaultCategory, LEVEL, CONDITION) 339 #define FCITX_ASSERT(...) \ 340 FCITX_LOG_IF(Fatal, !(__VA_ARGS__)) << #__VA_ARGS__ << " failed. " 342 #define FCITX_DEFINE_LOG_CATEGORY(name, ...) \ 343 const ::fcitx::LogCategory &name() { \ 344 static const ::fcitx::LogCategory category(__VA_ARGS__); \ 348 #define FCITX_DECLARE_LOG_CATEGORY(name) const ::fcitx::LogCategory &name() 350 #endif // _FCITX_UTILS_LOG_H_
Simple file system related API for checking file status.
LogLevel
LogLevel from high to low.
static std::ostream & logStream()
Return the default log stream to be used.
Class to represent a key.
std::string toString(KeyStringFormat format=KeyStringFormat::Portable) const
Convert key to a string.
Fatal will always abort regardless of log or not.