-
Notifications
You must be signed in to change notification settings - Fork 14
/
MongoLog.hh
101 lines (86 loc) · 3.12 KB
/
MongoLog.hh
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
#ifndef _MONGOLOG_HH_
#define _MONGOLOG_HH_
#include <unistd.h>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cstdarg>
#include <cstring>
#include <mutex>
#include <string>
#include <map>
#include <vector>
#include <atomic>
#include <thread>
#include <experimental/filesystem>
#include <memory>
#include <mongocxx/pool.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/database.hpp>
#include <mongocxx/collection.hpp>
/*
A brief treatise on log priorities.
LOCAL: the debuggiest debug messages possible. Only written to
local file and not uploaded to the database. Nobody will ever
want to read these except you in the next 5 minutes and maybe
not even you. The default running mode does not record these at all.
DEBUG: Actual debug stuff that you may want to leave on during
commissioning. Will be uploaded to the DB. A very good idea would
be to configure a TTL index (or some similar functionality) on
messages containing this priority so they get deleted after, say,
a month. Then just leave on forever.
WARNING: little oops. Bad thing happened but it is concievable
the DAQ will continue without intervention if it just auto restarts.
An example would be if you try to start with a run mode this process
cannot find.
ERROR: big oops. The DAQ will not recover from this without manual
intervention. An example would be like a disk full error or something
along those lines.
FATAL: reserved for something to write out right before crashing and
dying. Like a last gasp before giving up the ghost. These are the words
on your process' tombstone. An operator shouldn't expect to find a running
*process.
*/
class MongoLog{
/*
Logging class that writes to MongoDB
*/
public:
MongoLog(int DeleteAfterDays, std::shared_ptr<mongocxx::pool>&, std::string, std::string, std::string);
~MongoLog();
int Initialize(std::string connection_string,
std::string db, std::string collection,
std::string host, bool debug=false);
const static int Debug = 0; // Verbose output
const static int Message = 1; // Normal output
const static int Warning = 2; // Bad but minor operational impact
const static int Error = 3; // Major operational impact
const static int Fatal = 4; // Program gonna die
const static int Local = -1; // Write to local (file) log only
int Entry(int priority,std::string message, ...);
void SetRunId(const int runid) {fRunId = runid;}
private:
void Flusher();
std::string FormatTime(struct tm* date);
int Today(struct tm* date);
int RotateLogFile();
std::string LogFileName(struct tm* date);
std::shared_ptr<mongocxx::pool> fPool;
mongocxx::pool::entry fClient;
mongocxx::database fDB;
mongocxx::collection fCollection;
std::vector<std::string> fPriorities{"LOCAL", "DEBUG", "MESSAGE",
"WARNING", "ERROR", "FATAL"};
std::ofstream fOutfile;
std::string fHostname;
int fLogLevel;
int fDeleteAfterDays;
int fToday;
std::mutex fMutex;
std::experimental::filesystem::path fOutputDir;
std::thread fFlushThread;
std::atomic_bool fFlush;
int fFlushPeriod;
int fRunId;
};
#endif