CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes

CloseCoutSentry Class Reference

#include <CloseCoutSentry.h>

List of all members.

Public Member Functions

void clear ()
 CloseCoutSentry (bool silent=true)
FILE * trueStdOut ()
 ~CloseCoutSentry ()

Static Public Member Functions

static void breakFree ()

Static Private Member Functions

static void reallyClear ()

Private Attributes

static int fdErr_ = 0
bool silent_
bool stdOutIsMine_

Static Private Attributes

static int fdOut_ = 0
static bool open_ = true
static FILE * trueStdOut_ = 0

Detailed Description

This class redirects cout and cerr to /dev/null when created, and restores them back when destroyed.

Definition at line 7 of file CloseCoutSentry.h.


Constructor & Destructor Documentation

CloseCoutSentry::CloseCoutSentry ( bool  silent = true)

Definition at line 11 of file CloseCoutSentry.cc.

References fdErr_, fdOut_, open_, and silent_.

                                            :
    silent_(silent), stdOutIsMine_(false)
{
    if (silent_) {
        if (open_) {
            open_ = false;
            if (fdOut_ == 0 && fdErr_ == 0) {
                fdOut_ = dup(1);
                fdErr_ = dup(2);
            }
            freopen("/dev/null", "w", stdout);
            freopen("/dev/null", "w", stderr);
        } else {
            silent_ = false; 
        }
    }
}
CloseCoutSentry::~CloseCoutSentry ( )

Definition at line 29 of file CloseCoutSentry.cc.

References clear().

{
    clear();
}

Member Function Documentation

void CloseCoutSentry::breakFree ( ) [static]

Definition at line 54 of file CloseCoutSentry.cc.

References reallyClear().

{
    reallyClear();
}
void CloseCoutSentry::clear ( void  )
void CloseCoutSentry::reallyClear ( ) [static, private]

Definition at line 43 of file CloseCoutSentry.cc.

References fdErr_, fdOut_, and open_.

Referenced by breakFree(), and clear().

{
    if (fdOut_ != fdErr_) {
        char buf[50];
        sprintf(buf, "/dev/fd/%d", fdOut_); freopen(buf, "w", stdout);
        sprintf(buf, "/dev/fd/%d", fdErr_); freopen(buf, "w", stderr);
        open_   = true;
        fdOut_ = fdErr_ = 0; 
    }
}
FILE * CloseCoutSentry::trueStdOut ( )

Definition at line 59 of file CloseCoutSentry.cc.

References fdOut_, open_, stdOutIsMine_, and trueStdOut_.

Referenced by MultiDimFit::doGrid().

{
    if (open_) return stdout;
    if (trueStdOut_) return trueStdOut_;
    stdOutIsMine_ = true;
    char buf[50];
    sprintf(buf, "/dev/fd/%d", fdOut_); trueStdOut_ = fopen(buf, "w");
    return trueStdOut_;
}

Member Data Documentation

int CloseCoutSentry::fdErr_ = 0 [private]

Definition at line 18 of file CloseCoutSentry.h.

Referenced by CloseCoutSentry(), and reallyClear().

int CloseCoutSentry::fdOut_ = 0 [static, private]

Definition at line 18 of file CloseCoutSentry.h.

Referenced by CloseCoutSentry(), reallyClear(), and trueStdOut().

bool CloseCoutSentry::open_ = true [static, private]

Definition at line 19 of file CloseCoutSentry.h.

Referenced by CloseCoutSentry(), reallyClear(), and trueStdOut().

bool CloseCoutSentry::silent_ [private]

Definition at line 17 of file CloseCoutSentry.h.

Referenced by clear(), and CloseCoutSentry().

Definition at line 23 of file CloseCoutSentry.h.

Referenced by clear(), and trueStdOut().

FILE * CloseCoutSentry::trueStdOut_ = 0 [static, private]

Definition at line 22 of file CloseCoutSentry.h.

Referenced by clear(), and trueStdOut().