Permalink
Browse files

String_views in COutputLogger

  • Loading branch information...
jolting committed Apr 30, 2018
1 parent 631f4c3 commit ecb953b5370107d77a174c6354f88ec3596b78da
Showing with 14 additions and 13 deletions.
  1. +8 −7 libs/system/include/mrpt/system/COutputLogger.h
  2. +6 −6 libs/system/src/COutputLogger.cpp
@@ -15,6 +15,7 @@
#include <mrpt/system/datetime.h> // TTimeStamp
#include <string>
#include <string_view>
#include <deque>
#include <sstream>
#include <iosfwd>
@@ -37,8 +38,8 @@ enum VerbosityLevel
/** Callback types for use with mrpt::system::COuputLogger */
using output_logger_callback_t = std::function<void(
const std::string& msg, const mrpt::system::VerbosityLevel level,
const std::string& loggerName, const mrpt::system::TTimeStamp timestamp)>;
std::string_view msg, const mrpt::system::VerbosityLevel level,
std::string_view loggerName, const mrpt::system::TTimeStamp timestamp)>;
/** \brief Versatile class for consistent logging and
* management of output messages
@@ -142,7 +143,7 @@ class COutputLogger
* a_logger.setLoggerName("logger_name");
* \endcode
*/
COutputLogger(const std::string& name);
COutputLogger(std::string_view name);
/** Default class constructor. Name of the logger is initialized to "logStr"
*/
COutputLogger();
@@ -151,7 +152,7 @@ class COutputLogger
/** \brief Main method to add the specified message string to the logger.
* \sa logCond, logFmt */
void logStr(const VerbosityLevel level, const std::string& msg_str)
void logStr(const VerbosityLevel level, std::string_view msg_str)
const; // renamed from log() to avoid conflict with math ::log()
/** \brief Alternative logging method, which mimics the printf behavior.
@@ -169,7 +170,7 @@ class COutputLogger
*
* \sa logStr, logCond
*/
void logFmt(const VerbosityLevel level, const char* fmt, ...) const
void logFmt(const VerbosityLevel level, const char *fmt, ...) const
MRPT_printf_format_check(3, 4); // arg 1=this
/** \brief Log the given message only if the condition is satisfied.
@@ -271,7 +272,7 @@ class COutputLogger
* current message
*/
TMsg(
const mrpt::system::VerbosityLevel level, const std::string& msg,
const mrpt::system::VerbosityLevel level, std::string_view msg,
const COutputLogger& logger);
/** \brief Default Destructor */
~TMsg();
@@ -305,7 +306,7 @@ class COutputLogger
/** Helper method for generating a std::string instance from printf-like
* arguments */
std::string generateStringFromFormat(const char* fmt, va_list argp) const;
std::string generateStringFromFormat(std::string_view fmt, va_list argp) const;
std::string m_logger_name;
mutable std::deque<TMsg>
@@ -51,15 +51,15 @@ std::string COutputLogger::logging_levels_to_names[NUMBER_OF_VERBOSITY_LEVELS] =
"ERROR" // LVL_ERROR
};
COutputLogger::COutputLogger(const std::string& name)
COutputLogger::COutputLogger(std::string_view name)
{
this->loggerReset();
m_logger_name = name;
}
COutputLogger::COutputLogger() { this->loggerReset(); }
COutputLogger::~COutputLogger() {}
void COutputLogger::logStr(
const VerbosityLevel level, const std::string& msg_str) const
const VerbosityLevel level, std::string_view msg_str) const
{
if (level < m_min_verbosity_level) return;
@@ -78,7 +78,7 @@ void COutputLogger::logStr(
}
void COutputLogger::logFmt(
const VerbosityLevel level, const char* fmt, ...) const
const VerbosityLevel level, const char *fmt, ...) const
{
// see MRPT/libs/base/src/utils/CDeugOutputCapable.cpp for the iniitial
// implementtion
@@ -97,15 +97,15 @@ void COutputLogger::logFmt(
}
std::string COutputLogger::generateStringFromFormat(
const char* fmt, va_list argp) const
std::string_view fmt, va_list argp) const
{
int result = -1, length = 1024;
std::vector<char> buffer;
// make sure that the buffer is large enough to handle the string
while (result == -1)
{
buffer.resize(length + 10);
result = os::vsnprintf(&buffer[0], length, fmt, argp);
result = os::vsnprintf(&buffer[0], length, fmt.data(), argp);
// http://www.cplusplus.com/reference/cstdio/vsnprintf/
// only when this returned value is non-negative and less than n, the
@@ -209,7 +209,7 @@ void COutputLogger::loggerReset()
// ////////////////////////////////////////////////////////////
COutputLogger::TMsg::TMsg(
const mrpt::system::VerbosityLevel in_level, const std::string& msg_str,
const mrpt::system::VerbosityLevel in_level, std::string_view msg_str,
const COutputLogger& logger)
{
this->reset();

2 comments on commit ecb953b

@jlblancoc

This comment has been minimized.

Member

jlblancoc replied Apr 30, 2018

Would it be advisable to use the "sv" postfix to exploit the advantages of string_views at some point?

@jolting

This comment has been minimized.

Member

jolting replied Apr 30, 2018

Sure. Honestly, I'm not sure what is best practice with these things yet. Most of the examples appear academic. However, the advantages are clear.

Please sign in to comment.