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"); JsonUtils::minimize(savedConf, "vcmi:settings");
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::out | std::ofstream::trunc); 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) JsonNode & SettingsStorage::getNode(std::vector<std::string> path)

View File

@ -976,7 +976,7 @@ void CModHandler::afterLoad()
modSettings["core"] = coreMod.saveLocalData(); modSettings["core"] = coreMod.saveLocalData();
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc); 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) 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) if (node.Vector().size() > 1)
{ {
logGlobal->warnStream() << "Unexpected length of town buildings requirements: " << node.Vector().size(); logGlobal->warn("Unexpected length of town buildings requirements: %d", node.Vector().size());
logGlobal->warnStream() << "Entry contains " << node; logGlobal->warn("Entry contains: ");
logGlobal->warn(node.toJson());
} }
return BuildingID(VLC->modh->identifiers.getIdentifier(requirement.town->getBuildingScope(), node.Vector()[0]).get()); 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): JsonWriter::JsonWriter(std::ostream & output)
out(output) : out(output)
{ {
writeNode(node);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -15,15 +15,15 @@ class JsonWriter
{ {
//prefix for each line (tabulation) //prefix for each line (tabulation)
std::string prefix; std::string prefix;
std::ostream &out; std::ostream & out;
public: public:
template<typename Iterator> template<typename Iterator>
void writeContainer(Iterator begin, Iterator end); void writeContainer(Iterator begin, Iterator end);
void writeEntry(JsonMap::const_iterator entry); void writeEntry(JsonMap::const_iterator entry);
void writeEntry(JsonVector::const_iterator entry); void writeEntry(JsonVector::const_iterator entry);
void writeString(const std::string &string); void writeString(const std::string & string);
void writeNode(const JsonNode &node); void writeNode(const JsonNode & node);
JsonWriter(std::ostream &output, const JsonNode &node); JsonWriter(std::ostream & output);
}; };
//Tiny string class that uses const char* as data for speed, members are private //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; static const JsonNode nullNode;
std::ostream & operator<<(std::ostream &out, const JsonNode &node)
{
JsonWriter writer(out, node);
return out << "\n";
}
JsonNode::JsonNode(JsonType Type): JsonNode::JsonNode(JsonType Type):
type(DATA_NULL) type(DATA_NULL)
{ {
@ -374,6 +367,15 @@ JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer)
return ::resolvePointer(*this, 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 ///JsonUtils
void JsonUtils::parseTypedBonusShort(const JsonVector& source, std::shared_ptr<Bonus> dest) 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::map <std::string, JsonNode> JsonMap;
typedef std::vector <JsonNode> JsonVector; typedef std::vector <JsonNode> JsonVector;
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const JsonNode &node);
struct Bonus; struct Bonus;
class ResourceID; class ResourceID;
@ -112,6 +110,8 @@ public:
JsonNode & operator[](std::string child); JsonNode & operator[](std::string child);
const JsonNode & operator[](std::string child) const; const JsonNode & operator[](std::string child) const;
std::string toJson() const;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
h & meta; h & meta;

View File

@ -13,6 +13,7 @@
#include "../filesystem/CInputStream.h" #include "../filesystem/CInputStream.h"
#include "../filesystem/COutputStream.h" #include "../filesystem/COutputStream.h"
#include "../JsonDetail.h"
#include "CMap.h" #include "CMap.h"
#include "../CModHandler.h" #include "../CModHandler.h"
#include "../CCreatureHandler.h" #include "../CCreatureHandler.h"
@ -1054,15 +1055,13 @@ CMapLoaderJson::MapObjectLoader::MapObjectLoader(CMapLoaderJson * _owner, JsonMa
void CMapLoaderJson::MapObjectLoader::construct() void CMapLoaderJson::MapObjectLoader::construct()
{ {
//logGlobal->debugStream() <<"Loading: " <<jsonKey;
//TODO:consider move to ObjectTypeHandler //TODO:consider move to ObjectTypeHandler
//find type handler //find type handler
std::string typeName = configuration["type"].String(), subtypeName = configuration["subtype"].String(); std::string typeName = configuration["type"].String(), subtypeName = configuration["subtype"].String();
if(typeName.empty()) if(typeName.empty())
{ {
logGlobal->error("Object type missing"); logGlobal->error("Object type missing");
logGlobal->debugStream() << configuration; logGlobal->debug(configuration.toJson());
return; return;
} }
@ -1082,7 +1081,7 @@ void CMapLoaderJson::MapObjectLoader::construct()
else if(subtypeName.empty()) else if(subtypeName.empty())
{ {
logGlobal->error("Object subtype missing"); logGlobal->error("Object subtype missing");
logGlobal->debugStream() << configuration; logGlobal->debug(configuration.toJson());
return; return;
} }
@ -1189,7 +1188,8 @@ CMapSaverJson::~CMapSaverJson()
void CMapSaverJson::addToArchive(const JsonNode & data, const std::string & filename) void CMapSaverJson::addToArchive(const JsonNode & data, const std::string & filename)
{ {
std::ostringstream out; std::ostringstream out;
out << data; JsonWriter writer(out);
writer.writeNode(data);
out.flush(); 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); boost::unique_lock<boost::recursive_mutex> lock(gsm);
logGlobal->trace("Player %s attempts answering query %d with answer:", player, qid); logGlobal->trace("Player %s attempts answering query %d with answer:", player, qid);
logGlobal->traceStream() << answer; logGlobal->trace(answer.toJson());
auto topQuery = queries.topQuery(player); auto topQuery = queries.topQuery(player);
COMPLAIN_RET_FALSE_IF(!topQuery, "This player doesn't have any queries!"); COMPLAIN_RET_FALSE_IF(!topQuery, "This player doesn't have any queries!");

View File

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