Commit f9905739 authored by Nils Erik Krumnack's avatar Nils Erik Krumnack Committed by Graeme Stewart
Browse files

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
parent 7a8af5ef
......@@ -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());
}
}
......@@ -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;
}
......
......@@ -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
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment