13df26c3
Steven
Static Logger cla...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/* ****************************************************************************
* Copyright 2019 Open Systems Development BV *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* ***************************************************************************/
|
13df26c3
Steven
Static Logger cla...
|
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
|
void Log::init( const std::string& context, const std::string& logFile, LogLevel logDepth )
{
s_logLevel = logDepth;
s_context = context;
if ( !logFile.empty() )
{
s_fileName = logFile;
}
}
void Log::terminate()
{
s_context.clear();
s_fileName.clear();
s_logLevel = LogLevel::Debug;
s_logMask = LogMask::None;
}
void Log::write( const int &priority, const std::string &message, const std::string& context )
{
switch( s_logMask )
{
case LogMask::Mask:
setlogmask( LOG_MASK( toInt( s_logLevel ) ) );
break;
case LogMask::Upto:
setlogmask( LOG_UPTO( toInt( s_logLevel ) ) );
break;
case LogMask::None:
setlogmask( LOG_UPTO( toInt( LogLevel::Debug ) ) );
break;
default:
break;
}
if( !context.empty() )
{
s_context = context;
openlog( s_context.c_str(), LOG_PID, LOG_USER );
}
else
{
openlog( NULL, LOG_PID, LOG_USER );
}
syslog( priority, "%s", message.c_str() );
closelog();
}
std::string Log::fileinfoMessage( const char* file, int line, const std::string& message )
{
(void)file;
(void)line;
|
13df26c3
Steven
Static Logger cla...
|
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
std::string logCategory = category;
std::string logMessage = message;
if( logMessage.empty() )
{
static const std::string emptyMessage( "--" );
logMessage = emptyMessage;
}
else
{
// Replace % signs
ReplaceAll( logMessage, "%", "%%" );
}
write( toInt( level ), logCategory, logMessage );
}
// Static functions
void Log::emergency( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Emergency );
}
void Log::alert( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Alert );
}
void Log::critical( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Critical );
}
void Log::error( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Error );
}
void Log::warning( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Warning );
}
void Log::notice( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Notice );
}
void Log::info( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Info );
}
void Log::debug( const char *file, int line, const std::string &category, const std::string &message )
{
log( category, fileinfoMessage( file, line, message ), LogLevel::Debug );
}
// Protected functions
void Log::emergency( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Emergency ), message, category );
}
void Log::alert( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Alert ), message, category );
}
void Log::critical( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Critical ), message, category );
}
void Log::error( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Error ), message, category );
}
void Log::warning( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Warning ), message, category );
}
void Log::notice( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Notice ), message, category );
}
void Log::info( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Info ), message, category );
}
void Log::debug( const std::string &category, const std::string &message )
{
write( toInt( LogLevel::Debug ), message, category );
}
void Log::ReplaceAll( std::string &strToReplace, const std::string& from_chars, const std::string& to_chars )
{
size_t start_pos = 0;
while( ( start_pos = strToReplace.find( from_chars, start_pos ) ) != std::string::npos )
{
strToReplace.replace( start_pos, from_chars.length(), to_chars );
start_pos += to_chars.length(); // Handles case where 'to' is a substring of 'from'
}
}
} /* End namespace log */
} /* End namespace components */
} /* End namespace osdev */
|