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