Change: Replace ScriptLog data array with std::deque. (#10770)
Due to cyclic header dependency this requires moving the data types used by ScriptLog out of the ScriptLog class.
This commit is contained in:
@@ -180,6 +180,7 @@ add_files(
|
||||
script_league.hpp
|
||||
script_list.hpp
|
||||
script_log.hpp
|
||||
script_log_types.hpp
|
||||
script_map.hpp
|
||||
script_marine.hpp
|
||||
script_newgrf.hpp
|
||||
|
@@ -53,7 +53,7 @@
|
||||
|
||||
char log_message[1024];
|
||||
seprintf(log_message, lastof(log_message), "Break: %s", message);
|
||||
ScriptLog::Log(ScriptLog::LOG_SQ_ERROR, log_message);
|
||||
ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, log_message);
|
||||
|
||||
/* Inform script developer that their script has been paused and
|
||||
* needs manual action to continue. */
|
||||
@@ -66,7 +66,7 @@
|
||||
|
||||
/* static */ void ScriptController::Print(bool error_msg, const char *message)
|
||||
{
|
||||
ScriptLog::Log(error_msg ? ScriptLog::LOG_SQ_ERROR : ScriptLog::LOG_SQ_INFO, message);
|
||||
ScriptLog::Log(error_msg ? ScriptLogTypes::LOG_SQ_ERROR : ScriptLogTypes::LOG_SQ_INFO, message);
|
||||
}
|
||||
|
||||
ScriptController::ScriptController(CompanyID company) :
|
||||
|
@@ -8,6 +8,7 @@
|
||||
/** @file script_log.cpp Implementation of ScriptLog. */
|
||||
|
||||
#include "../../stdafx.h"
|
||||
#include "script_log_types.hpp"
|
||||
#include "script_log.hpp"
|
||||
#include "../../core/alloc_func.hpp"
|
||||
#include "../../debug.h"
|
||||
@@ -18,75 +19,45 @@
|
||||
|
||||
/* static */ void ScriptLog::Info(const char *message)
|
||||
{
|
||||
ScriptLog::Log(LOG_INFO, message);
|
||||
ScriptLog::Log(ScriptLogTypes::LOG_INFO, message);
|
||||
}
|
||||
|
||||
/* static */ void ScriptLog::Warning(const char *message)
|
||||
{
|
||||
ScriptLog::Log(LOG_WARNING, message);
|
||||
ScriptLog::Log(ScriptLogTypes::LOG_WARNING, message);
|
||||
}
|
||||
|
||||
/* static */ void ScriptLog::Error(const char *message)
|
||||
{
|
||||
ScriptLog::Log(LOG_ERROR, message);
|
||||
ScriptLog::Log(ScriptLogTypes::LOG_ERROR, message);
|
||||
}
|
||||
|
||||
/* static */ void ScriptLog::Log(ScriptLog::ScriptLogType level, const char *message)
|
||||
/* static */ void ScriptLog::Log(ScriptLogTypes::ScriptLogType level, const char *message)
|
||||
{
|
||||
if (ScriptObject::GetLogPointer() == nullptr) {
|
||||
ScriptObject::GetLogPointer() = new LogData();
|
||||
LogData *log = (LogData *)ScriptObject::GetLogPointer();
|
||||
ScriptLogTypes::LogData &logdata = ScriptObject::GetLogData();
|
||||
|
||||
log->lines = CallocT<char *>(400);
|
||||
log->type = CallocT<ScriptLog::ScriptLogType>(400);
|
||||
log->count = 400;
|
||||
log->pos = log->count - 1;
|
||||
log->used = 0;
|
||||
}
|
||||
LogData *log = (LogData *)ScriptObject::GetLogPointer();
|
||||
/* Limit the log to 400 lines. */
|
||||
if (logdata.size() >= 400U) logdata.pop_front();
|
||||
|
||||
/* Go to the next log-line */
|
||||
log->pos = (log->pos + 1) % log->count;
|
||||
|
||||
if (log->used != log->count) log->used++;
|
||||
|
||||
/* Free last message, and write new message */
|
||||
free(log->lines[log->pos]);
|
||||
log->lines[log->pos] = stredup(message);
|
||||
log->type[log->pos] = level;
|
||||
auto &line = logdata.emplace_back();
|
||||
line.type = level;
|
||||
|
||||
/* Cut string after first \n */
|
||||
char *p;
|
||||
while ((p = strchr(log->lines[log->pos], '\n')) != nullptr) {
|
||||
*p = '\0';
|
||||
break;
|
||||
}
|
||||
const char *newline = strchr(message, '\n');
|
||||
line.text = std::string(message, 0, newline == nullptr ? strlen(message) : newline - message);
|
||||
|
||||
char logc;
|
||||
|
||||
switch (level) {
|
||||
case LOG_SQ_ERROR: logc = 'S'; break;
|
||||
case LOG_ERROR: logc = 'E'; break;
|
||||
case LOG_SQ_INFO: logc = 'P'; break;
|
||||
case LOG_WARNING: logc = 'W'; break;
|
||||
case LOG_INFO: logc = 'I'; break;
|
||||
default: logc = '?'; break;
|
||||
case ScriptLogTypes::LOG_SQ_ERROR: logc = 'S'; break;
|
||||
case ScriptLogTypes::LOG_ERROR: logc = 'E'; break;
|
||||
case ScriptLogTypes::LOG_SQ_INFO: logc = 'P'; break;
|
||||
case ScriptLogTypes::LOG_WARNING: logc = 'W'; break;
|
||||
case ScriptLogTypes::LOG_INFO: logc = 'I'; break;
|
||||
default: logc = '?'; break;
|
||||
}
|
||||
|
||||
/* Also still print to debug window */
|
||||
Debug(script, level, "[{}] [{}] {}", (uint)ScriptObject::GetRootCompany(), logc, log->lines[log->pos]);
|
||||
Debug(script, level, "[{}] [{}] {}", (uint)ScriptObject::GetRootCompany(), logc, line.text);
|
||||
InvalidateWindowData(WC_SCRIPT_DEBUG, 0, ScriptObject::GetRootCompany());
|
||||
}
|
||||
|
||||
/* static */ void ScriptLog::FreeLogPointer()
|
||||
{
|
||||
LogData *log = (LogData *)ScriptObject::GetLogPointer();
|
||||
|
||||
for (int i = 0; i < log->count; i++) {
|
||||
free(log->lines[i]);
|
||||
}
|
||||
|
||||
free(log->lines);
|
||||
free(log->type);
|
||||
delete log;
|
||||
}
|
||||
|
@@ -22,32 +22,6 @@ class ScriptLog : public ScriptObject {
|
||||
friend class ScriptController;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Log levels; The value is also feed to Debug() lvl.
|
||||
* This has no use for you, as script writer.
|
||||
* @api -all
|
||||
*/
|
||||
enum ScriptLogType {
|
||||
LOG_SQ_ERROR = 0, ///< Squirrel printed an error.
|
||||
LOG_ERROR = 1, ///< User printed an error.
|
||||
LOG_SQ_INFO = 2, ///< Squirrel printed some info.
|
||||
LOG_WARNING = 3, ///< User printed some warning.
|
||||
LOG_INFO = 4, ///< User printed some info.
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal representation of the log-data inside the script.
|
||||
* This has no use for you, as script writer.
|
||||
* @api -all
|
||||
*/
|
||||
struct LogData {
|
||||
char **lines; ///< The log-lines.
|
||||
ScriptLog::ScriptLogType *type; ///< Per line, which type of log it was.
|
||||
int count; ///< Total amount of log-lines possible.
|
||||
int pos; ///< Current position in lines.
|
||||
int used; ///< Total amount of used log-lines.
|
||||
};
|
||||
|
||||
/**
|
||||
* Print an Info message to the logs.
|
||||
* @param message The message to log.
|
||||
@@ -69,17 +43,11 @@ public:
|
||||
*/
|
||||
static void Error(const char *message);
|
||||
|
||||
/**
|
||||
* Free the log pointer.
|
||||
* @api -all
|
||||
*/
|
||||
static void FreeLogPointer();
|
||||
|
||||
private:
|
||||
/**
|
||||
* Internal command to log the message in a common way.
|
||||
*/
|
||||
static void Log(ScriptLog::ScriptLogType level, const char *message);
|
||||
static void Log(ScriptLogTypes::ScriptLogType level, const char *message);
|
||||
};
|
||||
|
||||
#endif /* SCRIPT_LOG_HPP */
|
||||
|
47
src/script/api/script_log_types.hpp
Normal file
47
src/script/api/script_log_types.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
||||
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file script_log_types.hpp Data types for script log messages. */
|
||||
|
||||
#ifndef SCRIPT_LOG_TYPES_HPP
|
||||
#define SCRIPT_LOG_TYPES_HPP
|
||||
|
||||
#include <deque>
|
||||
|
||||
namespace ScriptLogTypes {
|
||||
/**
|
||||
* Log levels; The value is also feed to Debug() lvl.
|
||||
* This has no use for you, as script writer.
|
||||
* @api -all
|
||||
*/
|
||||
enum ScriptLogType {
|
||||
LOG_SQ_ERROR = 0, ///< Squirrel printed an error.
|
||||
LOG_ERROR = 1, ///< User printed an error.
|
||||
LOG_SQ_INFO = 2, ///< Squirrel printed some info.
|
||||
LOG_WARNING = 3, ///< User printed some warning.
|
||||
LOG_INFO = 4, ///< User printed some info.
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal representation of the log-data inside the script.
|
||||
* This has no use for you, as script writer.
|
||||
* @api -all
|
||||
*/
|
||||
struct LogLine {
|
||||
std::string text; ///< The text
|
||||
ScriptLogType type; ///< Text type
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal representation of the log-data inside the script.
|
||||
* This has no use for you, as script writer.
|
||||
* @api -all
|
||||
*/
|
||||
using LogData = std::deque<LogLine>; ///< The log type
|
||||
};
|
||||
|
||||
#endif /* SCRIPT_LOG_TYPES_HPP */
|
@@ -213,7 +213,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
|
||||
return GetStorage()->event_data;
|
||||
}
|
||||
|
||||
/* static */ void *&ScriptObject::GetLogPointer()
|
||||
/* static */ ScriptLogTypes::LogData &ScriptObject::GetLogData()
|
||||
{
|
||||
return GetStorage()->log_data;
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@
|
||||
#include "../../core/random_func.hpp"
|
||||
|
||||
#include "script_types.hpp"
|
||||
#include "script_log_types.hpp"
|
||||
#include "../script_suspend.hpp"
|
||||
#include "../squirrel.hpp"
|
||||
|
||||
@@ -276,7 +277,7 @@ protected:
|
||||
/**
|
||||
* Get the pointer to store log message in.
|
||||
*/
|
||||
static void *&GetLogPointer();
|
||||
static ScriptLogTypes::LogData &GetLogData();
|
||||
|
||||
/**
|
||||
* Get an allocated string with all control codes stripped off.
|
||||
|
Reference in New Issue
Block a user