-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlog_impl.hpp
More file actions
175 lines (159 loc) · 3.93 KB
/
Copy pathlog_impl.hpp
File metadata and controls
175 lines (159 loc) · 3.93 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef LOG_IMPL_HPP
#define LOG_IMPL_HPP
#include <string>
#include <mutex>
#include <sstream>
#include <vector>
#include <iostream>
#include <memory>
#include <exception>
#include <fstream>
#include <unistd.h>
#include <libproc.h>
namespace logging {
enum severity_type
{
debug = 1,
error,
warning
};
class log_policy_interface
{
public:
virtual void setup() = 0;
virtual void teardown() = 0;
virtual void write(const std::string& msg) = 0;
virtual ~log_policy_interface() {}
};
class file_log_policy : public log_policy_interface
{
std::string name;
std::shared_ptr<std::ofstream> out_stream;
std::string dir;
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
const char* getProcPath()
{
pid_t pid = getpid();
proc_pidpath (pid, file_log_policy::pathbuf, sizeof(file_log_policy::pathbuf));
return &pathbuf[0];
}
public:
file_log_policy(const std::string& _name) :
name(_name),
out_stream( new std::ofstream )
{
dir = getProcPath();
std::string::size_type pos;
if((pos = dir.find(".app",0,4)) == std::string::npos){
dir.erase(dir.begin()+dir.find_last_of('/')+1, dir.end());
} else {
dir.erase(dir.begin()+pos+5, dir.end());
}
}
void setup()
{
try{
out_stream->open( (dir+name).c_str(), std::ios_base::binary|std::ios_base::out );
} catch (std::exception) {
teardown();
}
}
void teardown()
{
if( out_stream ){
out_stream->close();
}
}
void write(const std::string& msg)
{
if(!out_stream->is_open())
return;
(*out_stream)<<msg<<std::endl;
}
~file_log_policy()
{
if( out_stream ){
teardown();
}
}
};
class logger
{
std::stringstream log_stream;
std::mutex write_mutex;
unsigned log_line_number;
std::vector<log_policy_interface*> policy;
std::string get_time()
{
std::string time_str;
time_t raw_time;
time( & raw_time );
time_str = ctime( &raw_time );
//without the newline character
return time_str.substr( 0 , time_str.size() - 1 );
}
std::string get_logline_header()
{
std::stringstream header;
header.str("");
header.fill('0');
header.width(7);
header << log_line_number++ <<" < "<<get_time()<<" - ";
header.fill('0');
header.width(7);
header <<clock()<<" > ~ ";
return header.str();
}
//Core printing functionality
void print_impl()
{
for(std::vector<log_policy_interface* >::iterator iter = policy.begin(); iter != policy.end(); iter++){
(*iter)->write( get_logline_header() + log_stream.str() );
}
log_stream.str("");
}
template<typename First, typename...Rest >
void print_impl(First parm1, Rest...parm)
{
log_stream<<parm1;
print_impl(parm...);
}
public:
logger()
{
log_line_number = 0;
}
~logger()
{
for(std::vector<log_policy_interface* >::iterator iter = policy.begin(); iter != policy.end(); iter++){
(*iter)->teardown();
delete *iter;
}
}
template < severity_type severity , typename...Args >
void print( Args...args )
{
write_mutex.lock();
switch( severity )
{
case severity_type::debug:
log_stream<<"<DEBUG> :";
break;
case severity_type::warning:
log_stream<<"<WARNING> :";
break;
case severity_type::error:
log_stream<<"<ERROR> :";
break;
};
print_impl( args... );
write_mutex.unlock();
}
void register_policy(log_policy_interface *policy_)
{
policy_->setup();
policy.push_back(policy_);
}
};
} //namespace logging
#endif // LOG_HPP