From f6db59b011914c2180b42ed4f6f144679a126994 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 9 Jan 2019 14:28:42 +0000 Subject: [PATCH] notify: handle arbitrary tags --- src/common/notify.cpp | 22 +++++++++++++++++++--- src/common/notify.h | 2 +- src/cryptonote_core/blockchain.cpp | 2 +- src/wallet/wallet2.cpp | 2 +- tests/unit_tests/notify.cpp | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/common/notify.cpp b/src/common/notify.cpp index 0ac3d501b..c3165fb05 100644 --- a/src/common/notify.cpp +++ b/src/common/notify.cpp @@ -27,6 +27,7 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include +#include #include "misc_log_ex.h" #include "file_io_utils.h" #include "spawn.h" @@ -55,11 +56,26 @@ Notify::Notify(const char *spec) CHECK_AND_ASSERT_THROW_MES(epee::file_io_utils::is_file_exist(filename), "File not found: " << filename); } -int Notify::notify(const char *parameter) +static void replace(std::vector &v, const char *tag, const char *s) +{ + for (std::string &str: v) + boost::replace_all(str, tag, s); +} + +int Notify::notify(const char *tag, const char *s, ...) { std::vector margs = args; - for (std::string &s: margs) - boost::replace_all(s, "%s", parameter); + + replace(margs, tag, s); + + va_list ap; + va_start(ap, s); + while ((tag = va_arg(ap, const char*))) + { + s = va_arg(ap, const char*); + replace(margs, tag, s); + } + va_end(ap); return tools::spawn(filename.c_str(), margs, false); } diff --git a/src/common/notify.h b/src/common/notify.h index 81aacebb0..f813e8def 100644 --- a/src/common/notify.h +++ b/src/common/notify.h @@ -39,7 +39,7 @@ class Notify public: Notify(const char *spec); - int notify(const char *parameter); + int notify(const char *tag, const char *s, ...); private: std::string filename; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 9f1e2a0c0..7620dfd61 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -3701,7 +3701,7 @@ leave: std::shared_ptr block_notify = m_block_notify; if (block_notify) - block_notify->notify(epee::string_tools::pod_to_hex(id).c_str()); + block_notify->notify("%s", epee::string_tools::pod_to_hex(id).c_str(), NULL); return true; } diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a8fe9c7cb..eaffe6472 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2041,7 +2041,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote { std::shared_ptr tx_notify = m_tx_notify; if (tx_notify) - tx_notify->notify(epee::string_tools::pod_to_hex(txid).c_str()); + tx_notify->notify("%s", epee::string_tools::pod_to_hex(txid).c_str(), NULL); } } //---------------------------------------------------------------------------------------------------- diff --git a/tests/unit_tests/notify.cpp b/tests/unit_tests/notify.cpp index cd70b7739..ceeba8649 100644 --- a/tests/unit_tests/notify.cpp +++ b/tests/unit_tests/notify.cpp @@ -67,7 +67,7 @@ TEST(notify, works) + " " + name_template + " %s"; tools::Notify notify(spec.c_str()); - notify.notify("1111111111111111111111111111111111111111111111111111111111111111"); + notify.notify("%s", "1111111111111111111111111111111111111111111111111111111111111111", NULL); bool ok = false; for (int i = 0; i < 10; ++i)