-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.h
75 lines (62 loc) · 1.78 KB
/
Logger.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
/************************************************************
* SimLib simulation library for event-based simulations *
* Author: Martin Ubl (A16N0026P) *
************************************************************/
#pragma once
#include <iostream>
#include <fstream>
#include "Types.h"
class Logger;
/*
* Logger line guard created as "RAII" structure for putting endline after write end
*/
struct LoggerLineGuard
{
LoggerLineGuard(Logger& logger);
~LoggerLineGuard();
// "transparent" logging operator, pass directly to logger
template<typename T>
LoggerLineGuard& operator<<(T const& value)
{
m_logger.Write(value);
return *this;
}
Logger& m_logger;
};
/*
* Simulation logger class
*/
class Logger
{
public:
Logger(std::ostream& outFile);
virtual ~Logger();
// "transparent" logging function to pass inputs to output stream directly
template<typename T>
LoggerLineGuard operator<<(T const& value)
{
m_outputFile << value;
return LoggerLineGuard(*this);
}
// simulation time logger bridge
LoggerLineGuard operator()(const simtime_t time)
{
m_outputFile << "[" << time << "] ";
return LoggerLineGuard(*this);
}
// raw write to log
template<typename T>
void Write(T const& value)
{
m_outputFile << value;
}
// end of line
static constexpr auto endl()
{
return '\n';
}
protected:
// output file used
std::ostream& m_outputFile;
};