Skip to content

Commit

Permalink
Add deadline to fc::json::to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Jan 3, 2020
1 parent 7fc8c2e commit e7aea1c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 60 deletions.
21 changes: 11 additions & 10 deletions include/fc/io/json.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <fc/variant.hpp>
#include <fc/filesystem.hpp>
#include <fc/time.hpp>

#define DEFAULT_MAX_RECURSION_DEPTH 200

Expand Down Expand Up @@ -29,15 +30,15 @@ namespace fc
legacy_generator = 1
};

static ostream& to_stream( ostream& out, const fc::string&);
static ostream& to_stream( ostream& out, const variant& v, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const variants& v, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const variant_object& v, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const fc::string&, const fc::time_point& deadline );
static ostream& to_stream( ostream& out, const variant& v, const fc::time_point& deadline, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const variants& v, const fc::time_point& deadline, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const variant_object& v, const fc::time_point& deadline, output_formatting format = stringify_large_ints_and_doubles );

static variant from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static variants variants_from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static string to_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles );
static string to_pretty_string( const variant& v, output_formatting format = stringify_large_ints_and_doubles );
static string to_string( const variant& v, const fc::time_point& deadline, output_formatting format = stringify_large_ints_and_doubles );
static string to_pretty_string( const variant& v, const fc::time_point& deadline, output_formatting format = stringify_large_ints_and_doubles );

static bool is_valid( const std::string& json_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );

Expand All @@ -57,15 +58,15 @@ namespace fc
}

template<typename T>
static string to_string( const T& v, output_formatting format = stringify_large_ints_and_doubles )
static string to_string( const T& v, const fc::time_point& deadline, output_formatting format = stringify_large_ints_and_doubles )
{
return to_string( variant(v), format );
return to_string( variant(v), deadline, format );
}

template<typename T>
static string to_pretty_string( const T& v, output_formatting format = stringify_large_ints_and_doubles )
static string to_pretty_string( const T& v, const fc::time_point& deadline = fc::time_point::maximum(), output_formatting format = stringify_large_ints_and_doubles )
{
return to_pretty_string( variant(v), format );
return to_pretty_string( variant(v), deadline, format );
}

template<typename T>
Expand Down
13 changes: 11 additions & 2 deletions src/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ namespace fc
*/
string exception::to_detail_string( log_level ll )const
{
const auto deadline = fc::time_point::now() + fc::milliseconds(10);
std::stringstream ss;
try {
try {
Expand All @@ -170,11 +171,14 @@ namespace fc
for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ) {
try {
ss << itr->get_message() << "\n"; //fc::format_string( itr->get_format(), itr->get_data() ) <<"\n";
ss << " " << json::to_string( itr->get_data()) << "\n";
ss << " " << json::to_string( itr->get_data(), deadline ) << "\n";
ss << " " << itr->get_context().to_string();
++itr;
} catch( std::bad_alloc& ) {
throw;
} catch( const fc::timeout_exception& e) {
ss << "<- timeout exception in to_detail_string: " << e.to_string();
break;
} catch( ... ) {
ss << "<- exception in to_detail_string.";
}
Expand All @@ -193,6 +197,7 @@ namespace fc
*/
string exception::to_string( log_level ll )const
{
const auto deadline = fc::time_point::now() + fc::milliseconds(10);
std::stringstream ss;
try {
ss << my->_what;
Expand All @@ -205,10 +210,14 @@ namespace fc
}
for( auto itr = my->_elog.begin(); itr != my->_elog.end(); ++itr ) {
try {
ss << fc::format_string( itr->get_format(), itr->get_data()) << "\n";
FC_CHECK_DEADLINE(deadline);
ss << fc::format_string( itr->get_format(), itr->get_data(), true) << "\n";
// ss << " " << itr->get_context().to_string() <<"\n";
} catch( std::bad_alloc& ) {
throw;
} catch( const fc::timeout_exception& e) {
ss << "<- timeout exception in to_string: " << e.to_detail_string();
break;
} catch( ... ) {
ss << "<- exception in to_string.\n";
}
Expand Down
99 changes: 55 additions & 44 deletions src/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ namespace fc
template<typename T, json::parse_type parser_type> variants arrayFromStream( T& in, uint32_t max_depth );
template<typename T, json::parse_type parser_type> variant number_from_stream( T& in );
template<typename T> variant token_from_stream( T& in );
void escape_string( const std::string& str, std::ostream& os );
template<typename T> void to_stream( T& os, const variants& a, json::output_formatting format );
template<typename T> void to_stream( T& os, const variant_object& o, json::output_formatting format );
template<typename T> void to_stream( T& os, const variant& v, json::output_formatting format );
template<typename T> void to_stream( T& os, const variants& a, const fc::time_point& deadline, json::output_formatting format );
template<typename T> void to_stream( T& os, const variant_object& o, const fc::time_point& deadline, json::output_formatting format );
template<typename T> void to_stream( T& os, const variant& v, const fc::time_point& deadline, json::output_formatting format );
std::string pretty_print( const std::string& v, uint8_t indent );
}

Expand Down Expand Up @@ -491,11 +490,13 @@ namespace fc
*
* All other characters are printed as UTF8.
*/
void escape_string( const string& str, std::ostream& os )
void escape_string( const string& str, std::ostream& os, const fc::time_point& deadline )
{
os << '"';
for( auto itr = str.begin(); itr != str.end(); ++itr )
size_t i = 0;
for( auto itr = str.begin(); itr != str.end(); ++i,++itr )
{
if( i % 1024 == 0 ) FC_CHECK_DEADLINE(deadline);
switch( *itr )
{
case '\b': // \x08
Expand Down Expand Up @@ -560,38 +561,24 @@ namespace fc
}
os << '"';
}
std::ostream& json::to_stream( std::ostream& out, const std::string& str )
std::ostream& json::to_stream( std::ostream& out, const std::string& str, const fc::time_point& deadline )
{
escape_string( str, out );
escape_string( str, out, deadline );
return out;
}

template<typename T>
void to_stream( T& os, const variants& a, json::output_formatting format )
void to_stream( T& os, const variant_object& o, const fc::time_point& deadline, json::output_formatting format )
{
os << '[';
auto itr = a.begin();

while( itr != a.end() )
{
to_stream( os, *itr, format );
++itr;
if( itr != a.end() )
os << ',';
}
os << ']';
}
template<typename T>
void to_stream( T& os, const variant_object& o, json::output_formatting format )
{
os << '{';
FC_CHECK_DEADLINE(deadline);
os << '{';
auto itr = o.begin();

while( itr != o.end() )
{
escape_string( itr->key(), os );
escape_string( itr->key(), os, deadline );
os << ':';
to_stream( os, itr->value(), format );
to_stream( os, itr->value(), deadline, format );
++itr;
if( itr != o.end() )
os << ',';
Expand All @@ -600,8 +587,26 @@ namespace fc
}

template<typename T>
void to_stream( T& os, const variant& v, json::output_formatting format )
void to_stream( T& os, const variants& a, const fc::time_point& deadline, json::output_formatting format )
{
FC_CHECK_DEADLINE(deadline);
os << '[';
auto itr = a.begin();

while( itr != a.end() )
{
to_stream( os, *itr, deadline, format );
++itr;
if( itr != a.end() )
os << ',';
}
os << ']';
}

template<typename T>
void to_stream( T& os, const variant& v, const fc::time_point& deadline, json::output_formatting format )
{
FC_CHECK_DEADLINE(deadline);
switch( v.get_type() )
{
case variant::null_type:
Expand Down Expand Up @@ -639,32 +644,33 @@ namespace fc
os << v.as_string();
return;
case variant::string_type:
escape_string( v.get_string(), os );
escape_string( v.get_string(), os, deadline );
return;
case variant::blob_type:
escape_string( v.as_string(), os );
escape_string( v.as_string(), os, deadline );
return;
case variant::array_type:
{
const variants& a = v.get_array();
to_stream( os, a, format );
to_stream( os, a, deadline, format );
return;
}
case variant::object_type:
{
const variant_object& o = v.get_object();
to_stream(os, o, format );
to_stream(os, o, deadline, format );
return;
}
default:
FC_THROW_EXCEPTION( fc::invalid_arg_exception, "Unsupported variant type: " + std::to_string( v.get_type() ) );
}
}

std::string json::to_string( const variant& v, output_formatting format )
std::string json::to_string( const variant& v, const fc::time_point& deadline, output_formatting format )
{
std::stringstream ss;
fc::to_stream( ss, v, format );
fc::to_stream( ss, v, deadline, format );
FC_CHECK_DEADLINE(deadline);
return ss.str();
}

Expand Down Expand Up @@ -764,21 +770,23 @@ namespace fc



std::string json::to_pretty_string( const variant& v, output_formatting format )
std::string json::to_pretty_string( const variant& v, const fc::time_point& deadline, output_formatting format )
{
return pretty_print(to_string(v, format), 2);
auto s = to_string(v, deadline, format);
FC_CHECK_DEADLINE(deadline);
return pretty_print( std::move( s ), 2);
}

bool json::save_to_file( const variant& v, const fc::path& fi, bool pretty, output_formatting format )
{
if( pretty ) {
auto str = json::to_pretty_string( v, format );
auto str = json::to_pretty_string( v, fc::time_point::maximum(), format );
std::ofstream o(fi.generic_string().c_str());
o.write( str.c_str(), str.size() );
return o.good();
} else {
std::ofstream o(fi.generic_string().c_str());
fc::to_stream( o, v, format );
fc::to_stream( o, v, fc::time_point::maximum(), format );
return o.good();
}
}
Expand Down Expand Up @@ -821,19 +829,22 @@ namespace fc
}
*/

std::ostream& json::to_stream( std::ostream& out, const variant& v, output_formatting format )
std::ostream& json::to_stream( std::ostream& out, const variant& v, const fc::time_point& deadline, output_formatting format )
{
fc::to_stream( out, v, format );
FC_CHECK_DEADLINE(deadline);
fc::to_stream( out, v, deadline, format );
return out;
}
std::ostream& json::to_stream( std::ostream& out, const variants& v, output_formatting format )
std::ostream& json::to_stream( std::ostream& out, const variants& v, const fc::time_point& deadline, output_formatting format )
{
fc::to_stream( out, v, format );
FC_CHECK_DEADLINE(deadline);
fc::to_stream( out, v, deadline, format );
return out;
}
std::ostream& json::to_stream( std::ostream& out, const variant_object& v, output_formatting format )
std::ostream& json::to_stream( std::ostream& out, const variant_object& v, const fc::time_point& deadline, output_formatting format )
{
fc::to_stream( out, v, format );
FC_CHECK_DEADLINE(deadline);
fc::to_stream( out, v, deadline, format );
return out;
}

Expand Down
4 changes: 2 additions & 2 deletions src/log/gelf_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace fc
mutable_variant_object gelf_message;
gelf_message["version"] = "1.1";
gelf_message["host"] = my->cfg.host;
gelf_message["short_message"] = format_string(message.get_format(), message.get_data());
gelf_message["short_message"] = format_string(message.get_format(), message.get_data(), true);

// use now() instead of context.get_timestamp() because log_message construction can include user provided long running calls
const auto time_ns = time_point::now().time_since_epoch().count();
Expand Down Expand Up @@ -148,7 +148,7 @@ namespace fc
if (!context.get_task_name().empty())
gelf_message["_task_name"] = context.get_task_name();

string gelf_message_as_string = json::to_string(gelf_message, json::legacy_generator); // GELF 1.1 specifies unstringified numbers
string gelf_message_as_string = json::to_string(gelf_message, fc::time_point::now() + fc::milliseconds(10), json::legacy_generator); // GELF 1.1 specifies unstringified numbers
//unsigned uncompressed_size = gelf_message_as_string.size();
gelf_message_as_string = zlib_compress(gelf_message_as_string);

Expand Down
2 changes: 1 addition & 1 deletion src/network/http/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class http_client_impl {
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req.set(http::field::content_type, "application/json");
req.keep_alive(true);
req.body() = json::to_string(payload);
req.body() = json::to_string(payload, _deadline);
req.prepare_payload();

auto conn_iter = get_connection(dest, deadline);
Expand Down
2 changes: 1 addition & 1 deletion src/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ string format_string( const string& frmt, const variant_object& args, bool minim
if( minimize ) {
replaced = false;
} else {
result += json::to_string( val->value() );
result += json::to_string( val->value(), fc::time_point::maximum() );
}
} else if( val->value().is_blob() ) {
if( minimize && val->value().get_blob().data.size() > minimize_sub_max_size ) {
Expand Down

0 comments on commit e7aea1c

Please sign in to comment.