patterncppMinor
A modern and extensible C++14 logger
Viewed 0 times
andextensiblemodernlogger
Problem
I recently wrote this C++ Logger API code for a game engine. It needs to be as fast as possible so I tried to postpone everything to compile-time using different template meta-programming tricks (for instance,
Any advice, bug fixes or suggestions are welcome.
Log.h:
```
// Copyright(c) Matyas Yves Constans 2016-2017
#ifndef CIANADE_LOG_H
#define CIANADE_LOG_H
#include
#include
#include
namespace cnd
{
/** @brief Specifies the minimum log level.
* Enumeration specifying the minimum
* log level at which LogEntry will
* write a message to a stream.
* @see LogEntry
*/
enum class LogLevel
{
FATAL, ///
class LogEntry
{
public:
/** @brief Constructor.
*/
LogEntry();
/** @brief Prints current message.
* Prints a formatted message based on the internal stream's content.
*/
virtual ~LogEntry();
/** @brief Returns a reference to the instance's stream.
* Returns a reference to the stream so a message can be printed using the
* standard c++ stream syntax.
* Shall be overridden when inheriting from LogEntry
* @tparam Level Specifies the log level corresponding to the message
* @return The stream corresponding to the log entry
*/
template
std::ostringstream& GetMessageStream();
/** @brief Returns the current logging level.
* @return The Current logging level (The default log level is LogLevel::DEBUG).
* @see LogLevel
*/
constexpr static LogLevel GetReportingLevel();
public:
/** @brief Converts an std::string to a LogLevel
* @param level An std::string representing a LogLevel. it can be either "FATAL",
* "ERROR", "WARN", "INFO", "DEBUG", "DEBUG_1", "DEBUG_2", "DEBUG_3" or
* "DEBUG_4".
* @return The corresponding log level.
*/
static LogLevel StringToLogLevel
static_if).Any advice, bug fixes or suggestions are welcome.
Log.h:
```
// Copyright(c) Matyas Yves Constans 2016-2017
#ifndef CIANADE_LOG_H
#define CIANADE_LOG_H
#include
#include
#include
namespace cnd
{
/** @brief Specifies the minimum log level.
* Enumeration specifying the minimum
* log level at which LogEntry will
* write a message to a stream.
* @see LogEntry
*/
enum class LogLevel
{
FATAL, ///
class LogEntry
{
public:
/** @brief Constructor.
*/
LogEntry();
/** @brief Prints current message.
* Prints a formatted message based on the internal stream's content.
*/
virtual ~LogEntry();
/** @brief Returns a reference to the instance's stream.
* Returns a reference to the stream so a message can be printed using the
* standard c++ stream syntax.
* Shall be overridden when inheriting from LogEntry
* @tparam Level Specifies the log level corresponding to the message
* @return The stream corresponding to the log entry
*/
template
std::ostringstream& GetMessageStream();
/** @brief Returns the current logging level.
* @return The Current logging level (The default log level is LogLevel::DEBUG).
* @see LogLevel
*/
constexpr static LogLevel GetReportingLevel();
public:
/** @brief Converts an std::string to a LogLevel
* @param level An std::string representing a LogLevel. it can be either "FATAL",
* "ERROR", "WARN", "INFO", "DEBUG", "DEBUG_1", "DEBUG_2", "DEBUG_3" or
* "DEBUG_4".
* @return The corresponding log level.
*/
static LogLevel StringToLogLevel
Solution
You missed the most obvious issue.
If the log level
Bad Code:
This is not even legal C++
cnd::WriteLog(getMessage("BLa", "Bla", 5));If the log level
DEBUG is not going to print anything. Then calling the function getMessage() is a complete waste of time. You need to defer the call of getMessage() and only call it when it will generate something useful.Bad Code:
This is not even legal C++
cnd::LogLevel cnd::LogEntry::StringToLogLevel(const std::string& level)
{
switch (level) // level must be an integer typeCode Snippets
cnd::WriteLog<cnd::LogLevel::DEBUG>(getMessage("BLa", "Bla", 5));cnd::LogLevel cnd::LogEntry<T>::StringToLogLevel(const std::string& level)
{
switch (level) // level must be an integer typeContext
StackExchange Code Review Q#150804, answer score: 3
Revisions (0)
No revisions yet.