-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogFile.h
More file actions
117 lines (101 loc) · 3.5 KB
/
LogFile.h
File metadata and controls
117 lines (101 loc) · 3.5 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// logfile.h
// logfile class for easy and automatic store information in log file
#pragma once
#ifndef LOG_FILE_H_
#define LOG_FILE_H_
#include <fstream>
#include <string>
#include <ctime>
#include <chrono>
template<class _Elem>
class logfile : private std::basic_ofstream<_Elem> {
private:
static const char * START_LOG;
static const char * END_LOG;
unsigned int counter;
public:
// constructors, operator=()
logfile() : std::basic_ofstream<_Elem>() {}
template<class _El>
explicit logfile(const _El * filename);
template<class _El>
explicit logfile(const std::basic_string<_El> & filename);
logfile(logfile && lf) : std::basic_ofstream<_Elem>(std::move(lf)), counter(lf.counter) {}
logfile(const logfile &) = delete;
logfile & operator=(const logfile &) = delete;
logfile & operator=(logfile && lf);
~logfile();
// open()
template<class _El>
void open(const _El * filename);
template<class _El>
void open(const std::basic_string<_El> & filename);
using std::basic_ofstream<_Elem>::is_open;
using std::basic_ofstream<_Elem>::close;
// operator<<()
template<typename _Val>
logfile & operator<<(const _Val & val) {
static_cast<std::basic_ostream<_Elem> &>(*this) << val;
return *this;
}
logfile & operator<<(std::basic_ostream<_Elem> & (*manip)(std::basic_ostream<_Elem> &)) {
static_cast<std::basic_ostream<_Elem> &>(*this) << manip;
return *this;
}
logfile & operator<<(logfile & (*manip)(logfile &)) { return manip(*this); }
// manipulator
friend logfile & newdata(logfile & lf) {
lf.counter++;
lf << std::right << std::setw(2) << lf.counter << ") ";
return lf;
}
};
// strings
template<class _Elem>
const char * logfile<_Elem>::START_LOG =
">----------------------------------------------------------- START LOG ----------------------------------------------------------->";
template<class _Elem>
const char * logfile<_Elem>::END_LOG =
">------------------------------------------------------------ END LOG ------------------------------------------------------------>";
// constructors, operator=
template<class _Elem>
template<class _El>
logfile<_Elem>::logfile(const _El * filename) : std::basic_ofstream<_Elem>(filename), counter(0) {
using std::chrono::system_clock;
system_clock::time_point time_now = system_clock::now();
std::time_t time_now_t = system_clock::to_time_t(time_now);
*this << "Name of logfile: " << filename << std::endl
<< "Creation time: " << ctime(&time_now_t) << std::endl
<< "Srart of log:\n" << START_LOG << std::endl;
}
template<class _Elem>
template<class _El>
logfile<_Elem>::logfile(const std::basic_string<_El> & filename) : logfile(filename.c_str()) {}
template<class _Elem>
logfile<_Elem> & logfile<_Elem>::operator=(logfile && lf) {
std::basic_ofstream<_Elem>::operator=(std::move(lf));
counter = lf.counter;
return *this;
}
// open
template<class _Elem>
template<class _El>
void logfile<_Elem>::open(const _El * filename) {
counter = 0;
using std::chrono::system_clock;
system_clock::time_point time_now = system_clock::now();
std::time_t time_now_t = system_clock::to_time_t(time_now);
*this << "Name of logfile: " << filename << std::endl
<< "Creation time: " << ctime(&time_now_t) << std::endl
<< "Srart of log:\n" << START_LOG << std::endl;
}
template<class _Elem>
template<class _El>
void logfile<_Elem>::open(const std::basic_string<_El> & filename) { open(filename.c_str()); }
// destructor
template<class _Elem>
logfile<_Elem>::~logfile() {
*this << END_LOG << std::endl
<< "Number of logged files: " << counter << std::endl;
}
#endif