GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TRedirect.h
Go to the documentation of this file.
1#ifndef TREDIRECT_H
2#define TREDIRECT_H
3
4#include <iostream>
5#include <cstdio>
6#include <cstdlib>
7#include <fcntl.h>
8#include <unistd.h>
9
10/////////////////////////////////////////////////////////////////
11///
12/// \class TRedirect
13///
14/// A simple class to redirect stdout and stderr to file(s). To
15/// use it, create it, providing the path of the files stdout and
16/// stderr should be redirected to, and an (optional) flag whether
17/// to append to those files (default) or not.
18/// If only one file is provided, both stdout and stderr are
19/// redirected to it.
20/// If the filename for either stdout or stderr is a nullptr, the
21/// redirection for this output won't happen.
22/// The redirection ends when the TRedirect object is destroyed.
23///
24/////////////////////////////////////////////////////////////////
25
26class TRedirect {
27public:
28 TRedirect(const char* newOut, const char* newErr, bool append = true)
29 : fOutFile(newOut), fErrFile(newErr)
30 {
31 Redirect(append);
32 }
33 explicit TRedirect(const char* newOut, bool append = true)
34 : fOutFile(newOut), fErrFile(newOut)
35 {
36 Redirect(append);
37 }
38
39 const char* OutFile() { return fOutFile; }
40 const char* ErrFile() { return fErrFile; }
41
43 {
44 fflush(stdout);
45 dup2(fStdOutFileDescriptor, fileno(stdout));
47 fflush(stderr);
48 dup2(fStdErrFileDescriptor, fileno(stderr));
50 }
51
52 TRedirect(const TRedirect&) = delete;
53 TRedirect(TRedirect&&) = delete;
54
55 TRedirect& operator=(const TRedirect&) = delete;
57
58private:
59 void Redirect(bool append)
60 {
61 if(fOutFile != nullptr) {
62 fStdOutFileDescriptor = dup(fileno(stdout));
63 fflush(stdout);
64 int newStdOut = open(fOutFile, (append ? O_WRONLY | O_CREAT | O_APPEND : O_WRONLY | O_CREAT), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
65 dup2(newStdOut, fileno(stdout));
66 close(newStdOut);
67 }
68 if(fErrFile != nullptr) {
69 fStdErrFileDescriptor = dup(fileno(stderr));
70 fflush(stderr);
71 int newStdErr = open(fErrFile, (append ? O_WRONLY | O_CREAT | O_APPEND : O_WRONLY | O_CREAT), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
72 dup2(newStdErr, fileno(stderr));
73 close(newStdErr);
74 }
75 }
76
79 const char* fOutFile{nullptr};
80 const char* fErrFile{nullptr};
81};
82
83#endif
TRedirect(const char *newOut, const char *newErr, bool append=true)
Definition TRedirect.h:28
void Redirect(bool append)
Definition TRedirect.h:59
TRedirect(const char *newOut, bool append=true)
Definition TRedirect.h:33
TRedirect(const TRedirect &)=delete
const char * OutFile()
Definition TRedirect.h:39
int fStdErrFileDescriptor
Definition TRedirect.h:78
TRedirect & operator=(const TRedirect &)=delete
TRedirect & operator=(TRedirect &&)=delete
TRedirect(TRedirect &&)=delete
const char * fErrFile
Definition TRedirect.h:80
int fStdOutFileDescriptor
Definition TRedirect.h:77
const char * fOutFile
Definition TRedirect.h:79
const char * ErrFile()
Definition TRedirect.h:40