From: Aki Tuomi Date: Mon, 1 Jul 2013 06:31:36 +0000 (+0300) Subject: Allow logging of non-string values. X-Git-Tag: rec-3.6.0-rc1~609^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F887%2Fhead;p=thirdparty%2Fpdns.git Allow logging of non-string values. --- diff --git a/modules/remotebackend/remotebackend.cc b/modules/remotebackend/remotebackend.cc index 0a2b8defc2..f9f4e75595 100644 --- a/modules/remotebackend/remotebackend.cc +++ b/modules/remotebackend/remotebackend.cc @@ -29,13 +29,13 @@ bool Connector::recv(rapidjson::Document &value) { rv = false; } if (value.HasMember("log")) { - const rapidjson::Value& messages = value["log"]; + rapidjson::Value& messages = value["log"]; if (messages.IsArray()) { // log em all - for (rapidjson::Value::ConstValueIterator iter = messages.Begin(); iter != messages.End(); ++iter) - L<GetString() <(value.GetInt64()); + if (value.IsInt()) return boost::lexical_cast(value.GetInt()); + if (value.IsDouble()) return boost::lexical_cast(value.GetDouble()); + return "(unpresentable value)"; // cannot convert into presentation format +} + int RemoteBackend::getInt(rapidjson::Value &value) { - if (value.IsBool()) return (value.GetBool() ? 1 : 0); if (value.IsInt()) return value.GetInt(); + if (value.IsBool()) return (value.GetBool() ? 1 : 0); + if (value.IsUint()) return static_cast(value.GetUint()); if (value.IsDouble()) return static_cast(value.GetDouble()); if (value.IsString()) { // accepts 0, 1, false, true std::string tmp = value.GetString(); @@ -808,8 +818,9 @@ int RemoteBackend::getInt(rapidjson::Value &value) { } unsigned int RemoteBackend::getUInt(rapidjson::Value &value) { + if (value.IsUint()) return value.GetUint(); if (value.IsBool()) return (value.GetBool() ? 1 : 0); - if (value.IsInt()) return value.GetUint(); + if (value.IsInt()) return static_cast(value.GetInt()); if (value.IsDouble()) return static_cast(value.GetDouble()); if (value.IsString()) { // accepts 0, 1, false, true std::string tmp = value.GetString(); @@ -819,9 +830,9 @@ unsigned int RemoteBackend::getUInt(rapidjson::Value &value) { } int64_t RemoteBackend::getInt64(rapidjson::Value &value) { + if (value.IsInt64()) return value.GetInt64(); if (value.IsBool()) return (value.GetBool() ? 1 : 0); if (value.IsInt()) return value.GetInt(); - if (value.IsInt64()) return value.GetInt64(); if (value.IsDouble()) return static_cast(value.GetDouble()); if (value.IsString()) { // accepts 0, 1, false, true std::string tmp = value.GetString(); @@ -831,17 +842,19 @@ int64_t RemoteBackend::getInt64(rapidjson::Value &value) { } std::string RemoteBackend::getString(rapidjson::Value &value) { + if (value.IsString()) return value.GetString(); if (value.IsBool()) return (value.GetBool() ? "true" : "false"); + if (value.IsInt64()) return boost::lexical_cast(value.GetInt64()); if (value.IsInt()) return boost::lexical_cast(value.GetInt()); if (value.IsDouble()) return boost::lexical_cast(value.GetDouble()); - if (value.IsString()) return value.GetString(); throw new AhuException("Cannot convert rapidjson value into std::string"); } double RemoteBackend::getDouble(rapidjson::Value &value) { + if (value.IsDouble()) return value.GetDouble(); if (value.IsBool()) return (value.GetBool() ? 1.0L : 0.0L); + if (value.IsInt64()) return static_cast(value.GetInt64()); if (value.IsInt()) return static_cast(value.GetInt()); - if (value.IsDouble()) return value.GetDouble(); if (value.IsString()) { // accepts 0, 1, false, true std::string tmp = value.GetString(); return boost::lexical_cast(tmp); diff --git a/modules/remotebackend/remotebackend.hh b/modules/remotebackend/remotebackend.hh index 71f95af7c7..18b6a97ab4 100644 --- a/modules/remotebackend/remotebackend.hh +++ b/modules/remotebackend/remotebackend.hh @@ -30,7 +30,9 @@ class Connector { bool recv(rapidjson::Document &value); virtual int send_message(const rapidjson::Document &input) = 0; virtual int recv_message(rapidjson::Document &output) = 0; + protected: bool getBool(rapidjson::Value &value); + std::string getString(rapidjson::Value &value); }; // fwd declarations