notify: handle arbitrary tags

This commit is contained in:
moneromooo-monero 2019-01-09 14:28:42 +00:00
parent ff95921668
commit f6db59b011
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
5 changed files with 23 additions and 7 deletions

View file

@ -27,6 +27,7 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <stdarg.h>
#include "misc_log_ex.h" #include "misc_log_ex.h"
#include "file_io_utils.h" #include "file_io_utils.h"
#include "spawn.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); 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<std::string> &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<std::string> margs = args; std::vector<std::string> 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); return tools::spawn(filename.c_str(), margs, false);
} }

View file

@ -39,7 +39,7 @@ class Notify
public: public:
Notify(const char *spec); Notify(const char *spec);
int notify(const char *parameter); int notify(const char *tag, const char *s, ...);
private: private:
std::string filename; std::string filename;

View file

@ -3701,7 +3701,7 @@ leave:
std::shared_ptr<tools::Notify> block_notify = m_block_notify; std::shared_ptr<tools::Notify> block_notify = m_block_notify;
if (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; return true;
} }

View file

@ -2041,7 +2041,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote
{ {
std::shared_ptr<tools::Notify> tx_notify = m_tx_notify; std::shared_ptr<tools::Notify> tx_notify = m_tx_notify;
if (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);
} }
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View file

@ -67,7 +67,7 @@ TEST(notify, works)
+ " " + name_template + " %s"; + " " + name_template + " %s";
tools::Notify notify(spec.c_str()); tools::Notify notify(spec.c_str());
notify.notify("1111111111111111111111111111111111111111111111111111111111111111"); notify.notify("%s", "1111111111111111111111111111111111111111111111111111111111111111", NULL);
bool ok = false; bool ok = false;
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)