From 0b8842b48fa09ae9049d6689ad0ab9d83c80fd78 Mon Sep 17 00:00:00 2001 From: Nils Erik Krumnack <nils.erik.krumnack@cern.ch> Date: Tue, 10 May 2016 19:53:59 +0200 Subject: [PATCH] added a version of exec_read that provides a return code (RootCoreUtils-00-00-37) * added a version of exec_read that provides a return code * going into RootCoreUtils-00-00-37 2016-04-18 Nils Krumnack <Nils.Erik.Krumnack@cern.ch> * added a [[noreturn]] attribute to RCU_THROW_MSG * going into RootCoreUtils-00-00-36 Former-commit-id: f9905739001e8a3117cfcf2753a1a54032f49383 --- .../D3PDTools/RootCoreUtils/Root/PrintMsg.cxx | 34 +++++++++++++++++++ .../RootCoreUtils/Root/ShellExec.cxx | 19 ++++++++--- .../RootCoreUtils/RootCoreUtils/PrintMsg.h | 11 +++++- .../RootCoreUtils/RootCoreUtils/ShellExec.h | 9 +++++ 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/PrintMsg.cxx b/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/PrintMsg.cxx index 2085e6a92a21..77d64e14bef3 100644 --- a/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/PrintMsg.cxx +++ b/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/PrintMsg.cxx @@ -14,7 +14,9 @@ #include <RootCoreUtils/PrintMsg.h> #include <TString.h> +#include <RootCoreUtils/Assert.h> #include <RootCoreUtils/Message.h> +#include <cstdlib> // // method implementations @@ -49,4 +51,36 @@ namespace RCU { send_message (package, file, line, type, msg.Data()); } + + + + void send_message_abort (const char *package, const char *file, unsigned line, + MessageType type, const char *msg) + { + Message message; + message.package = package; + message.file = file; + message.line = line; + message.type = type; + message.message = msg; + message.send (); + RCU_ASSERT0 ("shouldn't get here"); + std::abort (); + } + + + + void send_message_abort (const char *package, const char *file, unsigned line, + MessageType type, const std::string& msg) + { + send_message_abort (package, file, line, type, msg.c_str()); + } + + + + void send_message_abort (const char *package, const char *file, unsigned line, + MessageType type, const TString& msg) + { + send_message_abort (package, file, line, type, msg.Data()); + } } diff --git a/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/ShellExec.cxx b/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/ShellExec.cxx index 3e3eec5a65f1..d487400d8ea9 100644 --- a/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/ShellExec.cxx +++ b/PhysicsAnalysis/D3PDTools/RootCoreUtils/Root/ShellExec.cxx @@ -35,6 +35,17 @@ namespace RCU std::string exec_read (const std::string& cmd) + { + int rc = 0; + std::string result = exec_read (cmd, rc); + if (rc != 0) + RCU_THROW_MSG ("command failed: " + cmd + "\nwith output:\n" + result); + return result; + } + + + + std::string exec_read (const std::string& cmd, int& rc) { std::string result; FILE *pipe = 0; @@ -48,15 +59,15 @@ namespace RCU { result.append (&buffer[0], read); } + rc = pclose (pipe); + pipe = nullptr; + return result; } catch (...) { if (pipe) - pclose (pipe); + rc = pclose (pipe); throw; } - if (pclose (pipe) != 0) - RCU_THROW_MSG ("command failed: " + cmd + "\nwith output:\n" + result); - return result; } diff --git a/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/PrintMsg.h b/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/PrintMsg.h index d2ef773550b6..d4349a8552b6 100644 --- a/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/PrintMsg.h +++ b/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/PrintMsg.h @@ -31,6 +31,15 @@ namespace RCU MessageType type, const std::string& msg); void send_message (const char *package, const char *file, unsigned line, MessageType type, const TString& msg); + [[noreturn]] void + send_message_abort (const char *package, const char *file, unsigned line, + MessageType type, const char *msg); + [[noreturn]] void + send_message_abort (const char *package, const char *file, unsigned line, + MessageType type, const std::string& msg); + [[noreturn]] void + send_message_abort (const char *package, const char *file, unsigned line, + MessageType type, const TString& msg); } #ifndef ROOTCORE_PACKAGE @@ -47,6 +56,6 @@ namespace RCU ::RCU::send_message (ROOTCORE_PACKAGE, __FILE__, __LINE__, ::RCU::MESSAGE_ERROR, (message)); #define RCU_THROW_MSG(message) \ - ::RCU::send_message (ROOTCORE_PACKAGE, __FILE__, __LINE__, ::RCU::MESSAGE_EXCEPTION, (message)); + ::RCU::send_message_abort (ROOTCORE_PACKAGE, __FILE__, __LINE__, ::RCU::MESSAGE_EXCEPTION, (message)); #endif diff --git a/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/ShellExec.h b/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/ShellExec.h index b977dec847eb..f40c72b9b085 100644 --- a/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/ShellExec.h +++ b/PhysicsAnalysis/D3PDTools/RootCoreUtils/RootCoreUtils/ShellExec.h @@ -41,6 +41,15 @@ namespace RCU std::string exec_read (const std::string& cmd); + /// effects: execute the given command and return the output + /// returns: the output of the command + /// guarantee: strong + /// failures: out of memory III + /// failures: system failure + /// failures: command failure + std::string exec_read (const std::string& cmd, int& rc); + + /// effects: quote the given name to protect it from the shell /// returns: the quoted name /// guarantee: strong -- GitLab