ROOT
6.12/07
Reference Guide
gui
gui
src
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
59
TGRedirectOutputGuard::TGRedirectOutputGuard
(
TGTextView
*tv,
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_"
;
70
fLogFileRead
=
gSystem
->
TempFileName
(
fLogFile
);
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,
83
(
EAccessMode
)(
kWritePermission
|
kReadPermission
))) {
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
113
TGRedirectOutputGuard::~TGRedirectOutputGuard
()
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)
124
gSystem
->
Unlink
(
fLogFile
);
125
126
// Restore standard output
127
gSystem
->
RedirectOutput
(0);
128
}
129
130
////////////////////////////////////////////////////////////////////////////////
131
/// Send to text frame the undisplayed content of the file.
132
133
void
TGRedirectOutputGuard::Update
()
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
}
TSystem::AccessPathName
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:1276
kFileExists
Definition:
TSystem.h:45
m
auto * m
Definition:
textangle.C:8
TGRedirectOutputGuard::TGRedirectOutputGuard
TGRedirectOutputGuard(const TGRedirectOutputGuard &)
line
TLine * line
Definition:
entrylistblock_figure1.C:235
TGTextView
Definition:
TGTextView.h:33
TGTextView.h
kWritePermission
Definition:
TSystem.h:47
TSystem::Unlink
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition:
TSystem.cxx:1357
TSystem::TempFileName
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:1473
TSystem.h
TGTextView::AddLine
virtual void AddLine(const char *string)
Add a line of text to the view widget.
Definition:
TGTextView.cxx:227
flog
TString flog
Definition:
pq2main.cxx:37
TGRedirectOutputGuard::fTextView
TGTextView * fTextView
Definition:
TGRedirectOutputGuard.h:49
Error
void Error(const char *location, const char *msgfmt,...)
gSystem
R__EXTERN TSystem * gSystem
Definition:
TSystem.h:540
TError.h
TString::Length
Ssiz_t Length() const
Definition:
TString.h:386
Warning
void Warning(const char *location, const char *msgfmt,...)
kFALSE
const Bool_t kFALSE
Definition:
RtypesCore.h:88
TSystem::RedirectOutput
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:1688
TGRedirectOutputGuard::Update
void Update()
Send to text frame the undisplayed content of the file.
Definition:
TGRedirectOutputGuard.cxx:133
TGRedirectOutputGuard::fLogFileRead
FILE * fLogFileRead
Definition:
TGRedirectOutputGuard.h:50
TGRedirectOutputGuard::fTmpFile
Bool_t fTmpFile
Definition:
TGRedirectOutputGuard.h:48
kReadPermission
Definition:
TSystem.h:48
EAccessMode
EAccessMode
Definition:
TSystem.h:44
TGRedirectOutputGuard::~TGRedirectOutputGuard
virtual ~TGRedirectOutputGuard()
Destructor.
Definition:
TGRedirectOutputGuard.cxx:113
TGRedirectOutputGuard::fLogFile
TString fLogFile
Definition:
TGRedirectOutputGuard.h:47
TGRedirectOutputGuard.h
kTRUE
const Bool_t kTRUE
Definition:
RtypesCore.h:87
TString::Data
const char * Data() const
Definition:
TString.h:345