Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature(Container) Logging of std containers with custom data types #250

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 49 additions & 33 deletions include/plog/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,6 @@ namespace plog
}
#endif //__cpp_char8_t

// Print `std::pair`
template<class T1, class T2>
inline void operator<<(util::nostringstream& stream, const std::pair<T1, T2>& data)
{
stream << data.first;
stream << ":";
stream << data.second;
}

#if defined(__clang__) || !defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__) >= 405 // skip for GCC < 4.5 due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38600
#if !defined(_MSC_VER) || _MSC_VER > 1400 // MSVC 2005 doesn't understand `enableIf`, so drop all `meta`
Expand All @@ -175,29 +167,6 @@ namespace plog
plog::detail::operator<<(stream, static_cast<util::nstring>(data));
}

// Print std containers
template<class T>
inline typename meta::enableIf<meta::isContainer<T>::value &&
!meta::isConvertibleToNString<T>::value &&
!meta::isConvertibleToString<T>::value &&
!meta::isFilesystemPath<T>::value, void>::type operator<<(util::nostringstream& stream, const T& data)
{
stream << "[";

for (typename T::const_iterator it = data.begin(); it != data.end();)
{
stream << *it;

if (++it == data.end())
{
break;
}

stream << ", ";
}

stream << "]";
}
#endif
#endif

Expand Down Expand Up @@ -316,15 +285,62 @@ namespace plog
# endif
#endif

template<typename T>
Record& operator<<(const T& data)
template <typename T>
inline typename detail::meta::enableIf<!detail::meta::isContainer<T>::value ||
detail::meta::isConvertibleToNString<T>::value ||
detail::meta::isConvertibleToString<T>::value, Record &>::type operator<<(const T &data)
{
using namespace plog::detail;

m_message << data;
return *this;
}

Record &operator<<(const std::string &data)
{
m_message << data.c_str();
return *this;
}

template <typename T>
inline typename detail::meta::enableIf<detail::meta::isContainer<T>::value &&
!detail::meta::isConvertibleToNString<T>::value &&
!detail::meta::isConvertibleToString<T>::value &&
!detail::meta::isFilesystemPath<T>::value,
Record &>::type
operator<<(const T &data)
{
using namespace plog::detail;

*this << "[";

for (typename T::const_iterator it = data.begin(); it != data.end();)
{
*this << *it;

if (++it == data.end())
{
break;
}

*this << ", ";
}

*this << "]";

return *this;
}

// Print `std::pair`
template <class T1, class T2>
inline Record& operator<<(const std::pair<T1, T2> &data)
{
*this << data.first;
*this << ":";
*this << data.second;
return *this;
}

#ifndef __cplusplus_cli
Record& printf(const char* format, ...)
{
Expand Down
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ add_subdirectory(Android)
add_subdirectory(AscDump)
add_subdirectory(Chained)
add_subdirectory(ColorConsole)
add_subdirectory(Container)
add_subdirectory(CustomAppender)
add_subdirectory(CustomConverter)
add_subdirectory(CustomFormatter)
Expand Down
3 changes: 3 additions & 0 deletions samples/Container/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(Container Main.cpp)
target_link_libraries(Container plog)
set_target_properties(Container PROPERTIES FOLDER Samples)
71 changes: 71 additions & 0 deletions samples/Container/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Container - shows how to log std containers.
//

#include <plog/Log.h>
#include <plog/Init.h>
#include <plog/Formatters/TxtFormatter.h>
#include <plog/Appenders/ColorConsoleAppender.h>
#include <vector>
#include <map>

struct data
{
int i;
std::string s;
};

namespace plog
{
Record &operator<<(Record &record, const data &data)
{
return record << "\n"
<< "struct data {\n"
<< " int i: " << data.i << "\n"
<< " std::string s: " << data.s << "\n"
<< "}";
}
} // namespace plog

void logSingle()
{
PLOGI << "Log a single custom data type";
data data;
data.i = 42;
data.s = "Hellow World!";
PLOGN << data;
}

void logContainer()
{
PLOGI << "Log a vector holding elements of the custom data type";

std::vector<data> vdata;
data data1;
data1.i = 42;
data1.s = "Hellow World!";
data data2;
data2.i = 12;
data2.s = "Good bye!";
vdata.push_back(data1);
vdata.push_back(data2);
PLOGN << vdata;

// like always you can also combine this with other strings or elements
PLOGN << "Prepend to this " << vdata << " Add to this";

std::map<const std::string, data> map{
{"FirstKey", data1},
{"SecondKey", data2}};

PLOGN << map;
}

int main()
{
static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
plog::init(plog::verbose, &consoleAppender);

logSingle();
logContainer();
}