-
Notifications
You must be signed in to change notification settings - Fork 1
/
miniLog.hpp
91 lines (74 loc) · 2.26 KB
/
miniLog.hpp
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
#pragma once
#include<string>
#include<boost/filesystem.hpp>
#include<boost/filesystem/fstream.hpp>
#include<chrono>
#include<iostream>
#include<iomanip>
namespace fs = boost::filesystem;
namespace miniLog{
enum miniMessage_T {status, warning, error};
struct miniMessage{
std::chrono::system_clock::time_point messageTime;
miniMessage_T messageType;
std::string message;
miniMessage(miniMessage_T mType, const std::string & mString)
:messageTime{std::chrono::system_clock::now()},
messageType{mType},
message{mString}
{
}
};
std::string getTimeStr(const std::chrono::time_point<std::chrono::system_clock> & intime){
std::time_t now{std::chrono::system_clock::to_time_t(intime)};
std::string s(22, '\0');
std::strftime(&s[0], s.size(), "%Y-%m-%d %H:%M:%S", std::localtime(&now));
return s;
}
std::string getTimeStr(const miniMessage & m){
return getTimeStr(m.messageTime);
}
template <typename T>
T& operator<< (T& ofs, const miniMessage & m){
ofs << getTimeStr(m);
switch(m.messageType){
case status:
ofs << "STATUS: ";
break;
case warning:
ofs << "WARNING: ";
break;
case error:
ofs << "ERROR: ";
break;
}
ofs << m.message;
return ofs;
}
struct miniLogger{
fs::path p;
miniLogger(const std::string & logpath, const std::string & Appname)
:p{logpath}
{
fs::ofstream ofs{p, fs::ofstream::out|fs::ofstream::app};
ofs << std::endl
<< "-------------------------------------------------" << std::endl
<< "++++++++++++ Welcome to the Mini-Log ++++++++++++" << std::endl
<< "++ Mini-Log was build by ++" << std::endl
<< "++ Stephan Bökelmann ++" << std::endl
<< "++ github: bjoekeldude ++" << std::endl
<< "++ 2018 - Ruhr-Uni-Bochum ++" << std::endl
<< "+++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl
<< getTimeStr(std::chrono::system_clock::now())
<< "Start of logging " << Appname << std::endl;
}
void log(const miniMessage & message){
fs::ofstream ofs{p,fs::ofstream::out|fs::ofstream::app};
ofs << message << std::endl;
}
miniLogger& operator<< (const miniMessage & message){
log(message);
return *this;
}
};
}