-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_logger.cpp
More file actions
129 lines (107 loc) · 3.43 KB
/
fs_logger.cpp
File metadata and controls
129 lines (107 loc) · 3.43 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
#include "fs_logger.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <cctype>
#include <algorithm>
#include <boost/bimap.hpp>
#include <mongo/util/time_support.h>
using namespace std;
using namespace mgridfs;
namespace {
//typedef boost::bimap<LogLevel, string> LogLevelToStringMap;
typedef boost::bimap<LogLevel, string> LogLevelToStringMap;
LogLevelToStringMap logLevelToStringMap;
const string LL_INVALID_STR = "INVALID";
}
FSLogManager::FSLogManager()
: _ll(LL_TRACE), _logDestination(NULL) {
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_INVALID, LL_INVALID_STR));
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_TRACE, "TRACE"));
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_DEBUG, "DEBUG"));
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_INFO, "INFO"));
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_WARN, "WARN"));
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_ERROR, "ERROR"));
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_FATAL, "FATAL"));
logLevelToStringMap.insert(LogLevelToStringMap::value_type(LL_NONE, "NONE"));
}
FSLogManager::~FSLogManager() {
if (_logDestination) {
delete _logDestination;
_logDestination = NULL;
}
}
FSLogManager& FSLogManager::get() {
static FSLogManager instance;
return instance;
}
const string& FSLogManager::logLevelToString(LogLevel ll) const {
LogLevelToStringMap::left_map::const_iterator pIt = logLevelToStringMap.left.find(ll);
if (pIt != logLevelToStringMap.left.end()) {
return pIt->second;
}
return LL_INVALID_STR;
}
LogLevel FSLogManager::stringToLogLevel(const string& logLevel) const {
LogLevelToStringMap::right_map::const_iterator pIt = logLevelToStringMap.right.find(logLevel);
if (pIt != logLevelToStringMap.right.end()) {
return pIt->second;
}
return LL_INVALID;
}
bool FSLogManager::logAll(LogLevel ll, const string& logMessage) {
if (ll >= _ll) {
if (_logDestination) {
_logDestination->logAll(ll, logMessage);
} else {
cout << logMessage << flush;
}
}
return true;
}
bool FSLogManager::registerDestination(FSLogDestination* logDestination) {
// TODO: Make thread-safe if need to be enabled for multi-threaded
FSLogDestination* temp = _logDestination;
_logDestination = logDestination;
if (temp) {
delete temp;
}
return true;
}
FSLogFile::FSLogFile(const string& filename)
: _filename(filename), _logFile(filename.c_str()) {
info() << "Opened MongoDB-GridFS log file {file: " << filename << "}" << endl;
}
FSLogFile::~FSLogFile() {
if (_logFile.is_open()) {
info() << "Closing MongoDB-GridFS log file" << endl;
_logFile.close();
}
}
bool FSLogFile::logAll(LogLevel ll, const string& logMessage) {
_logFile << logMessage << flush;
return true;
}
FSLogStream::FSLogStream(LogLevel ll)
: _ll(ll), _enabledLL(FSLogManager::get().getLogLevel()), _dirty(false), _os(NULL) {
}
FSLogStream::FSLogStream(const FSLogStream& fsLogStream)
: _ll(fsLogStream._ll), _enabledLL(fsLogStream._enabledLL), _dirty(false), _os(NULL) {
}
FSLogStream::~FSLogStream() {
if (_os) {
if (_ll >= _enabledLL && _dirty) {
FSLogManager::get().logAll(_ll, _os->str());
}
delete _os;
_os = NULL;
}
}
ostringstream& FSLogStream::stream() {
if (_os) {
return *_os;
}
_os = new ostringstream();
*_os << dateToCtimeString(mongo::jsTime()) << " [thread-" << pthread_self() << "] " << FSLogManager::get().logLevelToString(_ll) << " ";
return *_os;
}