diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-07-10 19:19:18 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-07-10 19:19:18 +0200 |
commit | ae197a62b77cd93799571b16756c372081704929 (patch) | |
tree | f190d3e457845aec79899d71fc836f2f4561032b /src | |
parent | b4f7948dfbe991879dca39dc9b3d53056b8a9e81 (diff) |
Core/Logging: Remove temporary string creation when logging opcode names
Diffstat (limited to 'src')
-rw-r--r-- | src/server/game/Server/Protocol/Opcodes.cpp | 35 | ||||
-rw-r--r-- | src/server/game/Server/Protocol/Opcodes.h | 41 |
2 files changed, 56 insertions, 20 deletions
diff --git a/src/server/game/Server/Protocol/Opcodes.cpp b/src/server/game/Server/Protocol/Opcodes.cpp index aee210a2a64..ec6d0792dbc 100644 --- a/src/server/game/Server/Protocol/Opcodes.cpp +++ b/src/server/game/Server/Protocol/Opcodes.cpp @@ -17,8 +17,10 @@ #include "Opcodes.h" #include "Log.h" +#include "Util.h" #include "WorldSession.h" #include "Packets/AllPackets.h" +#include <charconv> namespace { @@ -2284,8 +2286,8 @@ void OpcodeTable::InitializeServerOpcodes() #undef DEFINE_SERVER_OPCODE_HANDLER } -template<typename OpcodeDefinition, std::size_t N, typename T> -static inline std::string GetOpcodeNameForLoggingImpl(std::array<OpcodeDefinition, N> const& definitions, T id) +template<typename OpcodeDefinition, std::size_t N, typename T, typename FormatContext> +static inline typename FormatContext::iterator GetOpcodeNameForLoggingImpl(std::array<OpcodeDefinition, N> const& definitions, T id, FormatContext& ctx) { uint32 opcode = uint32(id); char const* name = nullptr; @@ -2301,15 +2303,34 @@ static inline std::string GetOpcodeNameForLoggingImpl(std::array<OpcodeDefinitio else name = "INVALID OPCODE"; - return Trinity::StringFormat("[{0} 0x{1:06X} ({1})]", name, opcode); + std::array<char, 10> value; + + std::ranges::copy_n("[", 1, ctx.out()); + std::ranges::copy(name, CStringSentinel, ctx.out()); + std::ranges::copy_n(" 0x", 3, ctx.out()); + char const* hexEnd = std::to_chars(value.data(), value.data() + value.size(), opcode, 16).ptr; + if (std::ptrdiff_t written = std::ranges::distance(value.data(), hexEnd); written < 6) + std::ranges::fill_n(ctx.out(), 6 - written, '0'); + + std::ranges::transform(value.data(), hexEnd, ctx.out(), charToUpper); + std::ranges::copy_n(" (", 2, ctx.out()); + std::ranges::copy(value.data(), std::to_chars(value.data(), value.data() + value.size(), opcode, 10).ptr, ctx.out()); + std::ranges::copy_n(")]", 2, ctx.out()); + + return ctx.out(); } -std::string GetOpcodeNameForLogging(OpcodeClient opcode) +template <typename FormatContext> +typename FormatContext::iterator fmt::formatter<FormattedOpcodeName<OpcodeClient>>::format(FormattedOpcodeName<OpcodeClient> const& opcode, FormatContext& ctx) const { - return GetOpcodeNameForLoggingImpl(opcodeTable._internalTableClient, opcode); + return ::GetOpcodeNameForLoggingImpl(opcodeTable._internalTableClient, opcode.Opcode, ctx); } -std::string GetOpcodeNameForLogging(OpcodeServer opcode) +template <typename FormatContext> +typename FormatContext::iterator fmt::formatter<FormattedOpcodeName<OpcodeServer>>::format(FormattedOpcodeName<OpcodeServer> const& opcode, FormatContext& ctx) const { - return GetOpcodeNameForLoggingImpl(opcodeTable._internalTableServer, opcode); + return ::GetOpcodeNameForLoggingImpl(opcodeTable._internalTableServer, opcode.Opcode, ctx); } + +template TC_GAME_API fmt::appender fmt::formatter<FormattedOpcodeName<OpcodeClient>>::format<fmt::format_context>(FormattedOpcodeName<OpcodeClient> const&, format_context&) const; +template TC_GAME_API fmt::appender fmt::formatter<FormattedOpcodeName<OpcodeServer>>::format<fmt::format_context>(FormattedOpcodeName<OpcodeServer> const&, format_context&) const; diff --git a/src/server/game/Server/Protocol/Opcodes.h b/src/server/game/Server/Protocol/Opcodes.h index abf04c9a6f4..60e3efdb92c 100644 --- a/src/server/game/Server/Protocol/Opcodes.h +++ b/src/server/game/Server/Protocol/Opcodes.h @@ -15,17 +15,13 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -/// \addtogroup u2w -/// @{ -/// \file - -#ifndef _OPCODES_H -#define _OPCODES_H +#ifndef TRINITYCORE_OPCODES_H +#define TRINITYCORE_OPCODES_H #include "Define.h" +#include "StringFormatFwd.h" #include <array> #include <memory> -#include <string> enum ConnectionType : int8 { @@ -36,7 +32,7 @@ enum ConnectionType : int8 CONNECTION_TYPE_DEFAULT = -1 }; -constexpr uint16 UNKNOWN_OPCODE = 0xBADD; // special marker value for uninitialized WorldPackets +constexpr uint32 UNKNOWN_OPCODE = 0xBADD; // special marker value for uninitialized WorldPackets enum OpcodeClient : uint32 { @@ -2287,9 +2283,29 @@ struct ServerOpcodeHandler ConnectionType ConnectionIndex; }; +template <typename OpcodeEnum> +struct FormattedOpcodeName +{ + OpcodeEnum Opcode; +}; + +template <> +struct fmt::formatter<FormattedOpcodeName<OpcodeClient>, char, void> : Trinity::NoArgFormatterBase +{ + template <typename FormatContext> + typename FormatContext::iterator format(FormattedOpcodeName<OpcodeClient> const& opcode, FormatContext& ctx) const; +}; + +template <> +struct fmt::formatter<FormattedOpcodeName<OpcodeServer>, char, void> : Trinity::NoArgFormatterBase +{ + template <typename FormatContext> + typename FormatContext::iterator format(FormattedOpcodeName<OpcodeServer> const& opcode, FormatContext& ctx) const; +}; + /// Lookup opcode name for human understandable logging -std::string GetOpcodeNameForLogging(OpcodeClient opcode); -std::string GetOpcodeNameForLogging(OpcodeServer opcode); +inline constexpr FormattedOpcodeName<OpcodeClient> GetOpcodeNameForLogging(OpcodeClient opcode) { return { .Opcode = opcode }; } +inline constexpr FormattedOpcodeName<OpcodeServer> GetOpcodeNameForLogging(OpcodeServer opcode) { return { .Opcode = opcode }; } class OpcodeTable { @@ -2334,11 +2350,10 @@ private: std::array<std::unique_ptr<ClientOpcodeHandler>, NUM_CMSG_OPCODES> _internalTableClient; std::array<std::unique_ptr<ServerOpcodeHandler>, NUM_SMSG_OPCODES> _internalTableServer; - friend std::string GetOpcodeNameForLogging(OpcodeClient opcode); - friend std::string GetOpcodeNameForLogging(OpcodeServer opcode); + friend fmt::formatter<FormattedOpcodeName<OpcodeClient>, char, void>; + friend fmt::formatter<FormattedOpcodeName<OpcodeServer>, char, void>; }; extern OpcodeTable opcodeTable; #endif -/// @} |