ROOT
v6-30
Reference Guide
Loading...
Searching...
No Matches
coutCapture.h
Go to the documentation of this file.
1
/*
2
* Project: xRooFit
3
* Author:
4
* Will Buttinger, RAL 2022
5
*
6
* Copyright (c) 2022, CERN
7
*
8
* Redistribution and use in source and binary forms,
9
* with or without modification, are permitted according to the terms
10
* listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11
*/
12
13
#include "
TSystem.h
"
14
#include "
TUUID.h
"
15
#include <fstream>
16
17
struct
cout_redirect
{
18
cout_redirect
(std::string &_out,
size_t
bufSize = 102 * 1024)
19
:
out
(_out),
filename
{
"xRooFit-logging-"
},
fBufSize
(bufSize)
20
{
21
old
= std::cout.rdbuf(
buffer
.rdbuf());
22
old2
= std::cerr.rdbuf(
buffer
.rdbuf());
23
// buffer2 = (char *)calloc(sizeof(char), bufSize);fp = fmemopen(buffer2, bufSize, "w");
24
fp
=
gSystem
->
TempFileName
(
filename
);
25
if
(
fp
) {
26
stdout =
fp
;
27
stderr =
fp
;
28
}
29
}
30
~cout_redirect
()
31
{
32
std::cout.rdbuf(
old
);
33
std::cerr.rdbuf(
old2
);
34
stdout =
old3
;
35
stderr =
old4
;
36
if
(
fp
) {
37
std::fclose(
fp
);
38
{
39
std::ifstream t(
filename
);
40
buffer
<< t.rdbuf();
41
}
42
gSystem
->
Unlink
(
filename
);
// delete the temp file
43
}
44
out
=
buffer
.str();
45
if
(
buffer2
) {
46
out
+=
buffer2
;
47
free
(
buffer2
);
48
}
49
if
(
out
.length() >
fBufSize
)
50
out
.resize(
fBufSize
);
51
}
52
53
private
:
54
std::streambuf *
old
;
55
std::streambuf *
old2
;
56
std::stringstream
buffer
;
57
char
*
buffer2
=
nullptr
;
58
FILE *
fp
=
nullptr
;
59
FILE *
old3
= stdout;
60
FILE *
old4
= stdout;
61
std::string &
out
;
62
TString
filename
;
63
size_t
fBufSize
;
64
};
TSystem.h
gSystem
R__EXTERN TSystem * gSystem
Definition
TSystem.h:560
TUUID.h
free
#define free
Definition
civetweb.c:1539
TString
Basic string class.
Definition
TString.h:139
TSystem::TempFileName
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:1484
TSystem::Unlink
virtual int Unlink(const char *name)
Unlink, i.e.
Definition
TSystem.cxx:1368
cout_redirect
Definition
coutCapture.h:17
cout_redirect::old2
std::streambuf * old2
Definition
coutCapture.h:55
cout_redirect::old3
FILE * old3
Definition
coutCapture.h:59
cout_redirect::cout_redirect
cout_redirect(std::string &_out, size_t bufSize=102 *1024)
Definition
coutCapture.h:18
cout_redirect::old
std::streambuf * old
Definition
coutCapture.h:54
cout_redirect::buffer
std::stringstream buffer
Definition
coutCapture.h:56
cout_redirect::~cout_redirect
~cout_redirect()
Definition
coutCapture.h:30
cout_redirect::old4
FILE * old4
Definition
coutCapture.h:60
cout_redirect::buffer2
char * buffer2
Definition
coutCapture.h:57
cout_redirect::fp
FILE * fp
Definition
coutCapture.h:58
cout_redirect::filename
TString filename
Definition
coutCapture.h:62
cout_redirect::out
std::string & out
Definition
coutCapture.h:61
cout_redirect::fBufSize
size_t fBufSize
Definition
coutCapture.h:63
roofit
xroofit
src
coutCapture.h
ROOT v6-30 - Reference Guide Generated on Thu Dec 12 2024 06:12:26 (GVA Time) using Doxygen 1.9.8