Skip to content

log #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2025
Merged

log #1382

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
with:
path: fcitx5
cmake-option: >-
-DENABLE_DBUS=Off -DENABLE_X11=Off -DENABLE_WAYLAND=Off -DENABLE_ENCHANT=Off -DENABLE_SERVER=Off -DENABLE_XDGAUTOSTART=Off -DENABLE_LIBUUID=Off -DENABLE_KEYBOARD=Off
-DENABLE_DBUS=Off -DENABLE_X11=Off -DENABLE_WAYLAND=Off -DENABLE_ENCHANT=Off -DENABLE_SERVER=Off -DENABLE_XDGAUTOSTART=Off -DENABLE_LIBUUID=Off -DENABLE_KEYBOARD=Off -DCMAKE_CXX_FLAGS=-fexperimental-library
shell: pwsh

- name: Test
Expand Down
9 changes: 5 additions & 4 deletions src/lib/fcitx-utils/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include <charconv>
#include <chrono>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
#include <mutex>
#include <ostream>
#include <string>
#include <string_view>
#include <system_error>
#include <type_traits>
#include <unordered_set>
Expand Down Expand Up @@ -51,7 +51,7 @@ class LogRegistry {

void registerCategory(LogCategory &category) {
std::lock_guard<std::mutex> lock(mutex_);
if (!categories_.count(&category)) {
if (!categories_.contains(&category)) {
categories_.insert(&category);
applyRule(&category);
}
Expand Down Expand Up @@ -218,7 +218,7 @@ LogMessageBuilder::LogMessageBuilder(std::ostream &out, LogLevel l,
auto now = std::chrono::time_point_cast<std::chrono::microseconds>(
std::chrono::system_clock::now());
#if __cpp_lib_chrono >= 201907L
auto current_zone = std::chrono::current_zone();
const auto *current_zone = std::chrono::current_zone();
std::chrono::zoned_time zoned_time{current_zone, now};

auto timeString = std::format("{:%F %T}", zoned_time);
Expand All @@ -233,5 +233,6 @@ LogMessageBuilder::LogMessageBuilder(std::ostream &out, LogLevel l,
out_ << filename << ":" << lineNumber << "] ";
}

LogMessageBuilder::~LogMessageBuilder() { out_ << std::endl; }
LogMessageBuilder::~LogMessageBuilder() { out_ << '\n'; }

} // namespace fcitx
75 changes: 43 additions & 32 deletions src/lib/fcitx-utils/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
#include <fcitx-utils/metastring.h>
#include <fcitx-utils/misc.h>
#include <fcitx-utils/tuplehelpers.h>
#include <source_location> // IWYU pragma: keep
#include <span>
#include <syncstream>

namespace fcitx {

Expand Down Expand Up @@ -110,12 +112,12 @@ class FCITXUTILS_EXPORT LogMessageBuilder {

LogMessageBuilder &self() { return *this; }

inline LogMessageBuilder &operator<<(const std::string &s) {
LogMessageBuilder &operator<<(const std::string &s) {
*this << s.c_str();
return *this;
}

inline LogMessageBuilder &operator<<(const Key &key) {
LogMessageBuilder &operator<<(const Key &key) {
out_ << "Key(" << key.toString()
<< " states=" << key.states().toInteger() << ")";
return *this;
Expand Down Expand Up @@ -143,7 +145,7 @@ class FCITXUTILS_EXPORT LogMessageBuilder {
FCITX_SIMPLE_LOG(T)

template <typename T>
inline LogMessageBuilder &operator<<(const std::optional<T> &opt) {
LogMessageBuilder &operator<<(const std::optional<T> &opt) {
*this << "optional(has_value=" << opt.has_value() << " ";
if (opt.has_value()) {
*this << *opt;
Expand All @@ -153,43 +155,43 @@ class FCITXUTILS_EXPORT LogMessageBuilder {
}

template <typename T>
inline LogMessageBuilder &operator<<(const std::unique_ptr<T> &ptr) {
LogMessageBuilder &operator<<(const std::unique_ptr<T> &ptr) {
*this << "unique_ptr(" << ptr.get() << ")";
return *this;
}

template <typename T>
inline LogMessageBuilder &operator<<(const std::vector<T> &vec) {
LogMessageBuilder &operator<<(const std::vector<T> &vec) {
*this << "[";
printRange(vec.begin(), vec.end());
*this << "]";
return *this;
}

template <typename T>
inline LogMessageBuilder &operator<<(const std::span<T> &vec) {
LogMessageBuilder &operator<<(const std::span<T> &vec) {
*this << "span[";
printRange(vec.begin(), vec.end());
*this << "]";
return *this;
}

template <typename T>
inline LogMessageBuilder &operator<<(const std::list<T> &lst) {
LogMessageBuilder &operator<<(const std::list<T> &lst) {
*this << "list[";
printRange(lst.begin(), lst.end());
*this << "]";
return *this;
}

template <typename K, typename V>
inline LogMessageBuilder &operator<<(const std::pair<K, V> &pair) {
LogMessageBuilder &operator<<(const std::pair<K, V> &pair) {
*this << "(" << pair.first << ", " << pair.second << ")";
return *this;
}

template <typename... Args>
inline LogMessageBuilder &operator<<(const std::tuple<Args...> &tuple) {
LogMessageBuilder &operator<<(const std::tuple<Args...> &tuple) {
typename MakeSequence<sizeof...(Args)>::type a;
*this << "(";
printWithIndices(a, tuple);
Expand All @@ -198,65 +200,63 @@ class FCITXUTILS_EXPORT LogMessageBuilder {
}

template <typename K, typename V>
inline LogMessageBuilder &operator<<(const std::unordered_map<K, V> &vec) {
LogMessageBuilder &operator<<(const std::unordered_map<K, V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
return *this;
}

template <typename V>
inline LogMessageBuilder &operator<<(const std::unordered_set<V> &vec) {
LogMessageBuilder &operator<<(const std::unordered_set<V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
return *this;
}

template <typename K, typename V>
inline LogMessageBuilder &operator<<(const std::map<K, V> &vec) {
LogMessageBuilder &operator<<(const std::map<K, V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
return *this;
}

template <typename V>
inline LogMessageBuilder &operator<<(const std::set<V> &vec) {
LogMessageBuilder &operator<<(const std::set<V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
return *this;
}

template <typename K, typename V>
inline LogMessageBuilder &operator<<(const std::multimap<K, V> &vec) {
LogMessageBuilder &operator<<(const std::multimap<K, V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
return *this;
}

template <typename V>
inline LogMessageBuilder &operator<<(const std::multiset<V> &vec) {
LogMessageBuilder &operator<<(const std::multiset<V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
return *this;
}

template <typename K, typename V>
inline LogMessageBuilder &
operator<<(const std::unordered_multimap<K, V> &vec) {
LogMessageBuilder &operator<<(const std::unordered_multimap<K, V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
return *this;
}

template <typename V>
inline LogMessageBuilder &
operator<<(const std::unordered_multiset<V> &vec) {
LogMessageBuilder &operator<<(const std::unordered_multiset<V> &vec) {
*this << "{";
printRange(vec.begin(), vec.end());
*this << "}";
Expand Down Expand Up @@ -288,33 +288,44 @@ class FCITXUTILS_EXPORT LogMessageBuilder {

std::ostream &out_;
};
} // namespace fcitx

#ifdef FCITX_USE_NO_METASTRING_FILENAME
#define FCITX_LOG_FILENAME_WRAP ::fcitx::fs::baseName(__FILE__).data()
#else
#define FCITX_LOG_FILENAME_WRAP \
fcitx::MetaStringBasenameType<fcitxMakeMetaString(__FILE__)>::data()
#endif
template <typename MetaStringFileName, int N>
class LogMessageBuilderWrapper {
public:
LogMessageBuilderWrapper(LogLevel l)
: out_(Log::logStream()),
builder_(out_, l, MetaStringFileName::data(), N) {}

LogMessageBuilder &self() { return builder_; }

private:
std::osyncstream out_;
LogMessageBuilder builder_;
};

} // namespace fcitx

// Use meta string for file name to avoid having full path in binary.
#define FCITX_LOGC_IF(CATEGORY, LEVEL, CONDITION) \
for (bool fcitxLogEnabled = \
(CONDITION) && CATEGORY().fatalWrapper(::fcitx::LogLevel::LEVEL); \
fcitxLogEnabled; \
fcitxLogEnabled = CATEGORY().fatalWrapper2(::fcitx::LogLevel::LEVEL)) \
::fcitx::LogMessageBuilder(::fcitx::Log::logStream(), \
::fcitx::LogLevel::LEVEL, \
FCITX_LOG_FILENAME_WRAP, __LINE__) \
::fcitx::LogMessageBuilderWrapper< \
fcitx::MetaStringBasenameType<fcitxMakeMetaString( \
std::source_location::current().file_name())>, \
std::source_location::current().line()>(::fcitx::LogLevel::LEVEL) \
.self()

#define FCITX_LOGC(CATEGORY, LEVEL) \
for (bool fcitxLogEnabled = \
CATEGORY().fatalWrapper(::fcitx::LogLevel::LEVEL); \
fcitxLogEnabled; \
fcitxLogEnabled = CATEGORY().fatalWrapper2(::fcitx::LogLevel::LEVEL)) \
::fcitx::LogMessageBuilder(::fcitx::Log::logStream(), \
::fcitx::LogLevel::LEVEL, \
FCITX_LOG_FILENAME_WRAP, __LINE__) \
::fcitx::LogMessageBuilderWrapper< \
fcitx::MetaStringBasenameType<fcitxMakeMetaString( \
std::source_location::current().file_name())>, \
std::source_location::current().line()>(::fcitx::LogLevel::LEVEL) \
.self()

#define FCITX_LOG(LEVEL) FCITX_LOGC(::fcitx::Log::defaultCategory, LEVEL)
Expand Down
9 changes: 9 additions & 0 deletions src/lib/fcitx-utils/metastring.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/// \brief Static string based on template argument.

#include <cstddef>
#include <string_view>

namespace fcitx {

Expand Down Expand Up @@ -40,6 +41,14 @@ constexpr char __getChar(char const (&str)[M]) noexcept {
return '\0';
}

template <int N>
constexpr char __getChar(std::string_view str) noexcept {
if (N < str.size()) {
return str[N];
}
return '\0';
}

template <typename... T>
struct MetaStringCombine;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/fcitx/addoninstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class FCITXCORE_EXPORT AddonInstance {
#define FCITX_ADDON_FACTORY_V2(AddonName, ClassName) \
extern "C" { \
FCITX_ADDON_EXPORT ::fcitx::AddonFactory * \
fcitx_addon_factory_instance_##AddonName() { \
fcitx_addon_factory_instance_##AddonName() { \
static ClassName factory; \
return &factory; \
} \
Expand Down
Loading