From 7521dc56e8297964217fb886b5f344810f91d350 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 6 Oct 2014 23:04:44 -0400 Subject: [PATCH] Hooks - Supports 'debug.hooks' configuration setting. --- ChangeLog | 1 + NEWS | 1 + src/Hooks.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/Hooks.h | 1 + 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index e77250b29..c89c774f1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -204,6 +204,7 @@ - The filter form 'name:value' now maps to the partial match operator '=', rather than the exact match operator, '=='. This means that dates now match on the day by default, not the time also. +- Supports 'debug.hooks' configuration setting. ------ current release --------------------------- diff --git a/NEWS b/NEWS index 82d189e08..11b8ff302 100644 --- a/NEWS +++ b/NEWS @@ -39,6 +39,7 @@ New configuration options in taskwarrior 2.4.0 - New truncated_count column style for the description field which as the name says is a combination of the existing truncated and count styles. - New 'hooks' setting is a master control switch for hook processing. + - New 'debug.hooks' for debugging hook scripts. Newly deprecated features in taskwarrior 2.4.0 diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 4515df3a5..f15b952ff 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -45,6 +45,7 @@ extern Context context; //////////////////////////////////////////////////////////////////////////////// Hooks::Hooks () : _enabled (true) +, _debug (0) { } @@ -56,6 +57,8 @@ Hooks::~Hooks () //////////////////////////////////////////////////////////////////////////////// void Hooks::initialize () { + _debug = context.config.getInteger ("debug.hooks"); + // Scan /hooks Directory d (context.config.get ("data.location")); d += "hooks"; @@ -64,7 +67,16 @@ void Hooks::initialize () { _scripts = d.list (); std::sort (_scripts.begin (), _scripts.end ()); + + if (_debug >= 1) + { + std::vector ::iterator i; + for (i = _scripts.begin (); i != _scripts.end (); ++i) + context.debug ("Found hook script " + *i); + } } + else if (_debug >= 1) + context.debug ("Hook directory not readable: " + d._data); _enabled = context.config.getBoolean ("hooks"); } @@ -103,10 +115,16 @@ void Hooks::onLaunch () std::vector ::iterator i; for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i) { + if (_debug >= 1) + context.debug ("Hooks: Calling " + *i); + std::string output; std::vector args; int status = execute (*i, args, "", output); + if (_debug >= 2) + context.debug (format ("Hooks: Completed with status {1}", status)); + std::vector lines; split (lines, output, '\n'); std::vector ::iterator line; @@ -117,6 +135,9 @@ void Hooks::onLaunch () { if (line->length () && (*line)[0] == '{') { + if (_debug >= 2) + context.debug ("Hook output: " + *line); + // Only 'add' is possible. Task newTask (*line); context.tdb2.add (newTask); @@ -163,22 +184,37 @@ void Hooks::onExit () std::string input = ""; std::vector ::const_iterator t; for (t = changes.begin (); t != changes.end (); ++t) - input += t->composeJSON () + "\n"; + { + std::string json = t->composeJSON (); + if (_debug >= 2) + context.debug ("Hook input: " + json); + + input += json + "\n"; + } std::vector matchingScripts = scripts ("on-exit"); std::vector ::iterator i; for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i) { + if (_debug >= 1) + context.debug ("Hooks: Calling " + *i); + std::string output; std::vector args; int status = execute (*i, args, input, output); + if (_debug >= 2) + context.debug (format ("Hooks: Completed with status {1}", status)); + std::vector lines; split (lines, output, '\n'); std::vector ::iterator line; for (line = lines.begin (); line != lines.end (); ++line) { + if (_debug >= 2) + context.debug ("Hook output: " + *line); + if (line->length () && (*line)[0] != '{') { if (status == 0) @@ -217,11 +253,21 @@ void Hooks::onAdd (std::vector & changes) std::vector ::iterator i; for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i) { - std::string input = changes[0].composeJSON () + "\n"; + if (_debug >= 1) + context.debug ("Hooks: Calling " + *i); + + std::string input = changes[0].composeJSON (); + if (_debug >= 2) + context.debug ("Hook input: " + input); + + input += "\n"; std::string output; std::vector args; int status = execute (*i, args, input, output); + if (_debug >= 2) + context.debug (format ("Hooks: Completed with status {1}", status)); + std::vector lines; split (lines, output, '\n'); std::vector ::iterator line; @@ -231,6 +277,9 @@ void Hooks::onAdd (std::vector & changes) changes.clear (); for (line = lines.begin (); line != lines.end (); ++line) { + if (_debug >= 2) + context.debug ("Hook output: " + *line); + if (line->length () && (*line)[0] == '{') changes.push_back (Task (*line)); else @@ -276,8 +325,18 @@ void Hooks::onModify (const Task& before, std::vector & changes) std::vector ::iterator i; for (i = matchingScripts.begin (); i != matchingScripts.end (); ++i) { + if (_debug >= 1) + context.debug ("Hooks: Calling " + *i); + + std::string beforeJSON = before.composeJSON (); std::string afterJSON = changes[0].composeJSON (); - std::string input = before.composeJSON () + if (_debug >= 2) + { + context.debug ("Hook input: " + beforeJSON); + context.debug ("Hook input: " + afterJSON); + } + + std::string input = beforeJSON + "\n" + afterJSON + "\n"; @@ -285,6 +344,9 @@ void Hooks::onModify (const Task& before, std::vector & changes) std::vector args; int status = execute (*i, args, input, output); + if (_debug >= 2) + context.debug (format ("Hooks: Completed with status {1}", status)); + std::vector lines; split (lines, output, '\n'); std::vector ::iterator line; @@ -294,6 +356,9 @@ void Hooks::onModify (const Task& before, std::vector & changes) changes.clear (); for (line = lines.begin (); line != lines.end (); ++line) { + if (_debug >= 2) + context.debug ("Hook output: " + *line); + if (line->length () && (*line)[0] == '{') changes.push_back (Task (*line)); else diff --git a/src/Hooks.h b/src/Hooks.h index b6e19accc..ae0d9d27c 100644 --- a/src/Hooks.h +++ b/src/Hooks.h @@ -54,6 +54,7 @@ private: private: bool _enabled; + int _debug; std::vector _scripts; };