1
0
Fork 0

Codechange: use std::string for script log calls

pull/10801/head
Rubidium 2023-05-05 19:32:27 +02:00 committed by rubidium42
parent 77177f7e8b
commit b24a6bb8f3
4 changed files with 14 additions and 17 deletions

View File

@ -45,15 +45,13 @@
throw Script_Suspend(ticks, nullptr); throw Script_Suspend(ticks, nullptr);
} }
/* static */ void ScriptController::Break(const char* message) /* static */ void ScriptController::Break(const std::string &message)
{ {
if (_network_dedicated || !_settings_client.gui.ai_developer_tools) return; if (_network_dedicated || !_settings_client.gui.ai_developer_tools) return;
ScriptObject::GetActiveInstance()->Pause(); ScriptObject::GetActiveInstance()->Pause();
char log_message[1024]; ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, fmt::format("Break: {}", message));
seprintf(log_message, lastof(log_message), "Break: %s", message);
ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, log_message);
/* Inform script developer that their script has been paused and /* Inform script developer that their script has been paused and
* needs manual action to continue. */ * needs manual action to continue. */
@ -64,7 +62,7 @@
} }
} }
/* static */ void ScriptController::Print(bool error_msg, const char *message) /* static */ void ScriptController::Print(bool error_msg, const std::string &message)
{ {
ScriptLog::Log(error_msg ? ScriptLogTypes::LOG_SQ_ERROR : ScriptLogTypes::LOG_SQ_INFO, message); ScriptLog::Log(error_msg ? ScriptLogTypes::LOG_SQ_ERROR : ScriptLogTypes::LOG_SQ_INFO, message);
} }

View File

@ -181,7 +181,7 @@ public:
* @note gui.ai_developer_tools setting must be enabled or the break is * @note gui.ai_developer_tools setting must be enabled or the break is
* ignored. * ignored.
*/ */
static void Break(const char* message); static void Break(const std::string &message);
/** /**
* When Squirrel triggers a print, this function is called. * When Squirrel triggers a print, this function is called.
@ -190,7 +190,7 @@ public:
* @param message The message Squirrel logged. * @param message The message Squirrel logged.
* @note Use ScriptLog.Info/Warning/Error instead of 'print'. * @note Use ScriptLog.Info/Warning/Error instead of 'print'.
*/ */
static void Print(bool error_msg, const char *message); static void Print(bool error_msg, const std::string &message);
/** /**
* Import a library. * Import a library.

View File

@ -17,22 +17,22 @@
#include "../../safeguards.h" #include "../../safeguards.h"
/* static */ void ScriptLog::Info(const char *message) /* static */ void ScriptLog::Info(const std::string &message)
{ {
ScriptLog::Log(ScriptLogTypes::LOG_INFO, message); ScriptLog::Log(ScriptLogTypes::LOG_INFO, message);
} }
/* static */ void ScriptLog::Warning(const char *message) /* static */ void ScriptLog::Warning(const std::string &message)
{ {
ScriptLog::Log(ScriptLogTypes::LOG_WARNING, message); ScriptLog::Log(ScriptLogTypes::LOG_WARNING, message);
} }
/* static */ void ScriptLog::Error(const char *message) /* static */ void ScriptLog::Error(const std::string &message)
{ {
ScriptLog::Log(ScriptLogTypes::LOG_ERROR, message); ScriptLog::Log(ScriptLogTypes::LOG_ERROR, message);
} }
/* static */ void ScriptLog::Log(ScriptLogTypes::ScriptLogType level, const char *message) /* static */ void ScriptLog::Log(ScriptLogTypes::ScriptLogType level, const std::string &message)
{ {
ScriptLogTypes::LogData &logdata = ScriptObject::GetLogData(); ScriptLogTypes::LogData &logdata = ScriptObject::GetLogData();
@ -43,8 +43,7 @@
line.type = level; line.type = level;
/* Cut string after first \n */ /* Cut string after first \n */
const char *newline = strchr(message, '\n'); line.text = message.substr(0, message.find_first_of('\n'));
line.text = std::string(message, 0, newline == nullptr ? strlen(message) : newline - message);
char logc; char logc;

View File

@ -27,27 +27,27 @@ public:
* @param message The message to log. * @param message The message to log.
* @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs. * @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs.
*/ */
static void Info(const char *message); static void Info(const std::string &message);
/** /**
* Print a Warning message to the logs. * Print a Warning message to the logs.
* @param message The message to log. * @param message The message to log.
* @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs. * @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs.
*/ */
static void Warning(const char *message); static void Warning(const std::string &message);
/** /**
* Print an Error message to the logs. * Print an Error message to the logs.
* @param message The message to log. * @param message The message to log.
* @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs. * @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs.
*/ */
static void Error(const char *message); static void Error(const std::string &message);
private: private:
/** /**
* Internal command to log the message in a common way. * Internal command to log the message in a common way.
*/ */
static void Log(ScriptLogTypes::ScriptLogType level, const char *message); static void Log(ScriptLogTypes::ScriptLogType level, const std::string &message);
}; };
#endif /* SCRIPT_LOG_HPP */ #endif /* SCRIPT_LOG_HPP */