ROOT
v6-32
Reference Guide
Loading...
Searching...
No Matches
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
/** \class TGRedirectOutputGuard
14
\ingroup guiwidgets
15
16
This class provides output redirection to a TGTextView in guaranteed
17
exception safe way. Use like this:
18
19
```
20
{
21
TGRedirectOutputGuard guard(textview);
22
... // do something
23
guard.Update();
24
... // do something else
25
}
26
```
27
28
when guard goes out of scope, Update() is called to flush what left
29
on the screed and the output is automatically redirected again to
30
the standard units.
31
The exception mechanism takes care of calling the dtors
32
of local objects so it is exception safe.
33
Optionally the output can also be saved into a file:
34
35
```
36
{
37
TGRedirectOutputGuard guard(textview, file, mode);
38
... // do something
39
}
40
```
41
42
*/
43
44
45
#include <errno.h>
46
#include <sys/types.h>
47
#ifdef WIN32
48
# include <io.h>
49
#else
50
# include <unistd.h>
51
#endif
52
53
#include "
TError.h
"
54
#include "
TGRedirectOutputGuard.h
"
55
#include "
TGTextView.h
"
56
#include "
TSystem.h
"
57
58
////////////////////////////////////////////////////////////////////////////////
59
/// Create output redirection guard.
60
/// The TGTextView instance should be initialized outside.
61
/// Text is added to the existing text in the frame.
62
/// If defined, 'flog' is interpreted as the path of a file
63
/// where to save the output; in such a case 'mode' if the
64
/// opening mode of the file (either "w" or "a").
65
/// By default a temporary file is used.
66
67
TGRedirectOutputGuard::TGRedirectOutputGuard
(
TGTextView
*tv,
68
const
char
*flog,
69
const
char
*
mode
)
70
{
71
fTextView
= tv;
72
fLogFile
= flog;
73
fTmpFile
=
kFALSE
;
74
fLogFileRead
= 0;
75
if
(!flog) {
76
// Create temporary file
77
fLogFile
=
"RedirOutputGuard_"
;
78
fLogFileRead
=
gSystem
->TempFileName(
fLogFile
);
79
if
(!
fLogFileRead
) {
80
Error
(
"TGRedirectOutputGuard"
,
"could create temp file"
);
81
return
;
82
}
83
fTmpFile
=
kTRUE
;
84
85
// We need it in read mode
86
fclose(
fLogFileRead
);
87
}
else
{
88
// Check permissions, if existing
89
if
(!
gSystem
->AccessPathName(flog,
kFileExists
)) {
90
if
(
gSystem
->AccessPathName(flog,
91
(
EAccessMode
)(
kWritePermission
|
kReadPermission
))) {
92
Error
(
"TGRedirectOutputGuard"
,
93
"no write or read permission on file: %s"
, flog);
94
return
;
95
}
96
}
97
}
98
99
// Redirect
100
const
char
*
m
= (
mode
[0] !=
'a'
&&
mode
[0] !=
'w'
) ?
"a"
:
mode
;
101
if
(
gSystem
->RedirectOutput(
fLogFile
.Data(),
m
) == -1) {
102
Error
(
"TGRedirectOutputGuard"
,
"could not redirect output"
);
103
return
;
104
}
105
106
// Open file in read mode
107
if
((
fLogFileRead
= fopen(
fLogFile
.Data(),
"r"
))) {
108
// Start reading from the present end
109
lseek(fileno(
fLogFileRead
),(off_t)0, SEEK_END);
110
}
else
{
111
Error
(
"TGRedirectOutputGuard"
,
"could not open file in read mode"
);
112
return
;
113
}
114
115
return
;
116
}
117
118
////////////////////////////////////////////////////////////////////////////////
119
/// Destructor.
120
121
TGRedirectOutputGuard::~TGRedirectOutputGuard
()
122
{
123
// Display last info
124
Update
();
125
126
// Close the file
127
if
(
fLogFileRead
)
128
fclose(
fLogFileRead
);
129
130
// Unlink the file if we are the owners
131
if
(
fTmpFile
&&
fLogFile
.Length() > 0)
132
gSystem
->Unlink(
fLogFile
);
133
134
// Restore standard output
135
gSystem
->RedirectOutput(0);
136
}
137
138
////////////////////////////////////////////////////////////////////////////////
139
/// Send to text frame the undisplayed content of the file.
140
141
void
TGRedirectOutputGuard::Update
()
142
{
143
if
(!
fTextView
) {
144
Warning
(
"Update"
,
"no TGTextView defined"
);
145
return
;
146
}
147
148
if
(!
fLogFileRead
) {
149
Warning
(
"Update"
,
"no file open for reading"
);
150
return
;
151
}
152
153
// Make sure you get anything
154
fflush(stdout);
155
156
char
line
[4096];
157
while
(fgets(
line
,
sizeof
(
line
),
fLogFileRead
)) {
158
159
// Get read of carriage return
160
if
(
line
[strlen(
line
)-1] ==
'\n'
)
161
line
[strlen(
line
)-1] = 0;
162
163
// Send line to the TGTextView
164
fTextView
->AddLine(
line
);
165
}
166
}
kFALSE
constexpr Bool_t kFALSE
Definition
RtypesCore.h:101
kTRUE
constexpr Bool_t kTRUE
Definition
RtypesCore.h:100
TError.h
Error
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition
TError.cxx:185
Warning
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition
TError.cxx:229
TGRedirectOutputGuard.h
TGTextView.h
mode
Option_t Option_t TPoint TPoint const char mode
Definition
TGWin32VirtualXProxy.cxx:68
TSystem.h
EAccessMode
EAccessMode
Definition
TSystem.h:51
kFileExists
@ kFileExists
Definition
TSystem.h:52
kReadPermission
@ kReadPermission
Definition
TSystem.h:55
kWritePermission
@ kWritePermission
Definition
TSystem.h:54
gSystem
R__EXTERN TSystem * gSystem
Definition
TSystem.h:566
TGRedirectOutputGuard::Update
void Update()
Send to text frame the undisplayed content of the file.
Definition
TGRedirectOutputGuard.cxx:141
TGRedirectOutputGuard::TGRedirectOutputGuard
TGRedirectOutputGuard(const TGRedirectOutputGuard &)=delete
TGRedirectOutputGuard::fLogFileRead
FILE * fLogFileRead
Definition
TGRedirectOutputGuard.h:26
TGRedirectOutputGuard::fTmpFile
Bool_t fTmpFile
Definition
TGRedirectOutputGuard.h:24
TGRedirectOutputGuard::~TGRedirectOutputGuard
virtual ~TGRedirectOutputGuard()
Destructor.
Definition
TGRedirectOutputGuard.cxx:121
TGRedirectOutputGuard::fLogFile
TString fLogFile
Definition
TGRedirectOutputGuard.h:23
TGRedirectOutputGuard::fTextView
TGTextView * fTextView
Definition
TGRedirectOutputGuard.h:25
TGTextView
A TGTextView is a text viewer widget.
Definition
TGTextView.h:22
line
TLine * line
Definition
entrylistblock_figure1.C:235
m
TMarker m
Definition
textangle.C:8
gui
gui
src
TGRedirectOutputGuard.cxx
ROOT v6-32 - Reference Guide Generated on Tue May 19 2026 02:46:52 (GVA Time) using Doxygen 1.13.2