-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_manager.cpp
64 lines (56 loc) · 1.66 KB
/
output_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "output_manager.hpp"
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "functions.hpp"
namespace tsp {
Output_handler::Output_handler(bool disappear, bool separate_stderr,
std::string jobid, bool rw)
: stdout_fn_{get_tmp() / (out_file_template + jobid)},
stderr_fn_{get_tmp() / (err_file_template + jobid)}, disappear_{disappear},
separate_stderr_{separate_stderr} {}
void Output_handler::init_pipes() {
if (disappear_) {
stdout_fd_ = open("/dev/null", O_WRONLY | O_CREAT, 0666);
stderr_fd_ = open("/dev/null", O_WRONLY | O_CREAT, 0666);
} else {
stdout_fd_ = open(stdout_fn_.c_str(), O_WRONLY | O_CREAT, 0600);
if (separate_stderr_) {
stderr_fd_ = open(stderr_fn_.c_str(), O_WRONLY | O_CREAT, 0600);
} else {
stderr_fd_ = stdout_fd_;
}
}
dup2(stdout_fd_, 1);
dup2(stderr_fd_, 2);
}
std::pair<std::string, std::string> Output_handler::get_output() {
std::ifstream stdout_stream(stdout_fn_);
std::stringstream ss_out{};
ss_out << stdout_stream.rdbuf();
stdout_stream.close();
std::filesystem::remove(stdout_fn_);
out_bufs_.first = ss_out.str();
std::ifstream stderr_stream(stderr_fn_);
std::stringstream ss_err{};
ss_err << stdout_stream.rdbuf();
stderr_stream.close();
std::filesystem::remove(stderr_fn_);
out_bufs_.second = ss_err.str();
return out_bufs_;
}
Output_handler::~Output_handler() {
if (stdout_fd_ != -1) {
close(stdout_fd_);
}
if (stderr_fd_ != -1) {
close(stderr_fd_);
}
}
} // namespace tsp