Skip to content

Commit

Permalink
remove all direct refrences of mh::format
Browse files Browse the repository at this point in the history
  • Loading branch information
surepy committed Jun 5, 2024
1 parent 1dda14b commit 9c06de9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 39 deletions.
8 changes: 4 additions & 4 deletions tf2_bot_detector/DiscordRichPresence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ static bool s_DiscordDebugLogEnabled = true;

template<typename... TArgs>
static auto DiscordDebugLog(const std::string_view& fmtStr, const TArgs&... args) ->
decltype(mh::format(fmtStr, args...), void())
decltype(fmt::format(fmtStr, args...), void())
{
if (!s_DiscordDebugLogEnabled)
return;

DebugLog(DISCORD_LOG_COLOR, "DRP: {}", mh::format(mh::runtime(fmtStr), args...));
DebugLog(DISCORD_LOG_COLOR, "DRP: {}", fmt::format(fmt::runtime(fmtStr), args...));
}

template<typename... TArgs>
static auto DiscordDebugLog(const mh::source_location& location,
static auto DiscordDebugLog(const std::source_location& location,
const std::string_view& fmtStr = {}, const TArgs&... args) ->
decltype(mh::format(fmtStr, args...), void())
decltype(fmt::format(fmtStr, args...), void())
{
if (!s_DiscordDebugLogEnabled)
return;
Expand Down
55 changes: 48 additions & 7 deletions tf2_bot_detector/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ using namespace std::string_literals;
using namespace std::string_view_literals;
using namespace tf2_bot_detector;

// copied from mh/format.hpp.
template<typename TFmtStr, typename TFmtArgs>
inline auto try_vformat(const TFmtStr& fmtStr, const TFmtArgs& args) try
{
return fmt::vformat(fmtStr, args);
}
catch (const fmt::format_error& e)
{
using char_type_t = std::decay_t<decltype(fmtStr[0])>;
if constexpr (std::is_same_v<char_type_t, char>)
{
return fmt::format(FMT_STRING("FORMATTING ERROR: Unable to construct string with fmtstr {}: {}"), std::quoted(fmtStr), e.what());
}
else if constexpr (std::is_same_v<char_type_t, wchar_t>)
{
// Can't print error message from exception because fmt does not handle conversion from char -> wchar_t on its own unfortunately
return fmt::format(FMT_STRING(L"FORMATTING ERROR: Unable to construct string with fmtstr {}"), std::quoted(fmtStr));
}
else
{
// Other character types are a compile error for now
}
}

namespace
{
class LogManager final : public ILogManager
Expand Down Expand Up @@ -328,21 +352,26 @@ void detail::log_h::LogImpl(const LogMessageColor& color, LogSeverity severity,
}

void detail::log_h::LogImpl(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const mh::source_location& location, const std::string_view& str)
const std::source_location& location, const std::string_view& str)
{
LogImpl(color, severity, visibility, mh::format(MH_FMT_STRING("{}: {}"sv), location, str));
// -> 349
LogImpl(color, severity, visibility, fmt::format(FMT_STRING("{}: {}"sv), location, str));
}

// called by LOG_DEFINITION_HELPER() (2nd one, debug)
void detail::log_h::LogImplBase(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const std::string_view& fmtStr, const mh::format_args& args)
const std::string_view& fmtStr, const fmt::format_args& args)
{
LogImpl(color, severity, visibility, mh::try_vformat(fmtStr, args));
// -> L349
LogImpl(color, severity, visibility, try_vformat(fmtStr, args));
}

// called by LOG_DEFINITION_HELPER() (1st one, real logs.)
void detail::log_h::LogImplBase(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const mh::source_location& location, const std::string_view& fmtStr, const mh::format_args& args)
const std::source_location& location, const std::string_view& fmtStr, const fmt::format_args& args)
{
LogImpl(color, severity, visibility, location, mh::try_vformat(fmtStr, args));
// -> L354
LogImpl(color, severity, visibility, location, try_vformat(fmtStr, args));
}

void LogManager::Log(std::string msg, const LogMessageColor& color,
Expand Down Expand Up @@ -493,7 +522,19 @@ LogMessageColor::LogMessageColor(const ImVec4& vec) :

namespace tf2_bot_detector
{
LOG_DEFINITION_HELPER(Log, LogColors::DEFAULT, LogSeverity::Info, LogVisibility::Default);
// LOG_DEFINITION_HELPER(Log, LogColors::DEFAULT, LogSeverity::Info, LogVisibility::Default);
void Log(const mh::source_location& location) {
Log(LogColors::DEFAULT, location);
}
void Log(const LogMessageColor& color, const std::string_view& msg, const mh::source_location& location) {
Log(color, location, msg);
}
void Log(const std::string_view& msg, const mh::source_location& location) {
Log(location, msg);
}
void Log(const LogMessageColor& color, const mh::source_location& location) {
Log(color, location, std::string_view{});
}
LOG_DEFINITION_HELPER(DebugLog, LogColors::DEFAULT_DEBUG, LogSeverity::Info, LogVisibility::Debug);
LOG_DEFINITION_HELPER(LogWarning, LogColors::WARN, LogSeverity::Warning, LogVisibility::Default);
LOG_DEFINITION_HELPER(DebugLogWarning, LogColors::WARN_DEBUG, LogSeverity::Warning, LogVisibility::Debug);
Expand Down
73 changes: 47 additions & 26 deletions tf2_bot_detector/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@
#include "Clock.h"

#include <mh/coroutine/generator.hpp>
#include <mh/text/format.hpp>
#include <mh/source_location.hpp>
#include <mh/reflection/enum.hpp>
#include <mh/text/format.hpp>

#include <filesystem>
#include <string>

struct ImVec4;

// copied from mh/format.hpp.
template<typename... TFmtStr>
constexpr inline auto try_format(fmt::format_string<TFmtStr...> fmtStr, TFmtStr&&... args)
-> decltype(fmt::format(fmtStr, std::forward<TFmtStr>(args)...))
{
try
{
return fmt::format(fmtStr, args...);
}
catch (const fmt::format_error& e)
{
// std::quoted can't be evaluated constexpr-ly, so we're just removing std::quoted
return fmt::format(FMT_STRING("FORMATTING ERROR: Unable to construct string with fmtstr \"{}\": {}"), fmtStr, e.what());
}
}

namespace tf2_bot_detector
{
struct LogMessageColor
Expand Down Expand Up @@ -108,36 +125,43 @@ namespace tf2_bot_detector
#define NOINLINE __attribute__((noinline))
#endif



namespace detail::log_h
{
void LogImpl(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility, std::string str);
void LogImpl(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const mh::source_location& location, const std::string_view& str);
NOINLINE void LogImplBase(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const std::string_view& fmtStr, const mh::format_args& args);
const std::string_view& fmtStr, const fmt::format_args& args);
NOINLINE void LogImplBase(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const mh::source_location& location, const std::string_view& fmtStr, const mh::format_args& args);
const std::source_location& location, const std::string_view& fmtStr, const fmt::format_args& args);

// TODO: make fmtStr not always fmt::runtime() <- possible even?
template<typename... TArgs>
NOINLINE inline auto LogImpl(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const std::string_view& fmtStr, const TArgs&... args)
-> decltype(mh::try_format(mh::runtime(fmtStr), mh::make_format_args(args...)), void())
NOINLINE inline auto LogImpl(
const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const std::string_view& fmtStr, const TArgs&... args
)
-> decltype(::try_format(fmt::runtime(fmtStr), fmt::make_format_args(args...)), void())
{
LogImplBase(color, severity, visibility, fmtStr, mh::make_format_args(args...));
LogImplBase(color, severity, visibility, fmtStr, fmt::make_format_args(args...));
}

template<typename... TArgs>
NOINLINE inline auto LogImpl(const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const mh::source_location& location, const std::string_view& fmtStr, const TArgs&... args)
-> decltype(mh::try_format(mh::runtime(fmtStr), mh::make_format_args(args...)), void())
NOINLINE inline auto LogImpl(
const LogMessageColor& color, LogSeverity severity, LogVisibility visibility,
const std::source_location& location, const std::string_view& fmtStr, const TArgs&... args
)
-> decltype(::try_format(fmt::runtime(fmtStr), fmt::make_format_args(args...)), void())
{
LogImplBase(color, severity, visibility, location, fmtStr, mh::make_format_args(args...));
LogImplBase(color, severity, visibility, location, fmtStr, fmt::make_format_args(args...));
}

struct src_location_wrapper
{
template<typename T, typename = std::enable_if_t<std::is_constructible_v<std::string_view, T>>>
constexpr src_location_wrapper(const T& value, MH_SOURCE_LOCATION_AUTO(location)) :
constexpr src_location_wrapper(const T& value, const ::std::source_location& location = ::std::source_location::current()) :
m_Value(value), m_Location(location)
{
}
Expand All @@ -149,7 +173,7 @@ namespace tf2_bot_detector

#undef LOG_DEFINITION_HELPER

#define LOG_DEFINITION_HELPER(name, defaultColor, severity, visibility) \
#define LOG_DEFINITION_HELPER_LOGS(name, defaultColor, severity, visibility) \
template<typename... TArgs> \
inline auto name(const LogMessageColor& color, const mh::source_location& location, const std::string_view& fmtStr, const TArgs&... args) \
{ \
Expand All @@ -175,23 +199,22 @@ namespace tf2_bot_detector
void name(const LogMessageColor& color, MH_SOURCE_LOCATION_AUTO(location)); \
void name(MH_SOURCE_LOCATION_AUTO(location));

LOG_DEFINITION_HELPER(Log, LogColors::DEFAULT, LogSeverity::Info, LogVisibility::Default);
LOG_DEFINITION_HELPER(DebugLog, LogColors::DEFAULT_DEBUG, LogSeverity::Info, LogVisibility::Debug);
LOG_DEFINITION_HELPER(LogWarning, LogColors::WARN, LogSeverity::Warning, LogVisibility::Default);
LOG_DEFINITION_HELPER(DebugLogWarning, LogColors::WARN_DEBUG, LogSeverity::Warning, LogVisibility::Debug);
LOG_DEFINITION_HELPER(LogError, LogColors::ERROR, LogSeverity::Error, LogVisibility::Default);
LOG_DEFINITION_HELPER_LOGS(Log, LogColors::DEFAULT, LogSeverity::Info, LogVisibility::Default);
LOG_DEFINITION_HELPER_LOGS(DebugLog, LogColors::DEFAULT_DEBUG, LogSeverity::Info, LogVisibility::Debug);
LOG_DEFINITION_HELPER_LOGS(LogWarning, LogColors::WARN, LogSeverity::Warning, LogVisibility::Default);
LOG_DEFINITION_HELPER_LOGS(DebugLogWarning, LogColors::WARN_DEBUG, LogSeverity::Warning, LogVisibility::Debug);
LOG_DEFINITION_HELPER_LOGS(LogError, LogColors::ERROR, LogSeverity::Error, LogVisibility::Default);

#undef LOG_DEFINITION_HELPER

void LogException(const mh::source_location& location, const std::exception_ptr& e,
LogSeverity severity, LogVisibility visibility, const std::string_view& msg = {});

#define LOG_DEFINITION_HELPER(name, attr, severity, visibility) \
#define LOG_DEFINITION_HELPER_DEBUG(name, attr, severity, visibility) \
template<typename... TArgs> \
attr void name(const mh::source_location& location, const std::exception_ptr& e, \
const std::string_view& fmtStr = {}, const TArgs&... args) \
{ \
LogException(location, e, severity, visibility, mh::format(mh::runtime(fmtStr), args...)); /* NOTE: we're losing compile-time evaluation on fmtStr, but I can't be arsed to bother. */ \
LogException(location, e, severity, visibility, fmt::format(fmt::runtime(fmtStr), args...)); /* NOTE: we're losing compile-time evaluation on fmtStr, but I can't be arsed to bother. */ \
} \
template<typename... TArgs> \
attr void name(const std::exception_ptr& e, const detail::log_h::src_location_wrapper& fmtStr = {}, const TArgs&... args) \
Expand Down Expand Up @@ -224,11 +247,9 @@ namespace tf2_bot_detector
attr void name(MH_SOURCE_LOCATION_AUTO(location)); \
attr void name(const std::exception& e, MH_SOURCE_LOCATION_AUTO(location));

LOG_DEFINITION_HELPER(DebugLogException, , LogSeverity::Error, LogVisibility::Debug);
LOG_DEFINITION_HELPER(LogException, , LogSeverity::Error, LogVisibility::Default);
LOG_DEFINITION_HELPER(LogFatalException, [[noreturn]], LogSeverity::Fatal, LogVisibility::Default);

#undef LOG_DEFINITION_HELPER
LOG_DEFINITION_HELPER_DEBUG(DebugLogException, , LogSeverity::Error, LogVisibility::Debug);
LOG_DEFINITION_HELPER_DEBUG(LogException, , LogSeverity::Error, LogVisibility::Default);
LOG_DEFINITION_HELPER_DEBUG(LogFatalException, [[noreturn]], LogSeverity::Fatal, LogVisibility::Default);

#undef NOINLINE
#pragma pop_macro("NOINLINE")
Expand Down
2 changes: 1 addition & 1 deletion tf2_bot_detector/ModeratorLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ bool ModeratorLogic::SetPlayerAttribute(const SteamID& player, std::string name,
case AttributePersistence::Transient: return data.m_TransientAttributes;
}

throw std::invalid_argument(mh::format("{}", ::std::source_location::current()));
throw std::invalid_argument(fmt::format("{}", ::std::source_location::current()));
}();

attributeChanged = attribs.SetAttribute(attribute, set);
Expand Down
2 changes: 1 addition & 1 deletion tf2_bot_detector/SetupFlow/AddonManagerPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ namespace
{
const auto originalInstallTarget = addon.m_InstallTarget;
addon.m_InstallTarget = mh::replace_filename_keep_extension(baseInstallTarget,
mh::format(MH_FMT_STRING("{}_{}"), mh::filename_without_extension(baseInstallTarget).string(), i));
fmt::format(FMT_STRING("{}_{}"), mh::filename_without_extension(baseInstallTarget).string(), i));

DebugLogWarning("{} already exists, trying {}...", originalInstallTarget, addon.m_InstallTarget);
}
Expand Down

0 comments on commit 9c06de9

Please sign in to comment.