Logo ROOT  
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/** \class TGRedirectOutputGuard
14 \ingroup guiwidgets
15
16This class provides output redirection to a TGTextView in guaranteed
17exception 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
28when guard goes out of scope, Update() is called to flush what left
29on the screed and the output is automatically redirected again to
30the standard units.
31The exception mechanism takes care of calling the dtors
32of local objects so it is exception safe.
33Optionally 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"
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
68 const char *flog,
69 const char *mode)
70{
71 fTextView = tv;
72 fLogFile = flog;
74 fLogFileRead = 0;
75 if (!flog) {
76 // Create temporary file
77 fLogFile = "RedirOutputGuard_";
79 if (!fLogFileRead) {
80 Error("TGRedirectOutputGuard", "could create temp file");
81 return;
82 }
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,
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
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)
133
134 // Restore standard output
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Send to text frame the undisplayed content of the file.
140
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
165 }
166}
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:188
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition: TError.cxx:232
Option_t Option_t TPoint TPoint const char mode
EAccessMode
Definition: TSystem.h:43
@ kFileExists
Definition: TSystem.h:44
@ kReadPermission
Definition: TSystem.h:47
@ kWritePermission
Definition: TSystem.h:46
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
void Update()
Send to text frame the undisplayed content of the file.
TGRedirectOutputGuard(const TGRedirectOutputGuard &)=delete
virtual ~TGRedirectOutputGuard()
Destructor.
A TGTextView is a text viewer widget.
Definition: TGTextView.h:22
virtual void AddLine(const char *string)
Add a line of text to the view widget.
Definition: TGTextView.cxx:229
Ssiz_t Length() const
Definition: TString.h:410
const char * Data() const
Definition: TString.h:369
virtual Int_t RedirectOutput(const char *name, const char *mode="a", RedirectHandle_t *h=nullptr)
Redirect standard output (stdout, stderr) to the specified file.
Definition: TSystem.cxx:1716
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:1299
virtual FILE * TempFileName(TString &base, const char *dir=nullptr)
Create a secure temporary file by appending a unique 6 letter string to base.
Definition: TSystem.cxx:1500
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1384
TLine * line
TMarker m
Definition: textangle.C:8