Skip to content

Commit fb189bb

Browse files
Logging std::filesystem::path without explicit conversion to std::string (#168)
1 parent 47883f0 commit fb189bb

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

include/plog/Record.h

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ namespace plog
1111
{
1212
namespace detail
1313
{
14+
namespace meta
15+
{
16+
template<class T>
17+
inline T& declval()
18+
{
19+
#ifdef __INTEL_COMPILER
20+
# pragma warning(suppress: 327) // NULL reference is not allowed
21+
#endif
22+
return *reinterpret_cast<T*>(0);
23+
}
24+
25+
template<bool B, class T = void>
26+
struct enableIf {};
27+
28+
template<class T>
29+
struct enableIf<true, T> { typedef T type; };
30+
}
31+
1432
//////////////////////////////////////////////////////////////////////////
1533
// Stream output operators as free functions
1634

@@ -27,11 +45,22 @@ namespace plog
2745
#endif
2846
}
2947

48+
inline void operator<<(util::nostringstream& stream, char* data)
49+
{
50+
plog::detail::operator<<(stream, const_cast<const char*>(data));
51+
}
52+
3053
inline void operator<<(util::nostringstream& stream, const std::string& data)
3154
{
3255
plog::detail::operator<<(stream, data.c_str());
3356
}
3457

58+
template<typename T>
59+
inline typename meta::enableIf<sizeof(static_cast<std::basic_string<util::nchar> >(meta::declval<T>())), void>::type operator<<(util::nostringstream& stream, const T& data)
60+
{
61+
plog::detail::operator<<(stream, static_cast<std::basic_string<util::nchar> >(data));
62+
}
63+
3564
#if PLOG_ENABLE_WCHAR_INPUT
3665
inline void operator<<(util::nostringstream& stream, const wchar_t* data)
3766
{
@@ -44,6 +73,11 @@ namespace plog
4473
# endif
4574
}
4675

76+
inline void operator<<(util::nostringstream& stream, wchar_t* data)
77+
{
78+
plog::detail::operator<<(stream, const_cast<const wchar_t*>(data));
79+
}
80+
4781
inline void operator<<(util::nostringstream& stream, const std::wstring& data)
4882
{
4983
plog::detail::operator<<(stream, data.c_str());
@@ -67,10 +101,7 @@ namespace plog
67101
template <class T, class Stream>
68102
struct isStreamable
69103
{
70-
#ifdef __INTEL_COMPILER
71-
# pragma warning(suppress: 327) // NULL reference is not allowed
72-
#endif
73-
enum { value = sizeof(operator<<(*reinterpret_cast<Stream*>(0), *reinterpret_cast<const T*>(0))) != sizeof(char) };
104+
enum { value = sizeof(operator<<(meta::declval<Stream>(), meta::declval<const T>())) != sizeof(char) };
74105
};
75106

76107
template <class Stream>
@@ -90,12 +121,6 @@ namespace plog
90121
{
91122
enum { value = false };
92123
};
93-
94-
template<bool B, class T = void>
95-
struct enableIf {};
96-
97-
template<class T>
98-
struct enableIf<true, T> { typedef T type; };
99124
}
100125

101126
template<class T>

samples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ add_subdirectory(Library)
5555
add_subdirectory(MultiAppender)
5656
add_subdirectory(MultiInstance)
5757
add_subdirectory(ObjectiveC)
58+
add_subdirectory(Path)
5859
add_subdirectory(Performance)
5960
add_subdirectory(SetFileName)
6061
add_subdirectory(Shared)

samples/Path/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if (CMAKE_VERSION VERSION_GREATER 3.1.0)
2+
if ((CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) OR (MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 19.10))
3+
add_executable(Path Main.cpp)
4+
target_link_libraries(Path plog)
5+
set_target_properties(Path PROPERTIES FOLDER Samples)
6+
set_target_properties(Path PROPERTIES CXX_STANDARD 17)
7+
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
8+
target_link_libraries(Path stdc++fs)
9+
endif()
10+
endif()
11+
endif()

samples/Path/Main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Path - a test sample to check that std::filesystem::path can be logged.
3+
//
4+
5+
#include <plog/Log.h>
6+
7+
#if __has_include(<filesystem>)
8+
#include <filesystem>
9+
namespace fs = std::filesystem;
10+
#else
11+
#include <experimental/filesystem>
12+
namespace fs = std::experimental::filesystem;
13+
#endif
14+
15+
int main()
16+
{
17+
plog::init(plog::debug, "Path.txt");
18+
19+
PLOGI << "Current path: " << fs::current_path();
20+
21+
return 0;
22+
}

0 commit comments

Comments
 (0)