Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F99184641
log.hpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Jan 22, 03:17
Size
2 KB
Mime Type
text/x-c++
Expires
Fri, Jan 24, 03:17 (2 d)
Engine
blob
Format
Raw Data
Handle
23720245
Attached To
rSPECMICP SpecMiCP / ReactMiCP
log.hpp
View Options
/*-------------------------------------------------------
- Module : utils
- File : log.hpp
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget, Princeton University
---------------------------------------------------------*/
#ifndef SPECMICP_UTILS_LOG_HPP
#define SPECMICP_UTILS_LOG_HPP
//! \file log.hpp logger
namespace
specmicp
{
namespace
logger
{
//! \enum LogLevel the different log level
//!
//! If changed, the function to_string must also be changed
enum
LogLevel
{
Critical
,
Error
,
Warning
,
Info
,
Debug
,
Spam
};
//! Format the log level into a string
inline
std
::
string
to_string
(
LogLevel
);
/*! \brief A log message
*
* The message is written during the destruction of the Log object.
* The stream which contain the message is accessed with the member get(Level)
*/
template
<
typename
outputPolicy
>
class
Log
{
public
:
//! Constructor
Log
()
{}
//! Destructor - Output the message
~
Log
();
//! \brief Return the steam so we can write the message
std
::
ostringstream
&
get
(
LogLevel
level
);
//! \brief Return the report level
static
LogLevel
&
ReportLevel
(){
static
LogLevel
report_level
=
Debug
;
return
report_level
;
}
protected
:
std
::
ostringstream
msg
;
//!< the actual message
private
:
// this are hidden on purpose, no need
Log
(
Log
&
);
Log
&
operator
=
(
Log
&
);
};
template
<
typename
outputPolicy
>
std
::
ostringstream
&
Log
<
outputPolicy
>::
get
(
LogLevel
level
)
{
msg
<<
to_string
(
level
)
<<
" : "
;
return
msg
;
}
template
<
typename
outputPolicy
>
Log
<
outputPolicy
>::~
Log
()
{
outputPolicy
::
output
(
msg
.
str
());
}
//! Output Policy to use for logging
class
ErrFile
{
public
:
//! Return a pointer to the stream we want to write in
static
std
::
ostream
*&
stream
();
//! Output the message to the stream
static
void
output
(
const
std
::
string
&
msg
);
};
inline
std
::
ostream
*&
ErrFile
::
stream
()
{
static
std
::
ostream
*
stream
=
nullptr
;
return
stream
;
}
inline
void
ErrFile
::
output
(
const
std
::
string
&
msg
)
{
std
::
ostream
*
out
=
stream
();
(
*
out
)
<<
msg
<<
std
::
endl
;
out
->
flush
();
}
inline
std
::
string
to_string
(
LogLevel
level
)
{
static
const
char
*
list_level
[]
=
{
"CRITICAL"
,
"ERROR"
,
"Warning"
,
"info"
,
"debug"
,
"spam"
};
return
list_level
[
level
];
}
}
// end namespace logger
//! Standard logger type
using
stdlog
=
logger
::
Log
<
logger
::
ErrFile
>
;
//! Filter logs to stdlog
#define FILTER(level) \
if (level > stdlog::ReportLevel() || logger::ErrFile::stream() == nullptr) ;\
else stdlog().get(level)
//! Debug log to stdlog
#define SPAM FILTER(logger::Spam)
//! Debug log to stdlog
#define DEBUG FILTER(logger::Debug)
//! Debug log to stdlog
#define INFO FILTER(logger::Info)
//! Warning log to stdlog
#define WARNING FILTER(logger::Warning)
//! Error log to stdlog
#define ERROR FILTER(logger::Error)
//! Critical log to stdlog
#define CRITICAL FILTER(logger::Critical)
}
// end namespace specmicp
#endif
// SPECMICP_UTILS_LOG_HPP
Event Timeline
Log In to Comment