lock(m_Mutex);
++m_NumberOfErrors;
if (m_UseDebugPrintf)
debug_printf("ERROR: %.16000s\n", message);
if (g_Console) g_Console->InsertMessage(std::string("ERROR: ") + message);
*m_InterestingLog << "ERROR: " << cmessage << "
\n";
m_InterestingLog->flush();
*m_MainLog << "ERROR: " << cmessage << "
\n";
m_MainLog->flush();
PushRenderMessage(Error, message);
}
void CLogger::WriteWarning(const char* message)
{
std::string cmessage = ToHTML(message);
- CScopeLock lock(m_Mutex);
+ std::lock_guard lock(m_Mutex);
++m_NumberOfWarnings;
if (m_UseDebugPrintf)
debug_printf("WARNING: %s\n", message);
if (g_Console) g_Console->InsertMessage(std::string("WARNING: ") + message);
*m_InterestingLog << "WARNING: " << cmessage << "
\n";
m_InterestingLog->flush();
*m_MainLog << "WARNING: " << cmessage << "
\n";
m_MainLog->flush();
PushRenderMessage(Warning, message);
}
void CLogger::Render()
{
PROFILE3_GPU("logger");
CleanupRenderQueue();
CStrIntern font_name("mono-stroke-10");
CFontMetrics font(font_name);
int lineSpacing = font.GetLineSpacing();
CShaderTechniquePtr textTech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text);
textTech->BeginPass();
CTextRenderer textRenderer(textTech->GetShader());
textRenderer.Font(font_name);
textRenderer.Color(1.0f, 1.0f, 1.0f);
// Offset by an extra 35px vertically to avoid the top bar.
textRenderer.Translate(4.0f, 35.0f + lineSpacing, 0.0f);
// (Lock must come after loading the CFont, since that might log error messages
// and attempt to lock the mutex recursively which is forbidden)
- CScopeLock lock(m_Mutex);
+ std::lock_guard lock(m_Mutex);
for (const RenderedMessage& msg : m_RenderMessages)
{
const char* type;
if (msg.method == Normal)
{
type = "info";
textRenderer.Color(0.0f, 0.8f, 0.0f);
}
else if (msg.method == Warning)
{
type = "warning";
textRenderer.Color(1.0f, 1.0f, 0.0f);
}
else
{
type = "error";
textRenderer.Color(1.0f, 0.0f, 0.0f);
}
CMatrix3D savedTransform = textRenderer.GetTransform();
textRenderer.PrintfAdvance(L"[%8.3f] %hs: ", msg.time, type);
// Display the actual message in white so it's more readable
textRenderer.Color(1.0f, 1.0f, 1.0f);
textRenderer.Put(0.0f, 0.0f, msg.message.c_str());
textRenderer.SetTransform(savedTransform);
textRenderer.Translate(0.0f, (float)lineSpacing, 0.0f);
}
textRenderer.Render();
textTech->EndPass();
}
void CLogger::PushRenderMessage(ELogMethod method, const char* message)
{
double now = timer_Time();
// Add each message line separately
const char* pos = message;
const char* eol;
while ((eol = strchr(pos, '\n')) != NULL)
{
if (eol != pos)
{
RenderedMessage r = { method, now, std::string(pos, eol) };
m_RenderMessages.push_back(r);
}
pos = eol + 1;
}
// Add the last line, if we didn't end on a \n
if (*pos != '\0')
{
RenderedMessage r = { method, now, std::string(pos) };
m_RenderMessages.push_back(r);
}
}
void CLogger::CleanupRenderQueue()
{
- CScopeLock lock(m_Mutex);
+ std::lock_guard lock(m_Mutex);
if (m_RenderMessages.empty())
return;
double now = timer_Time();
// Initialise the timer on the first call (since we can't do it in the ctor)
if (m_RenderLastEraseTime == -1.0)
m_RenderLastEraseTime = now;
// Delete old messages, approximately at the given rate limit (and at most one per frame)
if (now - m_RenderLastEraseTime > 1.0/RENDER_TIMEOUT_RATE)
{
if (m_RenderMessages[0].time + RENDER_TIMEOUT < now)
{
m_RenderMessages.pop_front();
m_RenderLastEraseTime = now;
}
}
// If there's still too many then delete the oldest
if (m_RenderMessages.size() > RENDER_LIMIT)
m_RenderMessages.erase(m_RenderMessages.begin(), m_RenderMessages.end() - RENDER_LIMIT);
}
TestLogger::TestLogger()
{
m_OldLogger = g_Logger;
g_Logger = new CLogger(&m_Stream, &blackHoleStream, false, false);
}
TestLogger::~TestLogger()
{
delete g_Logger;
g_Logger = m_OldLogger;
}
std::string TestLogger::GetOutput()
{
return m_Stream.str();
}
TestStdoutLogger::TestStdoutLogger()
{
m_OldLogger = g_Logger;
g_Logger = new CLogger(&std::cout, &blackHoleStream, false, false);
}
TestStdoutLogger::~TestStdoutLogger()
{
delete g_Logger;
g_Logger = m_OldLogger;
}
Index: ps/trunk/source/ps/CLogger.h
===================================================================
--- ps/trunk/source/ps/CLogger.h (revision 22343)
+++ ps/trunk/source/ps/CLogger.h (revision 22344)
@@ -1,140 +1,140 @@
-/* Copyright (C) 2015 Wildfire Games.
+/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 0 A.D. If not, see .
*/
#ifndef INCLUDED_CLOGGER
#define INCLUDED_CLOGGER
#include
+#include
#include
#include
#include
-#include "ps/ThreadUtil.h"
#include "third_party/cppformat/format.h"
class CLogger;
extern CLogger* g_Logger;
#define LOGMESSAGE(...) g_Logger->WriteMessage(fmt::sprintf(__VA_ARGS__).c_str(), false)
#define LOGMESSAGERENDER(...) g_Logger->WriteMessage(fmt::sprintf(__VA_ARGS__).c_str(), true)
#define LOGWARNING(...) g_Logger->WriteWarning(fmt::sprintf(__VA_ARGS__).c_str())
#define LOGERROR(...) g_Logger->WriteError (fmt::sprintf(__VA_ARGS__).c_str())
/**
* Error/warning/message logging class.
*
* Thread-safety:
* - Expected to be constructed/destructed in the main thread.
* - The message logging functions may be called from any thread
* while the object is alive.
*/
class CLogger
{
NONCOPYABLE(CLogger);
public:
enum ELogMethod
{
Normal,
Error,
Warning
};
// Default constructor - outputs to normal log files
CLogger();
// Special constructor (mostly for testing) - outputs to provided streams.
// Can take ownership of streams and delete them in the destructor.
CLogger(std::ostream* mainLog, std::ostream* interestingLog, bool takeOwnership, bool useDebugPrintf);
~CLogger();
// Functions to write different message types (Errors and warnings are placed
// both in mainLog and intrestingLog.)
void WriteMessage(const char* message, bool doRender);
void WriteError (const char* message);
void WriteWarning(const char* message);
// Render recent log messages onto the screen
void Render();
private:
void Init();
void PushRenderMessage(ELogMethod method, const char* message);
// Delete old timed-out entries from the list of text to render
void CleanupRenderQueue();
// the output streams
std::ostream* m_MainLog;
std::ostream* m_InterestingLog;
bool m_OwnsStreams;
// whether errors should be reported via debug_printf (default)
// or suppressed (for tests that intentionally trigger errors)
bool m_UseDebugPrintf;
// vars to hold message counts
int m_NumberOfMessages;
int m_NumberOfErrors;
int m_NumberOfWarnings;
// Used for Render()
struct RenderedMessage
{
ELogMethod method;
double time;
std::string message;
};
std::deque m_RenderMessages;
double m_RenderLastEraseTime;
// Lock for all state modified by logging commands
- CMutex m_Mutex;
+ std::mutex m_Mutex;
};
/**
* Helper class for unit tests - captures all log output while it is in scope,
* and returns it as a single string.
*/
class TestLogger
{
NONCOPYABLE(TestLogger);
public:
TestLogger();
~TestLogger();
std::string GetOutput();
private:
CLogger* m_OldLogger;
std::stringstream m_Stream;
};
/**
* Helper class for unit tests - redirects all log output to stdout.
*/
class TestStdoutLogger
{
NONCOPYABLE(TestStdoutLogger);
public:
TestStdoutLogger();
~TestStdoutLogger();
private:
CLogger* m_OldLogger;
};
#endif
Index: ps/trunk/source/ps/ConfigDB.cpp
===================================================================
--- ps/trunk/source/ps/ConfigDB.cpp (revision 22343)
+++ ps/trunk/source/ps/ConfigDB.cpp (revision 22344)
@@ -1,469 +1,455 @@
-/* Copyright (C) 2018 Wildfire Games.
+/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 0 A.D. If not, see .
*/
#include "precompiled.h"
#include "ConfigDB.h"
#include
#include "lib/allocators/shared_ptr.h"
#include "lib/file/vfs/vfs_path.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "ps/Filesystem.h"
-#include "ps/ThreadUtil.h"
+#include
#include
+
typedef std::map TConfigMap;
TConfigMap CConfigDB::m_Map[CFG_LAST];
VfsPath CConfigDB::m_ConfigFile[CFG_LAST];
bool CConfigDB::m_HasChanges[CFG_LAST];
-static pthread_mutex_t cfgdb_mutex = PTHREAD_MUTEX_INITIALIZER;
+static std::recursive_mutex cfgdb_mutex;
// These entries will not be printed to logfiles, so that logfiles can be shared without leaking personal or sensitive data
static const std::unordered_set g_UnloggedEntries = {
"lobby.password",
"lobby.buddies",
"userreport.id" // authentication token for GDPR personal data requests
};
-CConfigDB::CConfigDB()
-{
- // Recursive mutex needed for WriteFile
- pthread_mutexattr_t attr;
- int err;
- err = pthread_mutexattr_init(&attr);
- ENSURE(err == 0);
- err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- ENSURE(err == 0);
- err = pthread_mutex_init(&cfgdb_mutex, &attr);
- ENSURE(err == 0);
- err = pthread_mutexattr_destroy(&attr);
- ENSURE(err == 0);
-}
-
#define CHECK_NS(rval)\
do {\
if (ns < 0 || ns >= CFG_LAST)\
{\
debug_warn(L"CConfigDB: Invalid ns value");\
return rval;\
}\
} while (false)
namespace {
template void Get(const CStr& value, T& ret)
{
std::stringstream ss(value);
ss >> ret;
}
template<> void Get<>(const CStr& value, bool& ret)
{
ret = value == "true";
}
template<> void Get<>(const CStr& value, std::string& ret)
{
ret = value;
}
std::string EscapeString(const CStr& str)
{
std::string ret;
for (size_t i = 0; i < str.length(); ++i)
{
if (str[i] == '\\')
ret += "\\\\";
else if (str[i] == '"')
ret += "\\\"";
else
ret += str[i];
}
return ret;
}
} // namespace
#define GETVAL(type)\
void CConfigDB::GetValue(EConfigNamespace ns, const CStr& name, type& value)\
{\
CHECK_NS(;);\
- CScopeLock s(&cfgdb_mutex);\
+ std::lock_guard s(cfgdb_mutex);\
TConfigMap::iterator it = m_Map[CFG_COMMAND].find(name);\
if (it != m_Map[CFG_COMMAND].end())\
{\
Get(it->second[0], value);\
return;\
}\
for (int search_ns = ns; search_ns >= 0; --search_ns)\
{\
it = m_Map[search_ns].find(name);\
if (it != m_Map[search_ns].end())\
{\
Get(it->second[0], value);\
return;\
}\
}\
}
GETVAL(bool)
GETVAL(int)
GETVAL(u32)
GETVAL(float)
GETVAL(double)
GETVAL(std::string)
#undef GETVAL
bool CConfigDB::HasChanges(EConfigNamespace ns) const
{
CHECK_NS(false);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
return m_HasChanges[ns];
}
void CConfigDB::SetChanges(EConfigNamespace ns, bool value)
{
CHECK_NS(;);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
m_HasChanges[ns] = value;
}
void CConfigDB::GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const
{
CHECK_NS(;);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
TConfigMap::iterator it = m_Map[CFG_COMMAND].find(name);
if (it != m_Map[CFG_COMMAND].end())
{
values = it->second;
return;
}
for (int search_ns = ns; search_ns >= 0; --search_ns)
{
it = m_Map[search_ns].find(name);
if (it != m_Map[search_ns].end())
{
values = it->second;
return;
}
}
}
EConfigNamespace CConfigDB::GetValueNamespace(EConfigNamespace ns, const CStr& name) const
{
CHECK_NS(CFG_LAST);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
TConfigMap::iterator it = m_Map[CFG_COMMAND].find(name);
if (it != m_Map[CFG_COMMAND].end())
return CFG_COMMAND;
for (int search_ns = ns; search_ns >= 0; --search_ns)
{
it = m_Map[search_ns].find(name);
if (it != m_Map[search_ns].end())
return (EConfigNamespace)search_ns;
}
return CFG_LAST;
}
std::map CConfigDB::GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const
{
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
std::map ret;
CHECK_NS(ret);
// Loop upwards so that values in later namespaces can override
// values in earlier namespaces
for (int search_ns = 0; search_ns <= ns; ++search_ns)
for (const std::pair& p : m_Map[search_ns])
if (boost::algorithm::starts_with(p.first, prefix))
ret[p.first] = p.second;
for (const std::pair& p : m_Map[CFG_COMMAND])
if (boost::algorithm::starts_with(p.first, prefix))
ret[p.first] = p.second;
return ret;
}
void CConfigDB::SetValueString(EConfigNamespace ns, const CStr& name, const CStr& value)
{
CHECK_NS(;);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
TConfigMap::iterator it = m_Map[ns].find(name);
if (it == m_Map[ns].end())
it = m_Map[ns].insert(m_Map[ns].begin(), make_pair(name, CConfigValueSet(1)));
it->second[0] = value;
}
void CConfigDB::SetValueBool(EConfigNamespace ns, const CStr& name, const bool value)
{
CStr valueString = value ? "true" : "false";
SetValueString(ns, name, valueString);
}
void CConfigDB::RemoveValue(EConfigNamespace ns, const CStr& name)
{
CHECK_NS(;);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
TConfigMap::iterator it = m_Map[ns].find(name);
if (it == m_Map[ns].end())
return;
m_Map[ns].erase(it);
}
void CConfigDB::SetConfigFile(EConfigNamespace ns, const VfsPath& path)
{
CHECK_NS(;);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
m_ConfigFile[ns] = path;
}
bool CConfigDB::Reload(EConfigNamespace ns)
{
CHECK_NS(false);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
shared_ptr buffer;
size_t buflen;
{
// Handle missing files quietly
if (g_VFS->GetFileInfo(m_ConfigFile[ns], NULL) < 0)
{
LOGMESSAGE("Cannot find config file \"%s\" - ignoring", m_ConfigFile[ns].string8());
return false;
}
LOGMESSAGE("Loading config file \"%s\"", m_ConfigFile[ns].string8());
Status ret = g_VFS->LoadFile(m_ConfigFile[ns], buffer, buflen);
if (ret != INFO::OK)
{
LOGERROR("CConfigDB::Reload(): vfs_load for \"%s\" failed: return was %lld", m_ConfigFile[ns].string8(), (long long)ret);
return false;
}
}
TConfigMap newMap;
char *filebuf = (char*)buffer.get();
char *filebufend = filebuf+buflen;
bool quoted = false;
CStr header;
CStr name;
CStr value;
int line = 1;
std::vector values;
for (char* pos = filebuf; pos < filebufend; ++pos)
{
switch (*pos)
{
case '\n':
case ';':
break; // We finished parsing this line
case ' ':
case '\r':
case '\t':
continue; // ignore
case '[':
header.clear();
for (++pos; pos < filebufend && *pos != '\n' && *pos != ']'; ++pos)
header.push_back(*pos);
if (pos == filebufend || *pos == '\n')
{
LOGERROR("Config header with missing close tag encountered on line %d in '%s'", line, m_ConfigFile[ns].string8());
header.clear();
++line;
continue;
}
LOGMESSAGE("Found config header '%s'", header.c_str());
header.push_back('.');
while (++pos < filebufend && *pos != '\n' && *pos != ';')
if (*pos != ' ' && *pos != '\r')
{
LOGERROR("Config settings on the same line as a header on line %d in '%s'", line, m_ConfigFile[ns].string8());
break;
}
while (pos < filebufend && *pos != '\n')
++pos;
++line;
continue;
case '=':
// Parse parameters (comma separated, possibly quoted)
for (++pos; pos < filebufend && *pos != '\n' && *pos != ';'; ++pos)
{
switch (*pos)
{
case '"':
quoted = true;
// parse until not quoted anymore
for (++pos; pos < filebufend && *pos != '\n' && *pos != '"'; ++pos)
{
if (*pos == '\\' && ++pos == filebufend)
{
LOGERROR("Escape character at end of input (line %d in '%s')", line, m_ConfigFile[ns].string8());
break;
}
value.push_back(*pos);
}
if (pos < filebufend && *pos == '"')
quoted = false;
else
--pos; // We should terminate the outer loop too
break;
case ' ':
case '\r':
case '\t':
break; // ignore
case ',':
if (!value.empty())
values.push_back(value);
value.clear();
break;
default:
value.push_back(*pos);
break;
}
}
if (quoted) // We ignore the invalid parameter
LOGERROR("Unmatched quote while parsing config file '%s' on line %d", m_ConfigFile[ns].string8(), line);
else if (!value.empty())
values.push_back(value);
value.clear();
quoted = false;
break; // We are either at the end of the line, or we still have a comment to parse
default:
name.push_back(*pos);
continue;
}
// Consume the rest of the line
while (pos < filebufend && *pos != '\n')
++pos;
// Store the setting
if (!name.empty() && !values.empty())
{
CStr key(header + name);
newMap[key] = values;
if (g_UnloggedEntries.find(key) != g_UnloggedEntries.end())
LOGMESSAGE("Loaded config string \"%s\"", key);
else
{
std::string vals;
for (size_t i = 0; i < newMap[key].size() - 1; ++i)
vals += "\"" + EscapeString(newMap[key][i]) + "\", ";
vals += "\"" + EscapeString(newMap[key][values.size()-1]) + "\"";
LOGMESSAGE("Loaded config string \"%s\" = %s", key, vals);
}
}
else if (!name.empty())
LOGERROR("Encountered config setting '%s' without value while parsing '%s' on line %d", name, m_ConfigFile[ns].string8(), line);
name.clear();
values.clear();
++line;
}
if (!name.empty())
LOGERROR("Config file does not have a new line after the last config setting '%s'", name);
m_Map[ns].swap(newMap);
return true;
}
bool CConfigDB::WriteFile(EConfigNamespace ns) const
{
CHECK_NS(false);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
return WriteFile(ns, m_ConfigFile[ns]);
}
bool CConfigDB::WriteFile(EConfigNamespace ns, const VfsPath& path) const
{
CHECK_NS(false);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
shared_ptr buf;
AllocateAligned(buf, 1*MiB, maxSectorSize);
char* pos = (char*)buf.get();
for (const std::pair& p : m_Map[ns])
{
size_t i;
pos += sprintf(pos, "%s = ", p.first.c_str());
for (i = 0; i < p.second.size() - 1; ++i)
pos += sprintf(pos, "\"%s\", ", EscapeString(p.second[i]).c_str());
pos += sprintf(pos, "\"%s\"\n", EscapeString(p.second[i]).c_str());
}
const size_t len = pos - (char*)buf.get();
Status ret = g_VFS->CreateFile(path, buf, len);
if (ret < 0)
{
LOGERROR("CConfigDB::WriteFile(): CreateFile \"%s\" failed (error: %d)", path.string8(), (int)ret);
return false;
}
return true;
}
bool CConfigDB::WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value)
{
CHECK_NS(false);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
return WriteValueToFile(ns, name, value, m_ConfigFile[ns]);
}
bool CConfigDB::WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path)
{
CHECK_NS(false);
- CScopeLock s(&cfgdb_mutex);
+ std::lock_guard s(cfgdb_mutex);
TConfigMap newMap;
m_Map[ns].swap(newMap);
Reload(ns);
SetValueString(ns, name, value);
bool ret = WriteFile(ns, path);
m_Map[ns].swap(newMap);
return ret;
}
#undef CHECK_NS
Index: ps/trunk/source/ps/ConfigDB.h
===================================================================
--- ps/trunk/source/ps/ConfigDB.h (revision 22343)
+++ ps/trunk/source/ps/ConfigDB.h (revision 22344)
@@ -1,177 +1,175 @@
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. 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 0 A.D. If not, see .
*/
/*
CConfigDB - Load, access and store configuration variables
TDD : http://www.wildfiregames.com/forum/index.php?showtopic=1125
OVERVIEW:
JavaScript: Check this documentation: http://trac.wildfiregames.com/wiki/Exposed_ConfigDB_Functions
*/
#ifndef INCLUDED_CONFIGDB
#define INCLUDED_CONFIGDB
#include "lib/file/vfs/vfs_path.h"
#include "ps/CStr.h"
#include "ps/Singleton.h"
#include