Replace VAFORMAT/InspIRCd::Format/... with fmt::sprintf.

This commit is contained in:
Sadie Powell 2023-01-23 09:37:15 +00:00
parent 5c4badf8ea
commit 7edc627317
13 changed files with 55 additions and 134 deletions

View File

@ -241,7 +241,6 @@ INCLUDE_PATH =
INCLUDE_FILE_PATTERNS =
PREDEFINED = CoreExport=/**/ \
ATTR_NOT_NULL(...)=/**/ \
ATTR_PRINTF(X,Y)=/**/ \
INSPIRCD_INTRUSIVE_LIST_NAME=intrusive_list
EXPAND_AS_DEFINED =
SKIP_FUNCTION_MACROS = YES

View File

@ -32,18 +32,6 @@
# define ATTR_NOT_NULL(...)
#endif
/**
* @def ATTR_PRINTF(STRINGPOS, FIRSTPOS)
* Enables the compile-time checking of printf format strings. If a function
* is marked with this attribute then the compiler will warn if a malformed
* format string is passed to it.
*/
#if defined __GNUC__
# define ATTR_PRINTF(STRINGPOS, FIRSTPOS) __attribute__((format(printf, STRINGPOS, FIRSTPOS)))
#else
# define ATTR_PRINTF(STRINGPOS, FIRSTPOS)
#endif
/**
* Windows is very different to UNIX so we have to wrap certain features in
* order to build on Windows correctly.

View File

@ -369,13 +369,6 @@ public:
[[noreturn]]
static void QuickExit(int status);
/** Formats the input string with the specified arguments.
* @param formatString The string to format
* @param ... A variable number of format arguments.
* @return The formatted string
*/
static std::string Format(va_list& vaList, const char* formatString) ATTR_PRINTF(2, 0);
/** Determines whether a nickname is valid. */
std::function<bool(const std::string_view&)> IsNick = &DefaultIsNick;

View File

@ -27,17 +27,6 @@
#pragma once
#include <cstring>
/** Sets ret to the formatted string. last is the last parameter before ..., and format is the format in printf-style */
#define VAFORMAT(ret, last, format) \
do { \
va_list _vaList; \
va_start(_vaList, last); \
ret.assign(InspIRCd::Format(_vaList, format)); \
va_end(_vaList); \
} while (false)
/** @def INSP_FORMAT(FORMAT, ...)
* Formats a string with format string checking.
*/
@ -48,6 +37,7 @@
# include <fmt/core.h>
# define INSP_FORMAT(FORMAT, ...) fmt::format(FMT_STRING(FORMAT), __VA_ARGS__)
#endif
#include <fmt/printf.h>
namespace Base64
{

View File

@ -235,17 +235,9 @@ private:
* @param level The level to log at.
* @param type The type of message that is being logged.
* @param message The message to log.
* */
*/
void Write(Level level, const std::string& type, const std::string& message);
/** Writes a message to the server logs.
* @param level The level to log at.
* @param type The type of message that is being logged.
* @param format The message to format and then log.
* @param args The arguments to use when formatting the log message
* */
void Write(Level level, const std::string& type, const char* format, va_list& args) ATTR_NOT_NULL(4) ATTR_PRINTF(4, 0);
public:
Manager();
@ -278,19 +270,18 @@ public:
/** Writes an error message to the server log.
* @param type The type of message that is being logged.
* @param format A format string to format and then log.
* */
inline void Error(const std::string& type, const char* format, ...) ATTR_NOT_NULL(3) ATTR_PRINTF(3, 4)
* @param args One or more arguments to format the string with.
*/
template <typename... Args>
void Error(const std::string& type, const char* format, Args&&... args)
{
va_list args;
va_start(args, format);
Write(Level::ERROR, type, format, args);
va_end(args);
Write(Level::ERROR, type, fmt::sprintf(format, std::forward<Args>(args)...));
}
/** Writes a warning message to the server log.
* @param type The type of message that is being logged.
* @param message The message to log.
* */
*/
inline void Warning(const std::string& type, const std::string& message)
{
Write(Level::WARNING, type, message);
@ -299,19 +290,18 @@ public:
/** Writes a warning message to the server log.
* @param type The type of message that is being logged.
* @param format A format string to format and then log.
* */
inline void Warning(const std::string& type, const char* format, ...) ATTR_NOT_NULL(3) ATTR_PRINTF(3, 4)
* @param args One or more arguments to format the string with.
*/
template <typename... Args>
void Warning(const std::string& type, const char* format, Args&&... args)
{
va_list args;
va_start(args, format);
Write(Level::WARNING, type, format, args);
va_end(args);
Write(Level::WARNING, type, fmt::sprintf(format, std::forward<Args>(args)...));
}
/** Writes a normal message to the server log.
* @param type The type of message that is being logged.
* @param message The message to log.
* */
*/
inline void Normal(const std::string& type, const std::string& message)
{
Write(Level::NORMAL, type, message);
@ -320,19 +310,18 @@ public:
/** Writes a normal message to the server log.
* @param type The type of message that is being logged.
* @param format A format string to format and then log.
* */
inline void Normal(const std::string& type, const char* format, ...) ATTR_NOT_NULL(3) ATTR_PRINTF(3, 4)
* @param args One or more arguments to format the string with.
*/
template <typename... Args>
void Normal(const std::string& type, const char* format, Args&&... args)
{
va_list args;
va_start(args, format);
Write(Level::NORMAL, type, format, args);
va_end(args);
Write(Level::NORMAL, type, fmt::sprintf(format, std::forward<Args>(args)...));
}
/** Writes a debug message to the server log.
* @param type The type of message that is being logged.
* @param message The message to log.
* */
*/
inline void Debug(const std::string& type, const std::string& message)
{
Write(Level::DEBUG, type, message);
@ -340,19 +329,18 @@ public:
/** Writes a debug message to the server log.
* @param type The type of message that is being logged.
* @param format A format string to format and then log.
* */
inline void Debug(const std::string& type, const char* format, ...) ATTR_NOT_NULL(3) ATTR_PRINTF(3, 4)
* @param args One or more arguments to format the string with.
*/
template <typename... Args>
void Debug(const std::string& type, const char* format, Args&&... args)
{
va_list args;
va_start(args, format);
Write(Level::DEBUG, type, format, args);
va_end(args);
Write(Level::DEBUG, type, fmt::sprintf(format, std::forward<Args>(args)...));
}
/** Writes a raw I/O message to the server log.
* @param type The type of message that is being logged.
* @param message The message to log.
* */
*/
inline void RawIO(const std::string& type, const std::string& message)
{
Write(Level::RAWIO, type, message);
@ -361,12 +349,11 @@ public:
/** Writes a raw I/O message to the server log.
* @param type The type of message that is being logged.
* @param format A format string to format and then log.
* */
inline void RawIO(const std::string& type, const char* format, ...) ATTR_NOT_NULL(3) ATTR_PRINTF(3, 4)
* @param args One or more arguments to format the string with.
*/
template <typename... Args>
void RawIO(const std::string& type, const char* format, Args&&... args)
{
va_list args;
va_start(args, format);
Write(Level::RAWIO, type, format, args);
va_end(args);
Write(Level::RAWIO, type, fmt::sprintf(format, std::forward<Args>(args)...));
}
};

View File

@ -99,9 +99,13 @@ public:
/** Write to all users with a given snomask (local server only)
* @param letter The snomask letter to write to
* @param text A format string containing text to send
* @param ... Format arguments
* @param args Format arguments
*/
void WriteToSnoMask(char letter, const char* text, ...) ATTR_PRINTF(3, 4);
template <typename... Args>
void WriteToSnoMask(char letter, const char* text, Args&&... args)
{
WriteToSnoMask(letter, fmt::sprintf(text, std::forward<Args>(args)...));
}
/** Write to all users with a given snomask (sent globally)
* @param letter The snomask letter to write to
@ -112,9 +116,13 @@ public:
/** Write to all users with a given snomask (sent globally)
* @param letter The snomask letter to write to
* @param text A format string containing text to send
* @param ... Format arguments
* @param args Format arguments
*/
void WriteGlobalSno(char letter, const char* text, ...) ATTR_PRINTF(3, 4);
template <typename... Args>
void WriteGlobalSno(char letter, const char* text, Args&&... args)
{
WriteGlobalSno(letter, fmt::sprintf(text, std::forward<Args>(args)...));
}
/** Called once per 5 seconds from the mainloop, this flushes any cached
* snotices. The way the caching works is as follows:

View File

@ -26,6 +26,11 @@
*/
#include "inspircd.h"
#include "modules/cap.h"
#include "modules/stats.h"
#include "xline.h"
#ifdef _WIN32
# include <psapi.h>
#else
@ -33,11 +38,6 @@
# include <sys/resource.h>
#endif
#include "inspircd.h"
#include "modules/cap.h"
#include "modules/stats.h"
#include "xline.h"
class StatsTagsProvider
: public ClientProtocol::MessageTagProvider
{

View File

@ -46,7 +46,7 @@ bool InsaneBan::MatchesEveryone(const std::string& mask, MatcherBase& test, User
ServerInstance->SNO.WriteToSnoMask('x', "\002WARNING\002: %s tried to set add %s %c-line on %s which covers %.2f%% of the network which is more than the maximum of %.2f%%!",
user->nick.c_str(), article, bantype, mask.c_str(), percent, itrigger);
user->WriteNotice(INSP_FORMAT("*** Unable to add {} {}-line on {} which covers {:.2}% of the network which is more than the maximum of {:.2}%!",
article, bantype, mask.c_str(), percent, itrigger));
article, bantype, mask, percent, itrigger));
return true;
}
return false;

View File

@ -387,29 +387,6 @@ std::string Duration::ToString(unsigned long duration)
return ret;
}
std::string InspIRCd::Format(va_list& vaList, const char* formatString)
{
static std::vector<char> formatBuffer(1024);
while (true)
{
va_list dst;
va_copy(dst, vaList);
int vsnret = vsnprintf(formatBuffer.data(), formatBuffer.size(), formatString, dst);
va_end(dst);
if (vsnret > 0 && static_cast<unsigned>(vsnret) < formatBuffer.size())
{
break;
}
formatBuffer.resize(formatBuffer.size() * 2);
}
return std::string(formatBuffer.data());
}
std::string InspIRCd::TimeString(time_t curtime, const char* format, bool utc)
{
#ifdef _WIN32

View File

@ -290,8 +290,3 @@ void Log::Manager::Write(Level level, const std::string& type, const std::string
cache.emplace_back(time, level, type, message);
logging = false;
}
void Log::Manager::Write(Level level, const std::string& type, const char* format, va_list& args)
{
Write(level, type, InspIRCd::Format(args, format));
}

View File

@ -280,7 +280,7 @@ public:
{
case Codepage::AllowCharacterResult::OKAY:
ServerInstance->Logs.Debug(MODNAME, "Marked %lu (%.4s) as allowed (front: %s)",
pos, reinterpret_cast<unsigned char*>(&pos), front ? "yes" : "no");
pos, fmt::ptr(&pos), front ? "yes" : "no");
break;
case Codepage::AllowCharacterResult::NOT_VALID:
@ -308,7 +308,7 @@ public:
throw ModuleException(this, "Malformed <cpcase> tag at " + tag->source.str());
ServerInstance->Logs.Debug(MODNAME, "Marked %lu (%.4s) as the lower case version of %lu (%.4s)",
lower, reinterpret_cast<unsigned char*>(&lower), upper, reinterpret_cast<unsigned char*>(&upper));
lower, fmt::ptr(&lower), upper, fmt::ptr(&upper));
}
charset = codepagetag->getString("charset");

View File

@ -64,15 +64,13 @@ private:
}
}
void WriteLog(const char* message, ...) const ATTR_PRINTF(2, 3)
template <typename... Args>
void WriteLog(const char* message, Args&&... args) const
{
std::string buffer;
VAFORMAT(buffer, message, message);
if (notifyopers)
ServerInstance->SNO.WriteToSnoMask('a', buffer);
ServerInstance->SNO.WriteToSnoMask('a', message, std::forward<Args>(args)...);
else
ServerInstance->Logs.Normal(MODNAME, buffer);
ServerInstance->Logs.Normal(MODNAME, message, std::forward<Args>(args)...);
}
public:

View File

@ -55,20 +55,6 @@ void SnomaskManager::WriteGlobalSno(char letter, const std::string& text)
ServerInstance->PI->SendSNONotice(letter, text);
}
void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
{
std::string textbuffer;
VAFORMAT(textbuffer, text, text);
this->WriteToSnoMask(letter, textbuffer);
}
void SnomaskManager::WriteGlobalSno(char letter, const char* text, ...)
{
std::string textbuffer;
VAFORMAT(textbuffer, text, text);
this->WriteGlobalSno(letter, textbuffer);
}
SnomaskManager::SnomaskManager()
{
EnableSnomask('a', "ANNOUNCEMENT");