mirror of https://github.com/OpenTTD/OpenTTD
Fix #11646: Non-thread safe shared buffer returned from GetLogPrefix().
Return string from GetLogPrefix instead of shared string's buffer.pull/11832/head
parent
2b599c9d00
commit
28ef5146ba
|
@ -51,10 +51,9 @@ static void IConsoleWriteToLogFile(const std::string &string)
|
||||||
{
|
{
|
||||||
if (_iconsole_output_file != nullptr) {
|
if (_iconsole_output_file != nullptr) {
|
||||||
/* if there is an console output file ... also print it there */
|
/* if there is an console output file ... also print it there */
|
||||||
const char *header = GetLogPrefix();
|
try {
|
||||||
if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
|
fmt::print(_iconsole_output_file, "{}{}\n", GetLogPrefix(), string);
|
||||||
fwrite(string.c_str(), string.size(), 1, _iconsole_output_file) != 1 ||
|
} catch (const std::system_error &) {
|
||||||
fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
|
|
||||||
fclose(_iconsole_output_file);
|
fclose(_iconsole_output_file);
|
||||||
_iconsole_output_file = nullptr;
|
_iconsole_output_file = nullptr;
|
||||||
IConsolePrint(CC_ERROR, "Cannot write to console log file; closing the log file.");
|
IConsolePrint(CC_ERROR, "Cannot write to console log file; closing the log file.");
|
||||||
|
|
|
@ -219,18 +219,16 @@ std::string GetDebugString()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the prefix for logs; if show_date_in_logs is enabled it returns
|
* Get the prefix for logs; if show_date_in_logs is enabled it returns
|
||||||
* the date, otherwise it returns nothing.
|
* the date, otherwise it returns an empty string.
|
||||||
* @return the prefix for logs (do not free), never nullptr
|
* @return the prefix for logs.
|
||||||
*/
|
*/
|
||||||
const char *GetLogPrefix()
|
std::string GetLogPrefix()
|
||||||
{
|
{
|
||||||
static std::string _log_prefix;
|
std::string log_prefix;
|
||||||
if (_settings_client.gui.show_date_in_logs) {
|
if (_settings_client.gui.show_date_in_logs) {
|
||||||
_log_prefix = fmt::format("[{:%Y-%m-%d %H:%M:%S}] ", fmt::localtime(time(nullptr)));
|
log_prefix = fmt::format("[{:%Y-%m-%d %H:%M:%S}] ", fmt::localtime(time(nullptr)));
|
||||||
} else {
|
|
||||||
_log_prefix.clear();
|
|
||||||
}
|
}
|
||||||
return _log_prefix.c_str();
|
return log_prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -120,7 +120,7 @@ std::string GetDebugString();
|
||||||
void ShowInfoI(const std::string &str);
|
void ShowInfoI(const std::string &str);
|
||||||
#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
|
#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
|
||||||
|
|
||||||
const char *GetLogPrefix();
|
std::string GetLogPrefix();
|
||||||
|
|
||||||
void DebugSendRemoteMessages();
|
void DebugSendRemoteMessages();
|
||||||
void DebugReconsiderSendRemoteMessages();
|
void DebugReconsiderSendRemoteMessages();
|
||||||
|
|
Loading…
Reference in New Issue