-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMongoLog.h
118 lines (101 loc) · 2.28 KB
/
MongoLog.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
#ifndef MongoLog_h__
#define MongoLog_h__
#include <string>
#include <sstream>
#include <iostream>
#include "MongoClient.h"
class MongoLogDef;
extern MongoLogDef mlDef;
static const auto mlMaxRecordCount = 100 * 10000;
static const std::string mlFieldLevel = "l";
static const std::string mlFieldCategory = "k";
static const std::string mlFieldValue = "v";
static const std::string mlFieldTimeStamp = "t";
enum MongoLogLevel
{
MLLUndef = 0,
MLLDebug = 1,
MLLDetail = 2,
MLLInfo = 3,
MLLWarn = 4,
MLLError = 5,
MLLFatal = 6,
};
inline bool MLog(const std::string& coll, MongoLogLevel level, const std::string& key, const std::string& value)
{
MongoClib::AutoPoolColl c(coll);
if (c.Count(MongoClib::AutoBson()) > mlMaxRecordCount)
{
std::stringstream ss;
ss << coll << "_" << time(nullptr);
c.Rename(ss.str());
}
if (!c.CreateIndex(mlFieldCategory))
{
return false;
}
MongoClib::AutoBson b;
b.AddTime(mlFieldTimeStamp);
b.Add(mlFieldLevel, level);
b.Add(mlFieldCategory, key);
b.Add(mlFieldValue, value);
if (b.IsFailed())
{
return false;
}
return c.Insert(b);
}
//////////////////////////////////////////////////////////////////////////
//
class MongoLogDef
{
private:
class Teacher
{
std::string coll = "TeacherLog";
public:
operator std::string() { return coll; };
// categories
std::string enroll = "enroll";
std::string out = "out";
std::string tStart = "testStart";
std::string tEnd = "testEnd";
bool Enroll(const std::string& v, MongoLogLevel lv = MLLWarn)
{
return MLog(coll, lv, enroll, v);
}
bool Out(const std::string& v)
{
return MLog(coll, MLLWarn, out, v);
}
bool Start(const std::string& v)
{
return MLog(coll, MLLInfo, tStart, v);
}
bool End(const std::string& v)
{
return MLog(coll, MLLInfo, tEnd, v);
}
bool WillAutoGenKeyByFuncName(const std::string& v)
{
// should use with macro
std::string fn = __FUNCTION__;
auto s = fn.substr(fn.rfind(":") + 1, std::string::npos);
return MLog(coll, MLLInfo, s, v);
}
};
class Student
{
std::string coll = "LogStudent";
public:
operator std::string() { return coll; };
// categories
std::string income = "income";
std::string out = "out";
std::string take = "take";
};
public:
Teacher teacher;
Student student;
};
#endif // LogAccess_h__