debug_api: Add API call to stream JSON objects to file

This commit is contained in:
theoreticalbts 2016-03-15 00:38:55 -04:00
parent c37c2d4543
commit a7c88be7c7
6 changed files with 120 additions and 5 deletions

View file

@ -12,6 +12,7 @@
#include <graphene/utilities/key_conversion.hpp> #include <graphene/utilities/key_conversion.hpp>
#include <graphene/debug_witness/debug_api.hpp> #include <graphene/debug_witness/debug_api.hpp>
#include <graphene/debug_witness/debug_witness.hpp>
namespace graphene { namespace debug_witness { namespace graphene { namespace debug_witness {
@ -26,6 +27,9 @@ class debug_api_impl
void debug_generate_blocks( const std::string& debug_key, uint32_t count ); void debug_generate_blocks( const std::string& debug_key, uint32_t count );
void debug_update_object( const fc::variant_object& update ); void debug_update_object( const fc::variant_object& update );
//void debug_save_db( std::string db_path ); //void debug_save_db( std::string db_path );
void debug_stream_json_objects( const std::string& filename );
void debug_stream_json_objects_flush();
std::shared_ptr< graphene::debug_witness_plugin::debug_witness_plugin > get_plugin();
graphene::app::application& app; graphene::app::application& app;
}; };
@ -102,6 +106,21 @@ void debug_api_impl::debug_update_object( const fc::variant_object& update )
db->debug_update( update ); db->debug_update( update );
} }
std::shared_ptr< graphene::debug_witness_plugin::debug_witness_plugin > debug_api_impl::get_plugin()
{
return app.get_plugin< graphene::debug_witness_plugin::debug_witness_plugin >( "debug_witness" );
}
void debug_api_impl::debug_stream_json_objects( const std::string& filename )
{
get_plugin()->set_json_object_stream( filename );
}
void debug_api_impl::debug_stream_json_objects_flush()
{
get_plugin()->flush_json_object_stream();
}
} // detail } // detail
debug_api::debug_api( graphene::app::application& app ) debug_api::debug_api( graphene::app::application& app )
@ -124,4 +143,15 @@ void debug_api::debug_update_object( fc::variant_object update )
my->debug_update_object( update ); my->debug_update_object( update );
} }
void debug_api::debug_stream_json_objects( std::string filename )
{
my->debug_stream_json_objects( filename );
}
void debug_api::debug_stream_json_objects_flush()
{
my->debug_stream_json_objects_flush();
}
} } // graphene::debug_witness } } // graphene::debug_witness

View file

@ -98,29 +98,76 @@ void debug_witness_plugin::plugin_startup()
// connect needed signals // connect needed signals
db.applied_block.connect([this](const graphene::chain::signed_block& b){ on_applied_block(b); }); _applied_block_conn = db.applied_block.connect([this](const graphene::chain::signed_block& b){ on_applied_block(b); });
db.changed_objects.connect([this](const std::vector<graphene::db::object_id_type>& ids){ on_changed_objects(ids); }); _changed_objects_conn = db.changed_objects.connect([this](const std::vector<graphene::db::object_id_type>& ids){ on_changed_objects(ids); });
db.removed_objects.connect([this](const std::vector<const graphene::db::object*>& objs){ on_removed_objects(objs); }); _removed_objects_conn = db.removed_objects.connect([this](const std::vector<const graphene::db::object*>& objs){ on_removed_objects(objs); });
return; return;
} }
void debug_witness_plugin::on_changed_objects( const std::vector<graphene::db::object_id_type>& ids ) void debug_witness_plugin::on_changed_objects( const std::vector<graphene::db::object_id_type>& ids )
{ {
if( _json_object_stream && (ids.size() > 0) )
{
const chain::database& db = database();
for( const graphene::db::object_id_type& oid : ids )
{
const graphene::db::object* obj = db.find_object( oid );
if( obj == nullptr )
{
(*_json_object_stream) << "{\"id\":" << fc::json::to_string( oid ) << "}\n";
}
else
{
(*_json_object_stream) << fc::json::to_string( obj->to_variant() ) << '\n';
}
}
}
} }
void debug_witness_plugin::on_removed_objects( const std::vector<const graphene::db::object*> objs ) void debug_witness_plugin::on_removed_objects( const std::vector<const graphene::db::object*> objs )
{ {
/*
if( _json_object_stream )
{
for( const graphene::db::object* obj : objs )
{
(*_json_object_stream) << "{\"id\":" << fc::json::to_string( obj->id ) << "}\n";
}
}
*/
} }
void debug_witness_plugin::on_applied_block( const graphene::chain::signed_block& b ) void debug_witness_plugin::on_applied_block( const graphene::chain::signed_block& b )
{ {
if( _json_object_stream )
{
(*_json_object_stream) << "{\"bn\":" << fc::to_string( b.block_num() ) << "}\n";
}
}
void debug_witness_plugin::set_json_object_stream( const std::string& filename )
{
if( _json_object_stream )
{
_json_object_stream->close();
_json_object_stream.reset();
}
_json_object_stream = std::make_shared< std::ofstream >( filename );
}
void debug_witness_plugin::flush_json_object_stream()
{
if( _json_object_stream )
_json_object_stream->flush();
} }
void debug_witness_plugin::plugin_shutdown() void debug_witness_plugin::plugin_shutdown()
{ {
if( _json_object_stream )
{
_json_object_stream->close();
_json_object_stream.reset();
}
return; return;
} }

View file

@ -71,6 +71,17 @@ class debug_api
// not implemented // not implemented
//void save_db( std::string db_path ); //void save_db( std::string db_path );
/**
* Stream objects to file. (Hint: Create with mkfifo and pipe it to a script)
*/
void debug_stream_json_objects( std::string filename );
/**
* Flush streaming file.
*/
void debug_stream_json_objects_flush();
std::shared_ptr< detail::debug_api_impl > my; std::shared_ptr< detail::debug_api_impl > my;
}; };
@ -80,4 +91,6 @@ FC_API(graphene::debug_witness::debug_api,
(debug_push_blocks) (debug_push_blocks)
(debug_generate_blocks) (debug_generate_blocks)
(debug_update_object) (debug_update_object)
(debug_stream_json_objects)
(debug_stream_json_objects_flush)
) )

View file

@ -45,6 +45,9 @@ public:
virtual void plugin_startup() override; virtual void plugin_startup() override;
virtual void plugin_shutdown() override; virtual void plugin_shutdown() override;
void set_json_object_stream( const std::string& filename );
void flush_json_object_stream();
private: private:
void on_changed_objects( const std::vector<graphene::db::object_id_type>& ids ); void on_changed_objects( const std::vector<graphene::db::object_id_type>& ids );
@ -54,6 +57,11 @@ private:
boost::program_options::variables_map _options; boost::program_options::variables_map _options;
std::map<chain::public_key_type, fc::ecc::private_key> _private_keys; std::map<chain::public_key_type, fc::ecc::private_key> _private_keys;
std::shared_ptr< std::ofstream > _json_object_stream;
boost::signals2::scoped_connection _applied_block_conn;
boost::signals2::scoped_connection _changed_objects_conn;
boost::signals2::scoped_connection _removed_objects_conn;
}; };
} } //graphene::debug_witness_plugin } } //graphene::debug_witness_plugin

View file

@ -1406,6 +1406,7 @@ class wallet_api
void dbg_make_mia(string creator, string symbol); void dbg_make_mia(string creator, string symbol);
void dbg_push_blocks( std::string src_filename, uint32_t count ); void dbg_push_blocks( std::string src_filename, uint32_t count );
void dbg_generate_blocks( std::string debug_wif_key, uint32_t count ); void dbg_generate_blocks( std::string debug_wif_key, uint32_t count );
void dbg_stream_json_objects( const std::string& filename );
void dbg_update_object( fc::variant_object update ); void dbg_update_object( fc::variant_object update );
void flood_network(string prefix, uint32_t number_of_transactions); void flood_network(string prefix, uint32_t number_of_transactions);
@ -1580,6 +1581,7 @@ FC_API( graphene::wallet::wallet_api,
(dbg_make_mia) (dbg_make_mia)
(dbg_push_blocks) (dbg_push_blocks)
(dbg_generate_blocks) (dbg_generate_blocks)
(dbg_stream_json_objects)
(dbg_update_object) (dbg_update_object)
(flood_network) (flood_network)
(network_add_nodes) (network_add_nodes)

View file

@ -2343,18 +2343,28 @@ public:
{ {
use_debug_api(); use_debug_api();
(*_remote_debug)->debug_push_blocks( src_filename, count ); (*_remote_debug)->debug_push_blocks( src_filename, count );
(*_remote_debug)->debug_stream_json_objects_flush();
} }
void dbg_generate_blocks( const std::string& debug_wif_key, uint32_t count ) void dbg_generate_blocks( const std::string& debug_wif_key, uint32_t count )
{ {
use_debug_api(); use_debug_api();
(*_remote_debug)->debug_generate_blocks( debug_wif_key, count ); (*_remote_debug)->debug_generate_blocks( debug_wif_key, count );
(*_remote_debug)->debug_stream_json_objects_flush();
}
void dbg_stream_json_objects( const std::string& filename )
{
use_debug_api();
(*_remote_debug)->debug_stream_json_objects( filename );
(*_remote_debug)->debug_stream_json_objects_flush();
} }
void dbg_update_object( const fc::variant_object& update ) void dbg_update_object( const fc::variant_object& update )
{ {
use_debug_api(); use_debug_api();
(*_remote_debug)->debug_update_object( update ); (*_remote_debug)->debug_update_object( update );
(*_remote_debug)->debug_stream_json_objects_flush();
} }
void use_network_node_api() void use_network_node_api()
@ -3264,6 +3274,11 @@ void wallet_api::dbg_generate_blocks( std::string debug_wif_key, uint32_t count
my->dbg_generate_blocks( debug_wif_key, count ); my->dbg_generate_blocks( debug_wif_key, count );
} }
void wallet_api::dbg_stream_json_objects( const std::string& filename )
{
my->dbg_stream_json_objects( filename );
}
void wallet_api::dbg_update_object( fc::variant_object update ) void wallet_api::dbg_update_object( fc::variant_object update )
{ {
my->dbg_update_object( update ); my->dbg_update_object( update );