diff --git a/src/script/api/script_controller.cpp b/src/script/api/script_controller.cpp index b083fccddb..cd39319f0d 100644 --- a/src/script/api/script_controller.cpp +++ b/src/script/api/script_controller.cpp @@ -45,15 +45,13 @@ 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; ScriptObject::GetActiveInstance()->Pause(); - char log_message[1024]; - seprintf(log_message, lastof(log_message), "Break: %s", message); - ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, log_message); + ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, fmt::format("Break: {}", message)); /* Inform script developer that their script has been paused and * 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); } diff --git a/src/script/api/script_controller.hpp b/src/script/api/script_controller.hpp index ca6bffdf39..f1e7cc9ea5 100644 --- a/src/script/api/script_controller.hpp +++ b/src/script/api/script_controller.hpp @@ -181,7 +181,7 @@ public: * @note gui.ai_developer_tools setting must be enabled or the break is * ignored. */ - static void Break(const char* message); + static void Break(const std::string &message); /** * When Squirrel triggers a print, this function is called. @@ -190,7 +190,7 @@ public: * @param message The message Squirrel logged. * @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. diff --git a/src/script/api/script_log.cpp b/src/script/api/script_log.cpp index dd3a78c071..adab8f709a 100644 --- a/src/script/api/script_log.cpp +++ b/src/script/api/script_log.cpp @@ -17,22 +17,22 @@ #include "../../safeguards.h" -/* static */ void ScriptLog::Info(const char *message) +/* static */ void ScriptLog::Info(const std::string &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); } -/* static */ void ScriptLog::Error(const char *message) +/* static */ void ScriptLog::Error(const std::string &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(); @@ -43,8 +43,7 @@ line.type = level; /* Cut string after first \n */ - const char *newline = strchr(message, '\n'); - line.text = std::string(message, 0, newline == nullptr ? strlen(message) : newline - message); + line.text = message.substr(0, message.find_first_of('\n')); char logc; diff --git a/src/script/api/script_log.hpp b/src/script/api/script_log.hpp index b2acd1e171..cac45a3067 100644 --- a/src/script/api/script_log.hpp +++ b/src/script/api/script_log.hpp @@ -27,27 +27,27 @@ public: * @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. */ - static void Info(const char *message); + static void Info(const std::string &message); /** * Print a Warning message to the logs. * @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. */ - static void Warning(const char *message); + static void Warning(const std::string &message); /** * Print an Error message to the logs. * @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. */ - static void Error(const char *message); + static void Error(const std::string &message); private: /** * 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 */