Logger: handle nullptr.

This commit is contained in:
Alexey Rybalchenko 2018-03-12 11:38:57 +01:00 committed by Mohammad Al-Turany
parent a9f9030041
commit 42470d2090
2 changed files with 43 additions and 3 deletions

View File

@ -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)

View File

@ -144,7 +144,35 @@ class Logger
static void AddCustomSink(const std::string& key, const std::string& severityStr, std::function<void(const std::string& content, const LogMetaData& metadata)> sink);
static void RemoveCustomSink(const std::string& key);
std::ostringstream& Log();
Logger& Log();
template<typename T>
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<std::string, Verbosity> fVerbosityMap;
static const std::unordered_map<std::string, Severity> fSeverityMap;