forked from dcampora/cl_forward
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileStdLogger.h
81 lines (65 loc) · 1.53 KB
/
FileStdLogger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* FileStdLogger
*
* author - Daniel Campora
* email - [email protected]
*
* April, 2014
* CERN
*/
#ifndef FILESTDLOGGER
#define FILESTDLOGGER 1
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
// Dumb type, just making constructor public
class FileStdLogger : public std::ostream {
private:
std::streambuf* b;
public:
FileStdLogger() : std::ostream(b) {}
};
// Can't believe this is so simple
class MessageLogger : public std::streambuf {
public:
std::string _buf;
std::ofstream* _file_io;
FileStdLogger* _file_std_io;
std::streambuf* _old;
MessageLogger(std::ofstream* file_io, FileStdLogger* file_std_io) :
_buf(""), _file_io(file_io), _file_std_io(file_std_io) {
// Override the previous read buffer
_old = _file_std_io->rdbuf(this);
}
~MessageLogger(){
_file_std_io->rdbuf(_old);
}
int overflow(int c){
if (c == '\n'){
std::cout << _buf << std::endl;
(*_file_io) << _buf << std::endl;
_buf = "";
}
else
_buf += c;
return c;
}
};
class VoidLogger : public std::streambuf {
public:
std::ostream* _void_stream;
std::streambuf* _old;
VoidLogger(std::ostream* void_stream) :
_void_stream(void_stream) {
_old = _void_stream->rdbuf(this);
}
~VoidLogger(){
_void_stream->rdbuf(_old);
}
int overflow(int c){
// Just don't do anything
return c;
}
};
#endif