cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Caller.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2023-2024 Laurent Forthomme
4
*
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include <iostream>
20
#include <memory>
21
#include <string>
22
#include <vector>
23
24
#include "
CepGen/Core/Exception.h
"
25
#include "
CepGen/Utils/Caller.h
"
26
#include "
CepGen/Utils/String.h
"
27
28
namespace
cepgen
{
29
namespace
utils {
30
Caller::Caller
() : oldcout_(std::cout.rdbuf(os_cout_.rdbuf())), oldcerr_(std::cerr.rdbuf(os_cerr_.rdbuf())) {}
31
32
Caller::~Caller
() {
33
std::cout.rdbuf(oldcout_);
34
std::cerr.rdbuf(oldcerr_);
35
if
(
const
auto
& str = os_cout_.str(); !str.empty())
36
CG_DEBUG
(
"Caller"
) <<
"At end of caller call, the following output was generated:\n"
<< str;
37
if
(
const
auto
& str = os_cerr_.str(); !str.empty())
38
CG_WARNING
(
"Caller"
) <<
"At end of caller call, the following error stream was generated:\n"
<< str;
39
}
40
41
std::string
Caller::output
()
const
{
return
os_cout_.str(); }
42
43
std::string
Caller::error
()
const
{
return
os_cerr_.str(); }
44
45
std::string
Caller::call
(
const
std::vector<std::string>& commands) {
return
call
(
utils::merge
(commands,
" "
)); }
46
47
std::string
Caller::call
(
const
std::string& command) {
48
auto
pipe = popen(command.c_str(),
"r"
);
49
if
(!pipe)
50
throw
CG_FATAL
(
"Caller"
) <<
"Failed to call the command '"
<< command <<
"'."
;
51
std::array<char, 128> buffer;
52
std::string out;
53
while
(!feof(pipe))
54
if
(fgets(buffer.data(), buffer.size(), pipe) !=
nullptr
) {
55
std::cout << buffer.data();
56
out += buffer.data();
57
}
58
auto
rc = pclose(pipe);
59
if
(rc != EXIT_SUCCESS)
60
throw
CG_FATAL
(
"Caller"
) <<
"Command '"
<< command <<
"' failed with return code '"
<< rc <<
"'."
;
61
return
out;
62
}
63
}
// namespace utils
64
}
// namespace cepgen
Caller.h
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
CG_WARNING
#define CG_WARNING(mod)
Definition
Message.h:228
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
String.h
cepgen::utils::Caller::call
static std::string call(const std::vector< std::string > &commands)
Start a logged call command.
Definition
Caller.cpp:45
cepgen::utils::Caller::output
std::string output() const
Retrieve the (potential) output from the command.
Definition
Caller.cpp:41
cepgen::utils::Caller::error
std::string error() const
Retrieve the (potential) error stream from the command.
Definition
Caller.cpp:43
cepgen::utils::Caller::~Caller
~Caller()
Definition
Caller.cpp:32
cepgen::utils::Caller::Caller
Caller()
Definition
Caller.cpp:30
cepgen::utils::merge
std::string merge(const std::vector< T > &vec, const std::string &delim)
Merge a collection of a printable type in a single string.
Definition
String.cpp:248
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGen
Utils
Caller.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7