diff --git a/include/plog/Record.h b/include/plog/Record.h index c0c4e87..c8fadda 100644 --- a/include/plog/Record.h +++ b/include/plog/Record.h @@ -157,14 +157,6 @@ namespace plog } #endif //__cpp_char8_t - // Print `std::pair` - template - inline void operator<<(util::nostringstream& stream, const std::pair& 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` @@ -175,29 +167,6 @@ namespace plog plog::detail::operator<<(stream, static_cast(data)); } - // Print std containers - template - inline typename meta::enableIf::value && - !meta::isConvertibleToNString::value && - !meta::isConvertibleToString::value && - !meta::isFilesystemPath::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 @@ -316,8 +285,10 @@ namespace plog # endif #endif - template - Record& operator<<(const T& data) + template + inline typename detail::meta::enableIf::value || + detail::meta::isConvertibleToNString::value || + detail::meta::isConvertibleToString::value, Record &>::type operator<<(const T &data) { using namespace plog::detail; @@ -325,6 +296,51 @@ namespace plog return *this; } + Record &operator<<(const std::string &data) + { + m_message << data.c_str(); + return *this; + } + + template + inline typename detail::meta::enableIf::value && + !detail::meta::isConvertibleToNString::value && + !detail::meta::isConvertibleToString::value && + !detail::meta::isFilesystemPath::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 + inline Record& operator<<(const std::pair &data) + { + *this << data.first; + *this << ":"; + *this << data.second; + return *this; + } + #ifndef __cplusplus_cli Record& printf(const char* format, ...) { diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 3bfd15e..98f75b2 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -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) diff --git a/samples/Container/CMakeLists.txt b/samples/Container/CMakeLists.txt new file mode 100644 index 0000000..61f154d --- /dev/null +++ b/samples/Container/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(Container Main.cpp) +target_link_libraries(Container plog) +set_target_properties(Container PROPERTIES FOLDER Samples) diff --git a/samples/Container/Main.cpp b/samples/Container/Main.cpp new file mode 100644 index 0000000..783a42c --- /dev/null +++ b/samples/Container/Main.cpp @@ -0,0 +1,71 @@ +// +// Container - shows how to log std containers. +// + +#include +#include +#include +#include +#include +#include + +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 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 map{ + {"FirstKey", data1}, + {"SecondKey", data2}}; + + PLOGN << map; +} + +int main() +{ + static plog::ColorConsoleAppender consoleAppender; + plog::init(plog::verbose, &consoleAppender); + + logSingle(); + logContainer(); +} \ No newline at end of file