Script: Only log each string parameter warning message once

pull/507/head
Jonathan G Rennison 1 year ago
parent ce5c758f2b
commit 29c451d1a8

@ -90,3 +90,11 @@
free(log->type);
delete log;
}
/* static */ void ScriptLog::LogOnce(ScriptLog::ScriptLogType level, std::string &&message)
{
if (ScriptObject::IsNewUniqueLogMessage(message)) {
ScriptLog::Log(level, message.c_str());
ScriptObject::RegisterUniqueLogMessage(std::move(message));
}
}

@ -75,6 +75,12 @@ public:
*/
static void FreeLogPointer();
/**
* Log this message once.
* @api -all
*/
static void LogOnce(ScriptLog::ScriptLogType level, std::string &&message);
private:
/**
* Internal command to log the message in a common way.

@ -411,3 +411,13 @@ void ScriptObject::InitializeRandomizers()
ScriptObject::GetRandomizer(owner).SetSeed(random.Next());
}
}
/* static */ bool ScriptObject::IsNewUniqueLogMessage(const std::string &msg)
{
return !GetStorage()->seen_unique_log_messages.contains(msg);
}
/* static */ void ScriptObject::RegisterUniqueLogMessage(std::string &&msg)
{
GetStorage()->seen_unique_log_messages.emplace(std::move(msg));
}

@ -304,6 +304,10 @@ protected:
*/
static char *GetString(StringID string);
static bool IsNewUniqueLogMessage(const std::string &msg);
static void RegisterUniqueLogMessage(std::string &&msg);
private:
/**
* Store a new_vehicle_id per company.

@ -170,12 +170,12 @@ const std::string ScriptText::GetEncodedText()
return buf;
}
void ScriptText::_TextParamError(const std::string &msg)
void ScriptText::_TextParamError(std::string msg)
{
if (this->GetActiveInstance()->IsTextParamMismatchAllowed()) {
ScriptLog::Error(msg.c_str());
ScriptLog::LogOnce(ScriptLog::LOG_ERROR, std::move(msg));
} else {
throw Script_FatalError(msg);
throw Script_FatalError(std::move(msg));
}
}

@ -135,7 +135,7 @@ private:
std::variant<SQInteger, std::string, ScriptTextRef> param[SCRIPT_TEXT_MAX_PARAMETERS];
int paramc;
void _TextParamError(const std::string &msg);
void _TextParamError(std::string msg);
/**
* Internal function for recursive calling this function over multiple

@ -22,6 +22,9 @@ public:
Script_FatalError(const std::string &msg) :
msg(msg)
{}
Script_FatalError(std::string &&msg) :
msg(std::move(msg))
{}
/**
* The error message associated with the fatal error.

@ -16,6 +16,7 @@
#include "../group.h"
#include "../goal_type.h"
#include "../story_type.h"
#include "../3rdparty/robin_hood/robin_hood.h"
#include "table/strings.h"
#include <vector>
@ -66,6 +67,8 @@ private:
void *event_data; ///< Pointer to the event data storage.
void *log_data; ///< Pointer to the log data storage.
robin_hood::unordered_node_set<std::string> seen_unique_log_messages; ///< Messages which have already been logged once and don't need to be logged again
public:
ScriptStorage() :
mode (nullptr),

Loading…
Cancel
Save