Logo ROOT  
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
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");
25 if (fp) {
26 stdout = fp;
27 stderr = fp;
28 }
29 }
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;
48 }
49 if (out.length() > fBufSize)
50 out.resize(fBufSize);
51 }
52
53private:
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;
63 size_t fBufSize;
64};
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
#define free
Definition civetweb.c:1539
Basic string class.
Definition TString.h:139
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
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1368
std::streambuf * old2
Definition coutCapture.h:55
cout_redirect(std::string &_out, size_t bufSize=102 *1024)
Definition coutCapture.h:18
std::streambuf * old
Definition coutCapture.h:54
std::stringstream buffer
Definition coutCapture.h:56
TString filename
Definition coutCapture.h:62
std::string & out
Definition coutCapture.h:61
size_t fBufSize
Definition coutCapture.h:63