Skip to content

Commit

Permalink
Switch from fmtlib to std::format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed May 21, 2024
1 parent 6012dbb commit 6b210fa
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mo2_configure_uibase(uibase
WARNINGS ON
EXTERNAL_WARNINGS ON
TRANSLATIONS OFF
PUBLIC_DEPENDS fmt Qt::Widgets Qt::Network Qt::QuickWidgets
PUBLIC_DEPENDS Qt::Widgets Qt::Network Qt::QuickWidgets
PRIVATE_DEPENDS boost boost::thread Qt::Qml Qt::Quick spdlog)
target_compile_definitions(uibase PRIVATE -DUIBASE_EXPORT)
mo2_install_target(uibase)
Expand Down
177 changes: 177 additions & 0 deletions src/formatters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#pragma once

#include <QColor>
#include <QFlag>
#include <QFlags>
#include <QRect>
#include <QSize>
#include <QString>
#include <QVariant>

namespace MOBase::details
{
template <class CharT>
inline std::basic_string<CharT> toStdBasicString(QString const& qstring);

template <>
inline std::basic_string<char> toStdBasicString(QString const& qstring)
{
return qstring.toStdString();
}
template <>
inline std::basic_string<wchar_t> toStdBasicString(QString const& qstring)
{
return qstring.toStdWString();
}
template <>
inline std::basic_string<char16_t> toStdBasicString(QString const& qstring)
{
return qstring.toStdU16String();
}
template <>
inline std::basic_string<char32_t> toStdBasicString(QString const& qstring)
{
return qstring.toStdU32String();
}

inline QString fromStdBasicString(std::string const& value)
{
return QString::fromStdString(value);
}
inline QString fromStdBasicString(std::wstring const& value)
{
return QString::fromStdWString(value);
}
inline QString fromStdBasicString(std::u16string const& value)
{
return QString::fromStdU16String(value);
}
inline QString fromStdBasicString(std::u32string const& value)
{
return QString::fromStdU32String(value);
}
} // namespace MOBase::details

template <class CharT>
struct std::formatter<QString, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QString s, FmtContext& ctx) const
{
return std::formatter<std::basic_string<CharT>, CharT>::format(
MOBase::details::toStdBasicString<CharT>(s), ctx);
}
};

template <class CharT1, class CharT2>
requires(!std::is_same_v<CharT1, CharT2>)
struct std::formatter<std::basic_string<CharT1>, CharT2>
: std::formatter<std::basic_string<CharT2>, CharT2>
{
template <class FmtContext>
FmtContext::iterator format(std::basic_string<CharT1> s, FmtContext& ctx) const
{
return std::formatter<std::basic_string<CharT2>, CharT2>::format(
MOBase::details::toStdBasicString<CharT2>(
MOBase::details::fromStdBasicString(s)),
ctx);
}
};

template <class CharT>
struct std::formatter<QStringView, CharT> : std::formatter<QString, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QStringView s, FmtContext& ctx) const
{
return std::formatter<QString, CharT>::format(s.toString(), ctx);
}
};

template <class CharT>
struct std::formatter<QSize, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QSize s, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QSize({}, {})", s.width(), s.height());
}
};

template <class CharT>
struct std::formatter<QRect, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QRect r, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QRect({},{}-{},{})", r.left(), r.top(), r.right(),
r.bottom());
}
};

template <class CharT>
struct std::formatter<QColor, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QColor c, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QColor({}, {}, {}, {})", c.red(), c.green(),
c.blue(), c.alpha());
}
};

template <class CharT>
struct std::formatter<QByteArray, CharT>
: std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QByteArray v, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QByteArray({} bytes)", v.size());
}
};

template <class CharT>
struct std::formatter<QVariant, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QVariant v, FmtContext& ctx) const
{
return std::format_to(
ctx.out(), "QVariant(type={}, value={})", v.typeName(),
(v.typeId() == QMetaType::Type::QByteArray ? "(binary)" : v.toString()));
}
};

template <class CharT>
struct std::formatter<QFlag, CharT> : std::formatter<int, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QFlag v, FmtContext& ctx) const
{
return std::formatter<int, CharT>::format(static_cast<int>(v), ctx);
}
};

template <class T, class CharT>
struct std::formatter<QFlags<T>, CharT> : std::formatter<int, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QFlags<T> v, FmtContext& ctx) const
{
// TODO: display flags has aa | bb | cc?
return std::formatter<int, CharT>::format(v.toInt(), ctx);
}
};

template <class Enum, class CharT>
requires std::is_enum_v<Enum>
struct std::formatter<Enum, CharT> : std::formatter<std::underlying_type_t<Enum>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(Enum v, FmtContext& ctx) const
{
return std::formatter<std::underlying_type_t<Enum>, CharT>::format(
static_cast<std::underlying_type_t<Enum>>(v), ctx);
}
};
45 changes: 1 addition & 44 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void Logger::setFile(const File& f)
addSink(m_file);
}
} catch (spdlog::spdlog_ex& e) {
error(e.what());
error("{}", e.what());
}
}
}
Expand Down Expand Up @@ -369,49 +369,6 @@ Logger& getDefault()
namespace MOBase::log::details
{

std::string converter<std::wstring>::convert(const std::wstring& s)
{
return QString::fromStdWString(s).toStdString();
}

std::string converter<QString>::convert(const QString& s)
{
return s.toStdString();
}

std::string converter<QStringView>::convert(const QStringView& s)
{
return converter<QString>::convert(s.toString());
}

std::string converter<QSize>::convert(const QSize& s)
{
return fmt::format("QSize({}, {})", s.width(), s.height());
}

std::string converter<QRect>::convert(const QRect& r)
{
return fmt::format("QRect({},{}-{},{})", r.left(), r.top(), r.right(), r.bottom());
}

std::string converter<QColor>::convert(const QColor& c)
{
return fmt::format("QColor({}, {}, {}, {})", c.red(), c.green(), c.blue(), c.alpha());
}

std::string converter<QByteArray>::convert(const QByteArray& v)
{
return fmt::format("QByteArray({} bytes)", v.size());
}

std::string converter<QVariant>::convert(const QVariant& v)
{
return fmt::format("QVariant(type={}, value='{}')", v.typeName(),
(v.typeId() == QMetaType::Type::QByteArray
? "(binary)"
: v.toString().toStdString()));
}

void doLogImpl(spdlog::logger& lg, Levels lv, const std::string& s) noexcept
{
try {
Expand Down
Loading

0 comments on commit 6b210fa

Please sign in to comment.