Skip to content

Commit

Permalink
add more restrict for write log with data/str (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Mar 29, 2024
1 parent 119abbc commit 38e2b7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/ylt/easylog/record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ template <typename Type, typename = void>
struct has_data : std::false_type {};

template <typename T>
struct has_data<T, std::void_t<decltype(std::declval<T>().data())>>
: std::true_type {};
struct has_data<T, std::void_t<decltype(std::declval<std::string>().append(
std::declval<T>().data()))>> : std::true_type {};

template <typename T>
constexpr inline bool has_data_v = has_data<std::remove_cvref_t<T>>::value;
Expand All @@ -73,8 +73,8 @@ template <typename Type, typename = void>
struct has_str : std::false_type {};

template <typename T>
struct has_str<T, std::void_t<decltype(std::declval<T>().str())>>
: std::true_type {};
struct has_str<T, std::void_t<decltype(std::declval<std::string>().append(
std::declval<T>().str()))>> : std::true_type {};

template <typename T>
constexpr inline bool has_str_v = has_str<std::remove_cvref_t<T>>::value;
Expand Down
16 changes: 16 additions & 0 deletions src/easylog/tests/test_easylog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ TEST_CASE("test severity") {
easylog::severity_str(easylog::Severity::FATAL));
}

struct dummy_t {
int* data() const {
static int val = 42;
return &val;
}

friend std::ostream& operator<<(std::ostream& os, const dummy_t& t);
};

std::ostream& operator<<(std::ostream& os, const dummy_t& t) {
os << t.data();
return os;
}

TEST_CASE("test basic") {
std::string filename = "easylog.txt";
std::filesystem::remove(filename);
Expand All @@ -65,6 +79,8 @@ TEST_CASE("test basic") {
ELOG_INFO << "info log";
easylog::set_min_severity(Severity::DEBUG);

ELOG_INFO << dummy_t{};

std::unique_ptr<int> ptr(new int(42));
ELOG_INFO << ptr.get();
ELOG_INFO << 42 << " " << 4.5 << 'a' << Severity::DEBUG;
Expand Down

0 comments on commit 38e2b7b

Please sign in to comment.