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));
46 fflush(stderr);
47 dup2(fStdErrFileDescriptor, fileno(stderr));
48 }
49
50 TRedirect(const TRedirect&) = delete;
51 TRedirect(TRedirect&&) = delete;
52
53 TRedirect& operator=(const TRedirect&) = delete;
55
56private:
57 void Redirect(bool append)
58 {
59 if(fOutFile != nullptr) {
60 fStdOutFileDescriptor = dup(fileno(stdout));
61 fflush(stdout);
62 int newStdOut = open(fOutFile, (append ? O_WRONLY | O_CREAT | O_APPEND : O_WRONLY | O_CREAT), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
63 dup2(newStdOut, fileno(stdout));
64 close(newStdOut);
65 }
66 if(fErrFile != nullptr) {
67 fStdErrFileDescriptor = dup(fileno(stderr));
68 fflush(stderr);
69 int newStdErr = open(fErrFile, (append ? O_WRONLY | O_CREAT | O_APPEND : O_WRONLY | O_CREAT), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
70 dup2(newStdErr, fileno(stderr));
71 close(newStdErr);
72 }
73 }
74
77 const char* fOutFile{nullptr};
78 const char* fErrFile{nullptr};
79};
80
81#endif
TRedirect(const char *newOut, const char *newErr, bool append=true)
Definition TRedirect.h:28
void Redirect(bool append)
Definition TRedirect.h:57
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:76
TRedirect & operator=(const TRedirect &)=delete
TRedirect & operator=(TRedirect &&)=delete
TRedirect(TRedirect &&)=delete
const char * fErrFile
Definition TRedirect.h:78
int fStdOutFileDescriptor
Definition TRedirect.h:75
const char * fOutFile
Definition TRedirect.h:77
const char * ErrFile()
Definition TRedirect.h:40