| Line | |
|---|
| 1 | // ================================================================ // |
|---|
| 2 | // // |
|---|
| 3 | // File : SuppressOutput.h // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in November 2010 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // ================================================================ // |
|---|
| 11 | |
|---|
| 12 | #ifndef SUPPRESSOUTPUT_H |
|---|
| 13 | #define SUPPRESSOUTPUT_H |
|---|
| 14 | |
|---|
| 15 | #ifndef _GLIBCXX_CSTDIO |
|---|
| 16 | #include <cstdio> |
|---|
| 17 | #endif |
|---|
| 18 | #ifndef ARBTOOLS_H |
|---|
| 19 | #include "arbtools.h" |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class SuppressOutput : virtual Noncopyable { |
|---|
| 24 | // temporarily redirect stdout and stderr to /dev/null |
|---|
| 25 | FILE *devnull; |
|---|
| 26 | FILE *org_stdout; |
|---|
| 27 | FILE *org_stderr; |
|---|
| 28 | |
|---|
| 29 | void flush() { |
|---|
| 30 | fflush(stdout); |
|---|
| 31 | fflush(stderr); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | public: |
|---|
| 35 | void suppress() { |
|---|
| 36 | if (stdout != devnull) { |
|---|
| 37 | flush(); |
|---|
| 38 | stdout = devnull; |
|---|
| 39 | stderr = devnull; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | void dont_suppress() { |
|---|
| 43 | if (stdout == devnull) { |
|---|
| 44 | flush(); |
|---|
| 45 | stdout = org_stdout; |
|---|
| 46 | stderr = org_stderr; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | SuppressOutput() : |
|---|
| 51 | devnull(fopen("/dev/null", "w")), |
|---|
| 52 | org_stdout(stdout), |
|---|
| 53 | org_stderr(stderr) |
|---|
| 54 | { |
|---|
| 55 | suppress(); |
|---|
| 56 | } |
|---|
| 57 | ~SuppressOutput() { |
|---|
| 58 | dont_suppress(); |
|---|
| 59 | fclose(devnull); |
|---|
| 60 | } |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | #else |
|---|
| 64 | #error SuppressOutput.h included twice |
|---|
| 65 | #endif // SUPPRESSOUTPUT_H |
|---|
Note: See
TracBrowser
for help on using the repository browser.