Logo ROOT   6.10/09
Reference Guide
TGRedirectOutputGuard.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: G. Ganis 10/10/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TGRedirectOutputGuard //
15 // //
16 // This class provides output redirection to a TGTextView in guaranteed //
17 // exception safe way. Use like this: //
18 // { //
19 // TGRedirectOutputGuard guard(textview); //
20 // ... // do something //
21 // guard.Update(); //
22 // ... // do something else //
23 // } //
24 // when guard goes out of scope, Update() is called to flush what left //
25 // on the screed and the output is automatically redirected again to //
26 // the standard units. //
27 // The exception mechanism takes care of calling the dtors //
28 // of local objects so it is exception safe. //
29 // Optionally the output can also be saved into a file: //
30 // { //
31 // TGRedirectOutputGuard guard(textview, file, mode); //
32 // ... // do something //
33 // } //
34 // //
35 //////////////////////////////////////////////////////////////////////////
36 
37 #include <errno.h>
38 #include <sys/types.h>
39 #ifdef WIN32
40 # include <io.h>
41 #else
42 # include <unistd.h>
43 #endif
44 
45 #include "TError.h"
46 #include "TGRedirectOutputGuard.h"
47 #include "TGTextView.h"
48 #include "TSystem.h"
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Create output redirection guard.
52 /// The TGTextView instance should be initialized outside.
53 /// Text is added to the existing text in the frame.
54 /// If defined, 'flog' is interpreted as the path of a file
55 /// where to save the output; in such a case 'mode' if the
56 /// opening mode of the file (either "w" or "a").
57 /// By default a temporary file is used.
58 
60  const char *flog,
61  const char *mode)
62 {
63  fTextView = tv;
64  fLogFile = flog;
65  fTmpFile = kFALSE;
66  fLogFileRead = 0;
67  if (!flog) {
68  // Create temporary file
69  fLogFile = "RedirOutputGuard_";
71  if (!fLogFileRead) {
72  Error("TGRedirectOutputGuard", "could create temp file");
73  return;
74  }
75  fTmpFile = kTRUE;
76 
77  // We need it in read mode
78  fclose(fLogFileRead);
79  } else {
80  // Check permissions, if existing
81  if (!gSystem->AccessPathName(flog, kFileExists)) {
82  if (gSystem->AccessPathName(flog,
84  Error("TGRedirectOutputGuard",
85  "no write or read permission on file: %s", flog);
86  return;
87  }
88  }
89  }
90 
91  // Redirect
92  const char *m = (mode[0] != 'a' && mode[0] != 'w') ? "a" : mode;
93  if (gSystem->RedirectOutput(fLogFile.Data(), m) == -1) {
94  Error("TGRedirectOutputGuard","could not redirect output");
95  return;
96  }
97 
98  // Open file in read mode
99  if ((fLogFileRead = fopen(fLogFile.Data(),"r"))) {
100  // Start reading from the present end
101  lseek(fileno(fLogFileRead),(off_t)0, SEEK_END);
102  } else {
103  Error("TGRedirectOutputGuard","could not open file in read mode");
104  return;
105  }
106 
107  return;
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Destructor.
112 
114 {
115  // Display last info
116  Update();
117 
118  // Close the file
119  if (fLogFileRead)
120  fclose(fLogFileRead);
121 
122  // Unlink the file if we are the owners
123  if (fTmpFile && fLogFile.Length() > 0)
125 
126  // Restore standard output
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Send to text frame the undisplayed content of the file.
132 
134 {
135  if (!fTextView) {
136  Warning("Update","no TGTextView defined");
137  return;
138  }
139 
140  if (!fLogFileRead) {
141  Warning("Update","no file open for reading");
142  return;
143  }
144 
145  // Make sure you get anything
146  fflush(stdout);
147 
148  char line[4096];
149  while (fgets(line,sizeof(line),fLogFileRead)) {
150 
151  // Get read of carriage return
152  if (line[strlen(line)-1] == '\n')
153  line[strlen(line)-1] = 0;
154 
155  // Send line to the TGTextView
156  fTextView->AddLine(line);
157  }
158 }
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1272
TGRedirectOutputGuard(const TGRedirectOutputGuard &)
TLine * line
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1353
virtual FILE * TempFileName(TString &base, const char *dir=0)
Create a secure temporary file by appending a unique 6 letter string to base.
Definition: TSystem.cxx:1469
virtual void AddLine(const char *string)
Add a line of text to the view widget.
Definition: TGTextView.cxx:227
TString flog
Definition: pq2main.cxx:37
void Error(const char *location, const char *msgfmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
TMarker * m
Definition: textangle.C:8
Ssiz_t Length() const
Definition: TString.h:388
void Warning(const char *location, const char *msgfmt,...)
const Bool_t kFALSE
Definition: RtypesCore.h:92
virtual Int_t RedirectOutput(const char *name, const char *mode="a", RedirectHandle_t *h=0)
Redirect standard output (stdout, stderr) to the specified file.
Definition: TSystem.cxx:1684
void Update()
Send to text frame the undisplayed content of the file.
EAccessMode
Definition: TSystem.h:44
virtual ~TGRedirectOutputGuard()
Destructor.
const Bool_t kTRUE
Definition: RtypesCore.h:91
const char * Data() const
Definition: TString.h:347