forked from lthn/blockchain
dump as json now supports different line endings + fixed logging for main_window
This commit is contained in:
parent
a13a5b270a
commit
408fab65a4
5 changed files with 69 additions and 44 deletions
|
|
@ -83,7 +83,7 @@ namespace epee
|
|||
bool load_from_binary(const binarybuffer& target);
|
||||
template<class trace_policy>
|
||||
bool dump_as_xml(std::string& targetObj, const std::string& root_name = "");
|
||||
bool dump_as_json(std::string& targetObj, size_t indent = 0);
|
||||
bool dump_as_json(std::string& targetObj, size_t indent = 0, end_of_line_t eol = eol_crlf);
|
||||
bool load_from_json(const std::string& source);
|
||||
|
||||
private:
|
||||
|
|
@ -106,11 +106,11 @@ namespace epee
|
|||
#pragma pack(pop)
|
||||
};
|
||||
inline
|
||||
bool portable_storage::dump_as_json(std::string& buff, size_t indent)
|
||||
bool portable_storage::dump_as_json(std::string& buff, size_t indent /* = 0 */, end_of_line_t eol /* = eol_crlf */)
|
||||
{
|
||||
TRY_ENTRY();
|
||||
std::stringstream ss;
|
||||
epee::serialization::dump_as_json(ss, m_root, indent);
|
||||
epee::serialization::dump_as_json(ss, m_root, indent, eol);
|
||||
buff = ss.str();
|
||||
return true;
|
||||
CATCH_ENTRY("portable_storage::dump_as_json", false)
|
||||
|
|
|
|||
|
|
@ -69,6 +69,8 @@ namespace epee
|
|||
{
|
||||
namespace serialization
|
||||
{
|
||||
enum end_of_line_t { eol_crlf = 0, eol_lf = 1, eol_cr = 2, eol_space = 3 };
|
||||
|
||||
struct section;
|
||||
|
||||
/************************************************************************/
|
||||
|
|
|
|||
|
|
@ -56,16 +56,16 @@ namespace epee
|
|||
}
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
template<class t_struct>
|
||||
bool store_t_to_json(const t_struct& str_in, std::string& json_buff, size_t indent = 0)
|
||||
bool store_t_to_json(const t_struct& str_in, std::string& json_buff, size_t indent = 0, end_of_line_t eol = eol_crlf)
|
||||
{
|
||||
portable_storage ps;
|
||||
str_in.store(ps);
|
||||
ps.dump_as_json(json_buff, indent);
|
||||
ps.dump_as_json(json_buff, indent, eol);
|
||||
return true;
|
||||
}
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
template<class t_struct>
|
||||
std::string store_t_to_json(const t_struct& str_in, size_t indent = 0)
|
||||
std::string store_t_to_json(const t_struct& str_in, size_t indent = 0, end_of_line_t eol = eol_crlf)
|
||||
{
|
||||
std::string json_buff;
|
||||
store_t_to_json(str_in, json_buff, indent);
|
||||
|
|
|
|||
|
|
@ -37,23 +37,23 @@ namespace epee
|
|||
{
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const array_entry& ae, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const array_entry& ae, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const storage_entry& se, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const storage_entry& se, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const std::string& v, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const std::string& v, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const int8_t& v, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const int8_t& v, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const uint8_t& v, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const uint8_t& v, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const bool& v, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const bool& v, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const double& v, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const double& v, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream, class t_type>
|
||||
void dump_as_json(t_stream& strm, const t_type& v, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const t_type& v, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const section& sec, size_t indent);
|
||||
void dump_as_json(t_stream& strm, const section& sec, size_t indent, end_of_line_t eol = eol_crlf);
|
||||
|
||||
|
||||
inline std::string make_indent(size_t indent)
|
||||
|
|
@ -66,7 +66,13 @@ namespace epee
|
|||
{
|
||||
t_stream& m_strm;
|
||||
size_t m_indent;
|
||||
array_entry_store_to_json_visitor(t_stream& strm, size_t indent):m_strm(strm), m_indent(indent){}
|
||||
end_of_line_t m_eol;
|
||||
|
||||
array_entry_store_to_json_visitor(t_stream& strm, size_t indent, end_of_line_t eol)
|
||||
: m_strm(strm)
|
||||
, m_indent(indent)
|
||||
, m_eol(eol)
|
||||
{}
|
||||
|
||||
template<class t_type>
|
||||
void operator()(const array_entry_t<t_type>& a)
|
||||
|
|
@ -77,7 +83,7 @@ namespace epee
|
|||
auto last_it = --a.m_array.end();
|
||||
for(auto it = a.m_array.begin(); it != a.m_array.end(); it++)
|
||||
{
|
||||
dump_as_json(m_strm, *it, m_indent);
|
||||
dump_as_json(m_strm, *it, m_indent, m_eol);
|
||||
if(it != last_it)
|
||||
m_strm << ",";
|
||||
}
|
||||
|
|
@ -91,50 +97,56 @@ namespace epee
|
|||
{
|
||||
t_stream& m_strm;
|
||||
size_t m_indent;
|
||||
storage_entry_store_to_json_visitor(t_stream& strm, size_t indent):m_strm(strm), m_indent(indent)
|
||||
end_of_line_t m_eol;
|
||||
|
||||
storage_entry_store_to_json_visitor(t_stream& strm, size_t indent, end_of_line_t eol)
|
||||
: m_strm(strm)
|
||||
, m_indent(indent)
|
||||
, m_eol(eol)
|
||||
{}
|
||||
|
||||
//section, array_entry
|
||||
template<class visited_type>
|
||||
void operator()(const visited_type& v)
|
||||
{
|
||||
dump_as_json(m_strm, v, m_indent);
|
||||
dump_as_json(m_strm, v, m_indent, m_eol);
|
||||
}
|
||||
};
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const array_entry& ae, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const array_entry& ae, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
array_entry_store_to_json_visitor<t_stream> aesv(strm, indent);
|
||||
array_entry_store_to_json_visitor<t_stream> aesv(strm, indent, eol);
|
||||
boost::apply_visitor(aesv, ae);
|
||||
}
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const storage_entry& se, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const storage_entry& se, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
storage_entry_store_to_json_visitor<t_stream> sv(strm, indent);
|
||||
storage_entry_store_to_json_visitor<t_stream> sv(strm, indent, eol);
|
||||
boost::apply_visitor(sv, se);
|
||||
}
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const std::string& v, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const std::string& v, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
strm << "\"" << misc_utils::parse::transform_to_json_escape_sequence(v) << "\"";
|
||||
}
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const int8_t& v, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const int8_t& v, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
strm << static_cast<int32_t>(v);
|
||||
}
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const uint8_t& v, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const uint8_t& v, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
strm << static_cast<int32_t>(v);
|
||||
}
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const bool& v, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const bool& v, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
if(v)
|
||||
strm << "true";
|
||||
|
|
@ -143,23 +155,34 @@ namespace epee
|
|||
}
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const double& v, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const double& v, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
strm.precision(8);
|
||||
strm << std::fixed << v;
|
||||
}
|
||||
|
||||
template<class t_stream, class t_type>
|
||||
void dump_as_json(t_stream& strm, const t_type& v, size_t /*indent*/)
|
||||
void dump_as_json(t_stream& strm, const t_type& v, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
strm << v;
|
||||
}
|
||||
|
||||
template<class t_stream>
|
||||
void dump_as_json(t_stream& strm, const section& sec, size_t indent)
|
||||
void dump_as_json(t_stream& strm, const section& sec, size_t indent, end_of_line_t eol)
|
||||
{
|
||||
auto put_eol = [&]() {
|
||||
switch (eol)
|
||||
{
|
||||
case eol_lf: strm << "\n"; break;
|
||||
case eol_cr: strm << "\r"; break;
|
||||
case eol_space: strm << " "; break;
|
||||
default: strm << "\r\n"; break;
|
||||
}
|
||||
};
|
||||
|
||||
size_t local_indent = indent + 1;
|
||||
strm << "{\r\n";
|
||||
strm << "{";
|
||||
put_eol();
|
||||
std::string indent_str = make_indent(local_indent);
|
||||
if(sec.m_entries.size())
|
||||
{
|
||||
|
|
@ -167,10 +190,10 @@ namespace epee
|
|||
for(auto it = sec.m_entries.begin(); it!= sec.m_entries.end();it++)
|
||||
{
|
||||
strm << indent_str << "\"" << misc_utils::parse::transform_to_json_escape_sequence(it->first) << "\"" << ": ";
|
||||
dump_as_json(strm, it->second, local_indent);
|
||||
dump_as_json(strm, it->second, local_indent, eol);
|
||||
if(it_last != it)
|
||||
strm << ",";
|
||||
strm << "\r\n";
|
||||
put_eol();
|
||||
}
|
||||
}
|
||||
strm << make_indent(indent) << "}";
|
||||
|
|
|
|||
|
|
@ -650,7 +650,7 @@ bool MainWindow::update_wallet_status(const view::wallet_status_info& wsi)
|
|||
TRY_ENTRY();
|
||||
m_wallet_states->operator [](wsi.wallet_id) = wsi.wallet_state;
|
||||
std::string json_str;
|
||||
epee::serialization::store_t_to_json(wsi, json_str);
|
||||
epee::serialization::store_t_to_json(wsi, json_str, 0, epee::serialization::eol_lf);
|
||||
LOG_PRINT_L0(get_wallet_log_prefix(wsi.wallet_id) + "SENDING SIGNAL -> [update_wallet_status]:" << std::endl << json_str );
|
||||
QMetaObject::invokeMethod(this, "update_wallet_status", Qt::QueuedConnection, Q_ARG(QString, json_str.c_str()));
|
||||
return true;
|
||||
|
|
@ -660,7 +660,7 @@ bool MainWindow::set_options(const view::gui_options& opt)
|
|||
{
|
||||
TRY_ENTRY();
|
||||
std::string json_str;
|
||||
epee::serialization::store_t_to_json(opt, json_str);
|
||||
epee::serialization::store_t_to_json(opt, json_str, 0, epee::serialization::eol_lf);
|
||||
LOG_PRINT_L0("SENDING SIGNAL -> [set_options]:" << std::endl << json_str);
|
||||
QMetaObject::invokeMethod(this, "set_options", Qt::QueuedConnection, Q_ARG(QString, json_str.c_str()));
|
||||
return true;
|
||||
|
|
@ -688,7 +688,7 @@ bool MainWindow::update_wallets_info(const view::wallets_summary_info& wsi)
|
|||
{
|
||||
TRY_ENTRY();
|
||||
std::string json_str;
|
||||
epee::serialization::store_t_to_json(wsi, json_str);
|
||||
epee::serialization::store_t_to_json(wsi, json_str, 0, epee::serialization::eol_lf);
|
||||
LOG_PRINT_L0("SENDING SIGNAL -> [update_wallets_info]"<< std::endl << json_str );
|
||||
|
||||
QMetaObject::invokeMethod(this, "update_wallets_info", Qt::QueuedConnection, Q_ARG(QString, json_str.c_str()));
|
||||
|
|
@ -700,7 +700,7 @@ bool MainWindow::money_transfer(const view::transfer_event_info& tei)
|
|||
{
|
||||
TRY_ENTRY();
|
||||
std::string json_str;
|
||||
epee::serialization::store_t_to_json(tei, json_str);
|
||||
epee::serialization::store_t_to_json(tei, json_str, 0, epee::serialization::eol_lf);
|
||||
|
||||
LOG_PRINT_L0(get_wallet_log_prefix(tei.wallet_id) + "SENDING SIGNAL -> [money_transfer]" << std::endl << json_str);
|
||||
//this->money_transfer(json_str.c_str());
|
||||
|
|
@ -745,7 +745,7 @@ bool MainWindow::money_transfer_cancel(const view::transfer_event_info& tei)
|
|||
{
|
||||
TRY_ENTRY();
|
||||
std::string json_str;
|
||||
epee::serialization::store_t_to_json(tei, json_str);
|
||||
epee::serialization::store_t_to_json(tei, json_str, 0, epee::serialization::eol_lf);
|
||||
|
||||
LOG_PRINT_L0(get_wallet_log_prefix(tei.wallet_id) + "SENDING SIGNAL -> [money_transfer_cancel]");
|
||||
//this->money_transfer_cancel(json_str.c_str());
|
||||
|
|
@ -760,7 +760,7 @@ bool MainWindow::wallet_sync_progress(const view::wallet_sync_progres_param& p)
|
|||
TRY_ENTRY();
|
||||
LOG_PRINT_L2(get_wallet_log_prefix(p.wallet_id) + "SENDING SIGNAL -> [wallet_sync_progress]" << " wallet_id: " << p.wallet_id << ": " << p.progress << "%");
|
||||
//this->wallet_sync_progress(epee::serialization::store_t_to_json(p).c_str());
|
||||
QMetaObject::invokeMethod(this, "wallet_sync_progress", Qt::QueuedConnection, Q_ARG(QString, epee::serialization::store_t_to_json(p).c_str()));
|
||||
QMetaObject::invokeMethod(this, "wallet_sync_progress", Qt::QueuedConnection, Q_ARG(QString, epee::serialization::store_t_to_json(p, 0, epee::serialization::eol_lf).c_str()));
|
||||
return true;
|
||||
CATCH_ENTRY2(false);
|
||||
}
|
||||
|
|
@ -811,7 +811,7 @@ QString MainWindow::get_alias_coast(const QString& param)
|
|||
PREPARE_ARG_FROM_JSON(view::struct_with_one_t_type<std::string>, lvl);
|
||||
view::get_alias_coast_response resp;
|
||||
resp.error_code = m_backend.get_alias_coast(lvl.v, resp.coast);
|
||||
return epee::serialization::store_t_to_json(resp).c_str();
|
||||
return epee::serialization::store_t_to_json(resp, 0, epee::serialization::eol_lf).c_str();
|
||||
CATCH_ENTRY_FAIL_API_RESPONCE();
|
||||
}
|
||||
|
||||
|
|
@ -836,7 +836,7 @@ QString MainWindow::set_localization_strings(const QString param)
|
|||
resp.error_code = API_RETURN_CODE_OK;
|
||||
LOG_PRINT_L0("New localization set, language title: " << lr.language_title << ", strings " << lr.strings.size());
|
||||
}
|
||||
return epee::serialization::store_t_to_json(resp).c_str();
|
||||
return epee::serialization::store_t_to_json(resp, 0, epee::serialization::eol_lf).c_str();
|
||||
CATCH_ENTRY_FAIL_API_RESPONCE();
|
||||
}
|
||||
|
||||
|
|
@ -1292,7 +1292,7 @@ QString MainWindow::show_openfile_dialog(const QString& param)
|
|||
if (!epee::serialization::load_t_from_json(ofdr, param.toStdString()))
|
||||
{
|
||||
ofdres.error_code = API_RETURN_CODE_BAD_ARG;
|
||||
return epee::serialization::store_t_to_json(ofdres).c_str();
|
||||
return epee::serialization::store_t_to_json(ofdres, 0, epee::serialization::eol_lf).c_str();
|
||||
}
|
||||
|
||||
QString path = QFileDialog::getOpenFileName(this, ofdr.caption.c_str(),
|
||||
|
|
@ -1302,7 +1302,7 @@ QString MainWindow::show_openfile_dialog(const QString& param)
|
|||
if (!path.length())
|
||||
{
|
||||
ofdres.error_code = API_RETURN_CODE_CANCELED;
|
||||
return epee::serialization::store_t_to_json(ofdres).c_str();
|
||||
return epee::serialization::store_t_to_json(ofdres, 0, epee::serialization::eol_lf).c_str();
|
||||
}
|
||||
|
||||
ofdres.error_code = API_RETURN_CODE_OK;
|
||||
|
|
@ -1325,7 +1325,7 @@ QString MainWindow::show_savefile_dialog(const QString& param)
|
|||
if (!path.length())
|
||||
{
|
||||
ofdres.error_code = API_RETURN_CODE_CANCELED;
|
||||
return epee::serialization::store_t_to_json(ofdres).c_str();
|
||||
return epee::serialization::store_t_to_json(ofdres, 0, epee::serialization::eol_lf).c_str();
|
||||
}
|
||||
|
||||
ofdres.error_code = API_RETURN_CODE_OK;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue