From 42470d2090319003203ecaa7575848fc6e2c0325 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Mon, 12 Mar 2018 11:38:57 +0100 Subject: [PATCH] Logger: handle nullptr. --- logger/Logger.cxx | 16 ++++++++++++++-- logger/Logger.h | 30 +++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/logger/Logger.cxx b/logger/Logger.cxx index b7655f0..2f8941c 100644 --- a/logger/Logger.cxx +++ b/logger/Logger.cxx @@ -470,7 +470,7 @@ void Logger::RemoveCustomSink(const string& key) } } -ostringstream& Logger::Log() +Logger& Logger::Log() { char tsstr[32]; if (!strftime(tsstr, sizeof(tsstr), "%H:%M:%S", localtime(&(fMetaData.timestamp)))) @@ -522,7 +522,19 @@ ostringstream& Logger::Log() fColorOut << " "; } - return fContent; + return *this; +} + +Logger& Logger::operator<<(ios_base& (*manip) (ios_base&)) +{ + fContent << manip; + return *this; +} + +Logger& Logger::operator<<(ostream& (*manip) (ostream&)) +{ + fContent << manip; + return *this; } Logger::~Logger() noexcept(false) diff --git a/logger/Logger.h b/logger/Logger.h index 1060ca4..f8eeed1 100644 --- a/logger/Logger.h +++ b/logger/Logger.h @@ -144,7 +144,35 @@ class Logger static void AddCustomSink(const std::string& key, const std::string& severityStr, std::function sink); static void RemoveCustomSink(const std::string& key); - std::ostringstream& Log(); + Logger& Log(); + + template + Logger& operator<<(const T& t) + { + fContent << t; + return *this; + } + + // overload for char* to make sure it is not nullptr + Logger& operator<<(const char* cptr) + { + if (cptr != nullptr) { + fContent << cptr; + } + return *this; + } + + // overload for char* to make sure it is not nullptr + Logger& operator<<(char* cptr) + { + if (cptr != nullptr) { + fContent << cptr; + } + return *this; + } + + Logger& operator<<(std::ios_base& (*manip) (std::ios_base&)); + Logger& operator<<(std::ostream& (*manip) (std::ostream&)); static const std::unordered_map fVerbosityMap; static const std::unordered_map fSeverityMap;