aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-09-04 14:15:37 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-09-04 14:15:37 +0200
commit81abf8eab067eab362f180a418215f9408de41dc (patch)
treefd9915dd0ac551e989247fcd2cc78f57a378b6c4
parent0aa9007705231b72616c533aeed6a28ca3349999 (diff)
Add support for printing process command line
-rw-r--r--bpkg/diagnostics31
-rw-r--r--bpkg/diagnostics.cxx34
2 files changed, 64 insertions, 1 deletions
diff --git a/bpkg/diagnostics b/bpkg/diagnostics
index 85eef2b..af9bde0 100644
--- a/bpkg/diagnostics
+++ b/bpkg/diagnostics
@@ -17,11 +17,41 @@
namespace bpkg
{
+ struct diag_record;
+
// Throw this exception to terminate the process. The handler should
// assume that the diagnostics has already been issued.
//
class failed: public std::exception {};
+ // Print process commmand line. If the number of elements is specified
+ // (or the second version is used), then it will print the piped multi-
+ // process command line, if present. In this case, the expected format
+ // is as follows:
+ //
+ // name1 arg arg ... nullptr
+ // name2 arg arg ... nullptr
+ // ...
+ // nameN arg arg ... nullptr nullptr
+ //
+ void
+ print_process (diag_record&, const char* const* args, std::size_t n = 0);
+
+ void
+ print_process (const char* const* args, std::size_t n = 0);
+
+ inline void
+ print_process (diag_record& dr, const cstrings& args)
+ {
+ print_process (dr, args.data (), args.size ());
+ }
+
+ inline void
+ print_process (const cstrings& args)
+ {
+ print_process (args.data (), args.size ());
+ }
+
// Trace verbosity level.
//
// 0 - tracing disabled.
@@ -45,7 +75,6 @@ namespace bpkg
//
extern std::ostream* diag_stream;
- struct diag_record;
template <typename> struct diag_prologue;
template <typename> struct diag_mark;
diff --git a/bpkg/diagnostics.cxx b/bpkg/diagnostics.cxx
index 2bee18e..4c5e24b 100644
--- a/bpkg/diagnostics.cxx
+++ b/bpkg/diagnostics.cxx
@@ -10,6 +10,40 @@ using namespace std;
namespace bpkg
{
+ // print_process
+ //
+ void
+ print_process (const char* const* args, size_t n)
+ {
+ diag_record r (text);
+ print_process (r, args, n);
+ }
+
+ void
+ print_process (diag_record& r, const char* const* args, size_t n)
+ {
+ size_t m (0);
+ const char* const* p (args);
+ do
+ {
+ if (m != 0)
+ r << " |"; // Trailing space will be added inside the loop.
+
+ for (m++; *p != nullptr; p++, m++)
+ r << (p != args ? " " : "")
+ << (**p == '\0' ? "\"" : "") // Quote empty arguments.
+ << *p
+ << (**p == '\0' ? "\"" : "");
+
+ if (m < n) // Can we examine the next element?
+ {
+ p++;
+ m++;
+ }
+
+ } while (*p != nullptr);
+ }
+
// Trace verbosity level.
//
uint16_t verb;