Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F61943404
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
Thu, May 9, 23:21
Size
6 KB
Mime Type
text/x-c++
Expires
Sat, May 11, 23:21 (2 d)
Engine
blob
Format
Raw Data
Handle
17528022
Attached To
rSPECMICP SpecMiCP / ReactMiCP
log.hpp
View Options
/* =============================================================================
Copyright (c) 2014 - 2016
F. Georget <fabieng@princeton.edu> Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
============================================================================= */
#ifndef SPECMICP_UTILS_LOG_HPP
#define SPECMICP_UTILS_LOG_HPP
#include <sstream>
//! \file log.hpp logger
/*!
\file utils/log.hpp
\brief logger
A common logger is shared by all SpecMiCP/ReactMiCP module.
The logger contains several level which can be activated (or not) by the user.
The user also decides of where the logger output its information.
\code{.cpp}
// output to std::cerr for Warning and Errors (nmo Debug messages)
init_logger(&std::cerr, logger::LogLevel::Warning)
// Log an Error
ERROR << "Error";
// Log a Warning
WARNING << "Warning";
// Log a debug message (no effect);
DEBUG << "Debug (disabled message)
\endcode
This module was inspired by :
- http://www.drdobbs.com/cpp/a-lightweight-logger-for-c/240147505
- http://www.drdobbs.com/cpp/logging-in-c/201804215?pgno=1
*/
namespace
specmicp
{
//! \namespace specmicp::logger
//! \brief The logger used by SpecMiCP/ReactMiCP
namespace
logger
{
//! \enum LogLevel the different log level
//!
//! If changed, the function to_string must also be changed
enum
LogLevel
{
Critical
,
//!< Error that should lead to abortion
Error
,
//!< Error
Warning
,
//!< Warnings
Info
,
//!< May be worth mentionning, not worth listening...
Debug
,
//!< (Relevant) Debugging information
Spam
//!< How is your life ? ususally contains large debugging information such as the variables vector
};
//! \brief Format the log level into a string
std
::
string
to_string
(
LogLevel
);
//! \brief Format the log level into a string for the conf logger
std
::
string
to_string_conf
(
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
//!
//! \param level Level of the message
std
::
ostringstream
&
get
(
LogLevel
level
);
//! \brief Return the report level
static
LogLevel
&
ReportLevel
();
protected
:
std
::
ostringstream
msg
;
//!< the actual message
private
:
// this are hidden on purpose, no need
Log
(
Log
&
)
=
delete
;
Log
&
operator
=
(
Log
&
)
=
delete
;
};
//! \brief Standard output Policy to use for logging
class
ErrFile
{
public
:
//! \brief Return a pointer to the stream we want to write in
static
std
::
ostream
*&
stream
();
//! \brief Output the message to the stream
static
void
output
(
const
std
::
string
&
msg
);
};
//! \brief Standard output Policy to use for logging
class
ConfFile
{
public
:
//! \brief Return a pointer to the stream we want to write in
static
std
::
ostream
*&
stream
();
//! \brief Output the message to the stream
static
void
output
(
const
std
::
string
&
msg
);
};
extern
template
class
Log
<
ConfFile
>
;
extern
template
class
Log
<
ErrFile
>
;
// implementation
// --------------
template
<
typename
outputPolicy
>
Log
<
outputPolicy
>::~
Log
()
{
outputPolicy
::
output
(
msg
.
str
());
}
}
// end namespace logger
//! \brief Standard logger type
using
stdlog
=
logger
::
Log
<
logger
::
ErrFile
>
;
using
conflog
=
logger
::
Log
<
logger
::
ConfFile
>
;
//! \brief Initialize the logger
//!
//! \param out the output stream
//! \param level the output level
void
init_logger
(
std
::
ostream
*
out
,
specmicp
::
logger
::
LogLevel
level
);
//! \brief Configure the special logger containing information about
//! the configuration of specmicp
void
init_conf_logger
(
std
::
ostream
*
out
);
#define SPC_CONF_LOG_FILTER(level) \
if (logger::ConfFile::stream() == nullptr) ;\
else conflog().get(level)
#define SPC_CONF_LOG SPC_CONF_LOG_FILTER(logger::Info)
#define SPC_CONF_LOG_WARNING SPC_CONF_LOG_FILTER(logger::Warning)
#define SPC_CONF_LOG_ERROR SPC_CONF_LOG_FILTER(logger::Error)
#define SPC_CONF_LOG_CRITICAL SPC_CONF_LOG_FILTER(logger::Critical)
#define SPC_CONF_LOG_SECTION "======================================"
#define SPC_CONF_LOG_HLINE "--------------------------------------"
//! \brief Filter logs to stdlog
#define SPC_LOG_FILTER(level) \
if (level > stdlog::ReportLevel() || logger::ErrFile::stream() == nullptr) ;\
else stdlog().get(level)
#define SPC_LOG_FILTER_NDEBUG \
if (true) ; \
else stdlog().get(logger::Warning)
//! \def SPAM
//! \brief Output spam-level log to stdlog
#ifdef NDEBUG
#define SPAM SPC_LOG_FILTER_NDEBUG
#else
#define SPAM SPC_LOG_FILTER(logger::Spam)
#endif
//! \def DEBUG
//! \brief Output debug-level log to stdlog
#ifdef NDEBUG
#define DEBUG SPC_LOG_FILTER_NDEBUG
#else
#define DEBUG SPC_LOG_FILTER(logger::Debug)
#endif
//! \def INFO
//! \brief Ooutput info-level log to stdlog
#ifdef INFO
#undef INFO
// jsoncpp is also using this, remove compiler warnings
#endif
#ifdef NDEBUG
#define INFO SPC_LOG_FILTER_NDEBUG
#else
#undef INFO
#define INFO SPC_LOG_FILTER(logger::Info)
#endif
//! \def WARNING
//! \brief Output warning-level log to stdlog
#define WARNING SPC_LOG_FILTER(logger::Warning)
//! \def ERROR
//! \brief Output error-level log to stdlog
#define ERROR SPC_LOG_FILTER(logger::Error)
//! \def CRITICAL
//! \brief Output critical-level log to stdlog
#define CRITICAL SPC_LOG_FILTER(logger::Critical)
}
// end namespace specmicp
#endif
// SPECMICP_UTILS_LOG_HPP
Event Timeline
Log In to Comment