-
Notifications
You must be signed in to change notification settings - Fork 14
/
MongoLog.cc
132 lines (118 loc) · 4.01 KB
/
MongoLog.cc
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
#include "MongoLog.hh"
#include <iostream>
#include <chrono>
#include <bsoncxx/builder/stream/document.hpp>
MongoLog::MongoLog(int DeleteAfterDays, std::shared_ptr<mongocxx::pool>& pool, std::string dbname, std::string log_dir, std::string host) :
fPool(pool), fClient(pool->acquire()) {
fLogLevel = 0;
fHostname = host;
fDeleteAfterDays = DeleteAfterDays;
fFlushPeriod = 5; // seconds
fOutputDir = log_dir;
//fPool = pool;
//fClient = pool->acquire();
fDB = (*fClient)[dbname];
fCollection = fDB["log"];
std::cout<<"Configured WITH local file logging to " << log_dir << std::endl;
fFlush = true;
fFlushThread = std::thread(&MongoLog::Flusher, this);
fRunId = -1;
RotateLogFile();
fLogLevel = 1;
}
MongoLog::~MongoLog(){
fFlush = false;
fFlushThread.join();
fOutfile.close();
}
void MongoLog::Flusher() {
while (fFlush == true) {
std::this_thread::sleep_for(std::chrono::seconds(fFlushPeriod));
fMutex.lock();
if (fOutfile.is_open()) fOutfile << std::flush;
fMutex.unlock();
}
}
std::string MongoLog::FormatTime(struct tm* date) {
std::stringstream s;
s <<std::put_time(date, "%F %T");
return s.str();
}
int MongoLog::Today(struct tm* date) {
return (date->tm_year+1900)*10000 + (date->tm_mon+1)*100 + (date->tm_mday);
}
std::string MongoLog::LogFileName(struct tm* date) {
return std::to_string(Today(date)) + "_" + fHostname + ".log";
}
int MongoLog::RotateLogFile() {
if (fOutfile.is_open()) fOutfile.close();
auto t = std::time(0);
auto today = *std::gmtime(&t);
std::string filename = LogFileName(&today);
std::cout<<"Logging to " << fOutputDir/filename<<std::endl;
fOutfile.open(fOutputDir / filename, std::ofstream::out | std::ofstream::app);
if (!fOutfile.is_open()) {
std::cout << "Could not rotate logfile!\n";
return -1;
}
fOutfile << FormatTime(&today) << " [INIT]: logfile initialized\n";
fToday = Today(&today);
std::vector<int> days_per_month = {31,28,31,30,31,30,31,31,30,31,30,31};
if (today.tm_year%4 == 0) days_per_month[1] += 1; // the edge-case is SEP
struct tm last_week = today;
last_week.tm_mday -= fDeleteAfterDays;
if (last_week.tm_mday <= 0) { // new month
last_week.tm_mon--;
if (last_week.tm_mon < 0) { // new year
last_week.tm_year--;
last_week.tm_mon = 11;
}
last_week.tm_mday += days_per_month[last_week.tm_mon]; // off by one error???
}
std::experimental::filesystem::path p = fOutputDir/LogFileName(&last_week);
if (std::experimental::filesystem::exists(p)) {
fOutfile << FormatTime(&today) << " [INIT]: Deleting " << p << '\n';
std::experimental::filesystem::remove(p);
} else {
fOutfile << FormatTime(&today) << " [INIT]: No older logfile to delete :(\n";
}
return 0;
}
int MongoLog::Entry(int priority, std::string message, ...){
// Thanks Martin
// http://www.martinbroadhurst.com/string-formatting-in-c.html
va_list args;
va_start (args, message); // First pass just gets what the length will be
size_t len = std::vsnprintf(NULL, 0, message.c_str(), args);
va_end (args);
std::vector<char> vec(len + 1); // Declare with proper length
va_start (args, message); // Fill the vector we just made
std::vsnprintf(&vec[0], len + 1, message.c_str(), args);
va_end (args);
message = &vec[0];
auto t = std::time(nullptr);
auto tm = *std::gmtime(&t);
std::stringstream msg;
msg<<FormatTime(&tm)<<" ["<<fPriorities[priority+1] <<"]: "<<message<<std::endl;
std::unique_lock<std::mutex> lg(fMutex);
std::cout << msg.str();
if (Today(&tm) != fToday) RotateLogFile();
fOutfile<<msg.str();
if(priority >= fLogLevel){
try{
auto d = bsoncxx::builder::stream::document{} <<
"user" << fHostname <<
"message" << message <<
"priority" << priority <<
"runid" << fRunId <<
bsoncxx::builder::stream::finalize;
fCollection.insert_one(std::move(d));
}
catch(const std::exception &e){
std::cout<<"Failed to insert log message "<<message<<" ("<<
priority<<")"<<std::endl;
return -1;
}
}
return 0;
}