Skip to content

Commit

Permalink
Add support for printing std containers (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiusTheBest committed Apr 24, 2022
1 parent 225c563 commit 130801c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
31 changes: 31 additions & 0 deletions include/plog/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,43 @@ namespace plog
plog::detail::operator<<(stream, data.c_str());
}

// 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__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 4 // skip for GCC < 4.5 due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38600
// Print data that can be casted to `std::basic_string`. `+ sizeof(T*)` is required for GCC 4.5-4.7.
template<typename T>
inline typename meta::enableIf<!!(sizeof(static_cast<std::basic_string<util::nchar> >(meta::declval<T>())) + sizeof(T*)), void>::type operator<<(util::nostringstream& stream, const T& data)
{
plog::detail::operator<<(stream, static_cast<std::basic_string<util::nchar> >(data));
}

// Print std containers
template<class Container>
inline typename meta::enableIf<!!(sizeof(typename Container::const_iterator)), void>::type operator<<(util::nostringstream& stream, const Container& data)
{
stream << "[";

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

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

stream << ", ";
}

stream << "]";
}
#endif

#ifdef __cplusplus_cli
Expand Down
58 changes: 57 additions & 1 deletion samples/Demo/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

#include <plog/Log.h>
#include <plog/Initializers/RollingFileInitializer.h>
#include <plog/Formatters/TxtFormatter.h>
#include <plog/Appenders/ColorConsoleAppender.h>

#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>

#include "MyClass.h"
#include "Customer.h"
Expand All @@ -20,7 +28,10 @@ void unmanagedFunc()

int main()
{
plog::init(plog::debug, "Demo.csv", 5000, 3); // Initialize the logger.
plog::init(plog::debug, "Demo.csv", 5000, 3); // Initialize logging to the file.

plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
plog::get()->addAppender(&consoleAppender); // Also add logging to the console.

// Log macro types.
PLOGD << "Hello log!"; // short macro
Expand Down Expand Up @@ -121,5 +132,50 @@ int main()
Customer customer = { 10, "John" };
PLOG_INFO << customer;

// Std containers can be printed
std::vector<int> vectorOfInts;
vectorOfInts.push_back(1);
vectorOfInts.push_back(2);
vectorOfInts.push_back(3);
PLOG_INFO << "std::vector<int>: " << vectorOfInts;

std::deque<std::string> dequeOfStrings;
dequeOfStrings.push_back("one");
dequeOfStrings.push_back("two");
dequeOfStrings.push_back("three");
PLOG_INFO << "std::deque<std::string>: " << dequeOfStrings;

std::list<const char*> listOfCharPointers;
listOfCharPointers.push_back("one");
listOfCharPointers.push_back("two");
listOfCharPointers.push_back(NULL);
PLOG_INFO << "std::list<const char*>: " << listOfCharPointers;

std::set<int> setOfInts;
setOfInts.insert(10);
setOfInts.insert(20);
setOfInts.insert(30);
PLOG_INFO << "std::set<int>: " << setOfInts;

std::map<std::string, int> mapStringToInt;
mapStringToInt["red"] = 1;
mapStringToInt["green"] = 2;
mapStringToInt["blue"] = 4;
PLOG_INFO << "std::map<std::string, int>: " << mapStringToInt;

std::multimap<int, std::string> multimapIntToString;
multimapIntToString.insert(std::make_pair(1, "one"));
multimapIntToString.insert(std::make_pair(1, "uno"));
multimapIntToString.insert(std::make_pair(2, "two"));
multimapIntToString.insert(std::make_pair(2, "due"));
PLOG_INFO << "std::multimap<int, std::string>: " << multimapIntToString;

std::vector<std::vector<int> > vectorOfVectorsOfInts(3);
vectorOfVectorsOfInts[0].push_back(1);
vectorOfVectorsOfInts[0].push_back(2);
vectorOfVectorsOfInts[1].push_back(-1);
vectorOfVectorsOfInts[1].push_back(-2);
PLOG_INFO << "std::vector<std::vector<int> >: " << vectorOfVectorsOfInts;

return 0;
}

0 comments on commit 130801c

Please sign in to comment.