Skip to content

Commit

Permalink
Added Log stream convenience functions
Browse files Browse the repository at this point in the history
Added hex stream manipulator
  • Loading branch information
mbcrawfo committed Nov 13, 2014
1 parent 474089e commit 2aafdfd
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 46 deletions.
78 changes: 76 additions & 2 deletions include/log/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,19 @@ class Log final
* If LU_LOG_DISABLE_VERBOSE is defined then the verbose methods are no-ops.
* If LU_LOG_DISABLE_DEBUG is defined then the debug methods are no-ops.
*/
StreamHelper verbose(const std::string& tag);
void verbose(const std::string& tag, const std::string& msg);
void verbosef(const std::string& tag, const char* fmt, ...);
StreamHelper debug(const std::string& tag);
void debug(const std::string& tag, const std::string& msg);
void debugf(const std::string& tag, const char* fmt, ...);
StreamHelper info(const std::string& tag);
void info(const std::string& tag, const std::string& msg);
void infof(const std::string& tag, const char* fmt, ...);
StreamHelper warning(const std::string& tag);
void warning(const std::string& tag, const std::string& msg);
void warningf(const std::string& tag, const char* fmt, ...);
StreamHelper error(const std::string& tag);
void error(const std::string& tag, const std::string& msg);
void errorf(const std::string& tag, const char* fmt, ...);
// \}
Expand Down Expand Up @@ -164,7 +169,7 @@ class Log final
using Manipulator = std::ostream& (*)(std::ostream&);

// constructors
StreamHelper() = delete;
StreamHelper() = default;
StreamHelper(Log& log, LogLevel level, const std::string& tag);
StreamHelper(const StreamHelper&) = default;
// operators
Expand Down Expand Up @@ -210,10 +215,79 @@ using WeakLogPtr = WeakPtr<Log>;
* Definitions
****************************************************************************/

inline Log::StreamHelper Log::verbose(const std::string& tag)
{
#ifdef LU_LOG_DISABLE_VERBOSE
LU_UNUSED(tag);
return StreamHelper();
#else
return stream(LogLevel::Verbose, tag);
#endif
}

inline void Log::verbose(const std::string& tag, const std::string& msg)
{
#ifdef LU_LOG_DISABLE_VERBOSE
LU_UNUSED(tag);
LU_UNUSED(msg);
#else
log(LogLevel::Verbose, tag, msg);
#endif
}

inline void Log::verbosef(const std::string& tag, const char* fmt, ...)
{
#ifdef LU_LOG_DISABLE_VERBOSE
LU_UNUSED(tag);
LU_UNUSED(fmt);
#else
va_list args;
va_start(args, fmt);
logv(LogLevel::Verbose, tag, fmt, args);
va_end(args);
#endif
}

inline Log::StreamHelper Log::debug(const std::string& tag)
{
#ifdef LU_LOG_DISABLE_DEBUG
LU_UNUSED(tag);
return StreamHelper();
#else
return stream(LogLevel::Debug, tag);
#endif
}

inline void Log::debug(const std::string& tag, const std::string& msg)
{
#ifdef LU_LOG_DISABLE_DEBUG
LU_UNUSED(tag);
LU_UNUSED(msg);
#else
log(LogLevel::Debug, tag, msg);
#endif
}

inline void Log::debugf(const std::string& tag, const char* fmt, ...)
{
#ifdef LU_LOG_DISABLE_DEBUG
LU_UNUSED(tag);
LU_UNUSED(fmt);
#else
va_list args;
va_start(args, fmt);
logv(LogLevel::Debug, tag, fmt, args);
va_end(args);
#endif
}

template<typename T>
Log::StreamHelper& Log::StreamHelper::operator<<(const T& arg)
{
impl->os << arg;
if (impl)
{
impl->os << arg;
}
return *this;
}

Expand Down
85 changes: 85 additions & 0 deletions include/utility/stream_manip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* The MIT License (MIT)
*
* Copyright (c) 2014 Michael Crawford
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef STREAM_MANIP_H_INCLUDED__
#define STREAM_MANIP_H_INCLUDED__

#include "prereqs.h"
#include <iomanip>
#include <iostream>

namespace util
{

// \{
/**
* Manipulates an input or output stream to output or read integers in
* hexadecimal format with leading zeros.
*/
template<typename T, std::size_t width = sizeof(T) * 2>
std::ostream& hex(std::ostream& str);
template<typename T, std::size_t width = sizeof(T) * 2>
std::istream& hex(std::istream& str);
// \}

/****************************************************************************
* Definitions
****************************************************************************/

template<typename T, std::size_t width>
std::ostream& hex(std::ostream& str)
{
str << std::setw(width) << std::setfill('0');
return str;
}

template<typename T, std::size_t width>
std::istream& hex(std::istream& str)
{
str >> std::setw(width) >> std::setfill('0');
return str;
}

}

// operator overloads to allow usage with STL streams
namespace std
{

template<typename T>
ostream& operator<<(ostream& os, decltype(util::hex<T>) h)
{
os << h;
return os;
}

template<typename T>
istream& operator<<(istream& is, decltype(util::hex<T>) h)
{
is >> h;
return is;
}

}

#endif
1 change: 1 addition & 0 deletions msvc/libutil.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<ClInclude Include="..\include\utility\IToString.h" />
<ClInclude Include="..\include\utility\Singleton.h" />
<ClInclude Include="..\include\utility\Singularity.h" />
<ClInclude Include="..\include\utility\stream_manip.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\log\FileLogWriter.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions msvc/libutil.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
<ClInclude Include="..\include\log\FileLogWriter.h">
<Filter>Header Files\log</Filter>
</ClInclude>
<ClInclude Include="..\include\utility\stream_manip.h">
<Filter>Header Files\utility</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\test\test.cpp">
Expand Down
60 changes: 16 additions & 44 deletions src/log/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,50 +87,9 @@ bool Log::removeWriter(const std::string& name)
return true;
}

void Log::verbose(const std::string& tag, const std::string& msg)
Log::StreamHelper Log::info(const std::string& tag)
{
#ifdef LU_LOG_DISABLE_VERBOSE
LU_UNUSED(tag);
LU_UNUSED(msg);
#else
log(LogLevel::Verbose, tag, msg);
#endif
}

void Log::verbosef(const std::string& tag, const char* fmt, ...)
{
#ifdef LU_LOG_DISABLE_VERBOSE
LU_UNUSED(tag);
LU_UNUSED(fmt);
#else
va_list args;
va_start(args, fmt);
logv(LogLevel::Verbose, tag, fmt, args);
va_end(args);
#endif
}

void Log::debug(const std::string& tag, const std::string& msg)
{
#ifdef LU_LOG_DISABLE_DEBUG
LU_UNUSED(tag);
LU_UNUSED(msg);
#else
log(LogLevel::Debug, tag, msg);
#endif
}

void Log::debugf(const std::string& tag, const char* fmt, ...)
{
#ifdef LU_LOG_DISABLE_DEBUG
LU_UNUSED(tag);
LU_UNUSED(fmt);
#else
va_list args;
va_start(args, fmt);
logv(LogLevel::Debug, tag, fmt, args);
va_end(args);
#endif
return stream(LogLevel::Info, tag);
}

void Log::info(const std::string& tag, const std::string& msg)
Expand All @@ -146,6 +105,11 @@ void Log::infof(const std::string& tag, const char* fmt, ...)
va_end(args);
}

Log::StreamHelper Log::warning(const std::string& tag)
{
return stream(LogLevel::Warning, tag);
}

void Log::warning(const std::string& tag, const std::string& msg)
{
log(LogLevel::Warning, tag, msg);
Expand All @@ -159,6 +123,11 @@ void Log::warningf(const std::string& tag, const char* fmt, ...)
va_end(args);
}

Log::StreamHelper Log::error(const std::string& tag)
{
return stream(LogLevel::Error, tag);
}

void Log::error(const std::string& tag, const std::string& msg)
{
log(LogLevel::Error, tag, msg);
Expand Down Expand Up @@ -259,7 +228,10 @@ Log::StreamHelper::StreamHelper(Log& log, LogLevel level,
Log::StreamHelper& Log::StreamHelper::operator<<(
Log::StreamHelper::Manipulator manip)
{
manip(impl->os);
if (impl)
{
manip(impl->os);
}
return *this;
}

Expand Down

0 comments on commit 2aafdfd

Please sign in to comment.