1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Prepared JsonNode for new logging API.

This commit is contained in:
AlexVinS 2017-08-11 16:27:42 +03:00
parent 64d9dadd64
commit 046e1a7c29
10 changed files with 34 additions and 36 deletions

View File

@ -81,7 +81,7 @@ void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath
JsonUtils::minimize(savedConf, "vcmi:settings");
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::out | std::ofstream::trunc);
file << savedConf;
file << savedConf.toJson();
}
JsonNode & SettingsStorage::getNode(std::vector<std::string> path)

View File

@ -976,7 +976,7 @@ void CModHandler::afterLoad()
modSettings["core"] = coreMod.saveLocalData();
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
file << modSettings;
file << modSettings.toJson();
}
std::string CModHandler::normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier)

View File

@ -815,8 +815,9 @@ void CTownHandler::initializeRequirements()
{
if (node.Vector().size() > 1)
{
logGlobal->warnStream() << "Unexpected length of town buildings requirements: " << node.Vector().size();
logGlobal->warnStream() << "Entry contains " << node;
logGlobal->warn("Unexpected length of town buildings requirements: %d", node.Vector().size());
logGlobal->warn("Entry contains: ");
logGlobal->warn(node.toJson());
}
return BuildingID(VLC->modh->identifiers.getIdentifier(requirement.town->getBuildingScope(), node.Vector()[0]).get());
});

View File

@ -125,10 +125,9 @@ void JsonWriter::writeNode(const JsonNode &node)
}
}
JsonWriter::JsonWriter(std::ostream &output, const JsonNode &node):
out(output)
JsonWriter::JsonWriter(std::ostream & output)
: out(output)
{
writeNode(node);
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -15,15 +15,15 @@ class JsonWriter
{
//prefix for each line (tabulation)
std::string prefix;
std::ostream &out;
std::ostream & out;
public:
template<typename Iterator>
void writeContainer(Iterator begin, Iterator end);
void writeEntry(JsonMap::const_iterator entry);
void writeEntry(JsonVector::const_iterator entry);
void writeString(const std::string &string);
void writeNode(const JsonNode &node);
JsonWriter(std::ostream &output, const JsonNode &node);
void writeString(const std::string & string);
void writeNode(const JsonNode & node);
JsonWriter(std::ostream & output);
};
//Tiny string class that uses const char* as data for speed, members are private

View File

@ -27,13 +27,6 @@ class CModHandler;
static const JsonNode nullNode;
std::ostream & operator<<(std::ostream &out, const JsonNode &node)
{
JsonWriter writer(out, node);
return out << "\n";
}
JsonNode::JsonNode(JsonType Type):
type(DATA_NULL)
{
@ -374,6 +367,15 @@ JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer)
return ::resolvePointer(*this, jsonPointer);
}
std::string JsonNode::toJson() const
{
std::ostringstream out;
JsonWriter writer(out);
writer.writeNode(*this);
out << "\n";
return out.str();
}
///JsonUtils
void JsonUtils::parseTypedBonusShort(const JsonVector& source, std::shared_ptr<Bonus> dest)

View File

@ -13,8 +13,6 @@ class JsonNode;
typedef std::map <std::string, JsonNode> JsonMap;
typedef std::vector <JsonNode> JsonVector;
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const JsonNode &node);
struct Bonus;
class ResourceID;
@ -112,6 +110,8 @@ public:
JsonNode & operator[](std::string child);
const JsonNode & operator[](std::string child) const;
std::string toJson() const;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & meta;

View File

@ -13,6 +13,7 @@
#include "../filesystem/CInputStream.h"
#include "../filesystem/COutputStream.h"
#include "../JsonDetail.h"
#include "CMap.h"
#include "../CModHandler.h"
#include "../CCreatureHandler.h"
@ -1054,15 +1055,13 @@ CMapLoaderJson::MapObjectLoader::MapObjectLoader(CMapLoaderJson * _owner, JsonMa
void CMapLoaderJson::MapObjectLoader::construct()
{
//logGlobal->debugStream() <<"Loading: " <<jsonKey;
//TODO:consider move to ObjectTypeHandler
//find type handler
std::string typeName = configuration["type"].String(), subtypeName = configuration["subtype"].String();
if(typeName.empty())
{
logGlobal->error("Object type missing");
logGlobal->debugStream() << configuration;
logGlobal->debug(configuration.toJson());
return;
}
@ -1082,7 +1081,7 @@ void CMapLoaderJson::MapObjectLoader::construct()
else if(subtypeName.empty())
{
logGlobal->error("Object subtype missing");
logGlobal->debugStream() << configuration;
logGlobal->debug(configuration.toJson());
return;
}
@ -1189,7 +1188,8 @@ CMapSaverJson::~CMapSaverJson()
void CMapSaverJson::addToArchive(const JsonNode & data, const std::string & filename)
{
std::ostringstream out;
out << data;
JsonWriter writer(out);
writer.writeNode(data);
out.flush();
{

View File

@ -3701,7 +3701,7 @@ bool CGameHandler::queryReply(QueryID qid, const JsonNode & answer, PlayerColor
boost::unique_lock<boost::recursive_mutex> lock(gsm);
logGlobal->trace("Player %s attempts answering query %d with answer:", player, qid);
logGlobal->traceStream() << answer;
logGlobal->trace(answer.toJson());
auto topQuery = queries.topQuery(player);
COMPLAIN_RET_FALSE_IF(!topQuery, "This player doesn't have any queries!");

View File

@ -9,6 +9,8 @@
*/
#include "StdInc.h"
#include "../lib/JsonDetail.h"
#include "../lib/filesystem/CMemoryBuffer.h"
#include "../lib/filesystem/Filesystem.h"
@ -92,17 +94,11 @@ static JsonNode getFromArchive(CZipLoader & archive, const std::string & archive
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
{
std::ostringstream out;
out << data;
out.flush();
auto s = data.toJson();
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
{
auto s = out.str();
std::unique_ptr<COutputStream> stream = saver.addFile(filename);
if(stream->write((const ui8*)s.c_str(), s.size()) != s.size())
throw new std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
}
if(stream->write((const ui8*)s.c_str(), s.size()) != s.size())
throw new std::runtime_error("CMapSaverJson::saveHeader() zip compression failed.");
}
static std::unique_ptr<CMap> loadOriginal(const JsonNode & header, const JsonNode & objects, const JsonNode & surface, const JsonNode & underground)