-
Notifications
You must be signed in to change notification settings - Fork 23
/
Logger.h
198 lines (145 loc) · 7.3 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Logger.h
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* This file is part of: freeture
*
* Copyright: (C) 2014-2015 Yoan Audureau
* FRIPON-GEOPS-UPSUD-CNRS
*
* License: GNU General Public License
*
* FreeTure is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* FreeTure is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with FreeTure. If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 20/07/2015
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/**
* \file Logger.h
* \author Yoan Audureau -- FRIPON-GEOPS-UPSUD
* \version 1.0
* \date 03/06/2014
*/
#pragma once
#include <boost/filesystem.hpp>
#include <string>
#include <numeric>
#include "TimeDate.h"
#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/bind.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include "TimeDate.h"
#include "Conversion.h"
class Logger {
private :
string mLogPath;
int mTimeLimit;
int mSizeLimit;
vector<string> mLogFiles;
vector<int> mRefDate;
public :
/**
* Constructor.
*
*/
Logger(string logPath, int timeLimit, int sizeLimit, vector<string> logFiles):
mLogPath(logPath), mTimeLimit(timeLimit), mSizeLimit(sizeLimit), mLogFiles(logFiles) {
mRefDate = TimeDate::splitStringToInt(TimeDate::localDateTime(microsec_clock::universal_time(),"%Y:%m:%d:%H:%M:%S"));
}
void monitorLog() {
vector<int> currDate = TimeDate::splitStringToInt(TimeDate::localDateTime(microsec_clock::universal_time(),"%Y:%m:%d:%H:%M:%S"));
//cout << "REFDATE : " << mRefDate.at(0) << mRefDate.at(1) << mRefDate.at(2) << endl;
//cout << "CURDATE : " << currDate.at(0) << currDate.at(1) << currDate.at(2) << endl;
// Create log date directories when date changes.
if(mRefDate.at(0) != currDate.at(0) || mRefDate.at(1) != currDate.at(1) || mRefDate.at(2) != currDate.at(2)) {
string rDate = Conversion::numbering(2, mRefDate.at(0)) + Conversion::intToString(mRefDate.at(0)) + Conversion::numbering(2, mRefDate.at(1)) + Conversion::intToString(mRefDate.at(1)) + Conversion::numbering(2, mRefDate.at(2)) + Conversion::intToString(mRefDate.at(2));
//cout << rDate << endl;
if(create_directory(path(mLogPath + "/LOG_" + rDate)) || exists(path(mLogPath + "/LOG_" + rDate))) {
//cout << mLogPath << "/LOG_" << rDate << " created." << endl;
for(int i = 0; i < mLogFiles.size(); i++) {
try {
rename(path(mLogPath + "/" + mLogFiles.at(i)), path(mLogPath + "/LOG_" + rDate + "/" + mLogFiles.at(i)));
//cout << "RENAME : " << mLogPath << "/" << mLogFiles.at(i) << " TO " << mLogPath << "/LOG_" << rDate << "/" + mLogFiles.at(i) << endl;
}catch(boost::filesystem::filesystem_error e) {
cout <<"filesystem error" << endl;
}
}
// Clean logs directories
boost::posix_time::ptime t1(boost::posix_time::from_iso_string(rDate + "T000000"));
int dirNb = 0;
vector<string> dirToRemove;
path pDir(mLogPath);
//cout << ">> LOOP DIR : "<< endl;
for(directory_iterator file(pDir);file!= directory_iterator(); ++file) {
path curr(file->path());
//cout << "-> " << curr << endl;
if(is_directory(curr)) {
string dirName = curr.filename().string();
vector<string> output;
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep("_");
tokenizer tokens(dirName, sep);
for (tokenizer::iterator tok_iter = tokens.begin();tok_iter != tokens.end(); ++tok_iter) {
output.push_back(*tok_iter);
}
if(output.size() == 2 && output.back().size() == 8) {
boost::posix_time::ptime t2(boost::posix_time::from_iso_string(output.back() + "T000000"));
boost::posix_time::time_duration td = t1 - t2;
long secTime = td.total_seconds();
//cout << secTime << endl;
if(abs(secTime) > mTimeLimit * 24 * 3600) {
dirToRemove.push_back(curr.string());
}
}else{
dirToRemove.push_back(curr.string());
//cout << "remove : " << curr.string() << endl;
}
}
}
//cout << ">> SIZE dirToRemove : " << dirToRemove.size() << endl;
for(int i = 0; i < dirToRemove.size(); i++) {
//cout << ">> REMOVE : " << dirToRemove.at(i) << endl;
boost::filesystem::remove_all(path(dirToRemove.at(i)));
}
mRefDate = currDate;
}/*else
cout << "DIR not exists" << endl;*/
}
// Check log size.
unsigned long long logSize = 0;
getFoldersize(mLogPath, logSize);
//cout << "LOG SIZE : " << logSize << endl;
if((logSize/1024.0)/1024.0 > mSizeLimit) {
boost::filesystem::path path_to_remove(mLogPath);
for (boost::filesystem::directory_iterator end_dir_it, it(path_to_remove); it!=end_dir_it; ++it) {
boost::filesystem::remove_all(it->path());
}
}
}
private :
void getFoldersize(string rootFolder,unsigned long long & f_size) {
path folderPath(rootFolder);
if (exists(folderPath)) {
directory_iterator end_itr;
for (directory_iterator dirIte(rootFolder); dirIte != end_itr; ++dirIte ) {
path filePath(complete (dirIte->path(), folderPath));
try{
if (!is_directory(dirIte->status()) ){
f_size = f_size + file_size(filePath);
}else{
getFoldersize(filePath.string(),f_size);
}
}catch(exception& e){ cout << e.what() << endl; }
}
}
}
};