Skip to content

Commit 130801c

Browse files
Add support for printing std containers (#207)
1 parent 225c563 commit 130801c

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

include/plog/Record.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,43 @@ namespace plog
7878
plog::detail::operator<<(stream, data.c_str());
7979
}
8080

81+
// Print `std::pair`
82+
template<class T1, class T2>
83+
inline void operator<<(util::nostringstream& stream, const std::pair<T1, T2>& data)
84+
{
85+
stream << data.first;
86+
stream << ":";
87+
stream << data.second;
88+
}
89+
8190
#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
91+
// Print data that can be casted to `std::basic_string`. `+ sizeof(T*)` is required for GCC 4.5-4.7.
8292
template<typename T>
8393
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)
8494
{
8595
plog::detail::operator<<(stream, static_cast<std::basic_string<util::nchar> >(data));
8696
}
97+
98+
// Print std containers
99+
template<class Container>
100+
inline typename meta::enableIf<!!(sizeof(typename Container::const_iterator)), void>::type operator<<(util::nostringstream& stream, const Container& data)
101+
{
102+
stream << "[";
103+
104+
for (typename Container::const_iterator it = data.begin(); it != data.end();)
105+
{
106+
stream << *it;
107+
108+
if (++it == data.end())
109+
{
110+
break;
111+
}
112+
113+
stream << ", ";
114+
}
115+
116+
stream << "]";
117+
}
87118
#endif
88119

89120
#ifdef __cplusplus_cli

samples/Demo/Main.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
#include <plog/Log.h>
66
#include <plog/Initializers/RollingFileInitializer.h>
7+
#include <plog/Formatters/TxtFormatter.h>
8+
#include <plog/Appenders/ColorConsoleAppender.h>
9+
10+
#include <vector>
11+
#include <deque>
12+
#include <list>
13+
#include <map>
14+
#include <set>
715

816
#include "MyClass.h"
917
#include "Customer.h"
@@ -20,7 +28,10 @@ void unmanagedFunc()
2028

2129
int main()
2230
{
23-
plog::init(plog::debug, "Demo.csv", 5000, 3); // Initialize the logger.
31+
plog::init(plog::debug, "Demo.csv", 5000, 3); // Initialize logging to the file.
32+
33+
plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
34+
plog::get()->addAppender(&consoleAppender); // Also add logging to the console.
2435

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

135+
// Std containers can be printed
136+
std::vector<int> vectorOfInts;
137+
vectorOfInts.push_back(1);
138+
vectorOfInts.push_back(2);
139+
vectorOfInts.push_back(3);
140+
PLOG_INFO << "std::vector<int>: " << vectorOfInts;
141+
142+
std::deque<std::string> dequeOfStrings;
143+
dequeOfStrings.push_back("one");
144+
dequeOfStrings.push_back("two");
145+
dequeOfStrings.push_back("three");
146+
PLOG_INFO << "std::deque<std::string>: " << dequeOfStrings;
147+
148+
std::list<const char*> listOfCharPointers;
149+
listOfCharPointers.push_back("one");
150+
listOfCharPointers.push_back("two");
151+
listOfCharPointers.push_back(NULL);
152+
PLOG_INFO << "std::list<const char*>: " << listOfCharPointers;
153+
154+
std::set<int> setOfInts;
155+
setOfInts.insert(10);
156+
setOfInts.insert(20);
157+
setOfInts.insert(30);
158+
PLOG_INFO << "std::set<int>: " << setOfInts;
159+
160+
std::map<std::string, int> mapStringToInt;
161+
mapStringToInt["red"] = 1;
162+
mapStringToInt["green"] = 2;
163+
mapStringToInt["blue"] = 4;
164+
PLOG_INFO << "std::map<std::string, int>: " << mapStringToInt;
165+
166+
std::multimap<int, std::string> multimapIntToString;
167+
multimapIntToString.insert(std::make_pair(1, "one"));
168+
multimapIntToString.insert(std::make_pair(1, "uno"));
169+
multimapIntToString.insert(std::make_pair(2, "two"));
170+
multimapIntToString.insert(std::make_pair(2, "due"));
171+
PLOG_INFO << "std::multimap<int, std::string>: " << multimapIntToString;
172+
173+
std::vector<std::vector<int> > vectorOfVectorsOfInts(3);
174+
vectorOfVectorsOfInts[0].push_back(1);
175+
vectorOfVectorsOfInts[0].push_back(2);
176+
vectorOfVectorsOfInts[1].push_back(-1);
177+
vectorOfVectorsOfInts[1].push_back(-2);
178+
PLOG_INFO << "std::vector<std::vector<int> >: " << vectorOfVectorsOfInts;
179+
124180
return 0;
125181
}

0 commit comments

Comments
 (0)