Skip to content

Commit 7c5f68a

Browse files
committed
Code review fixes
1 parent 213cf09 commit 7c5f68a

5 files changed

Lines changed: 40 additions & 29 deletions

File tree

demo/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ set_target_properties(log4cpp_demo PROPERTIES
77
# Link to library
88
target_link_libraries(log4cpp_demo PRIVATE log4cpp)
99

10-
# Copy any JSON configs from demo/ to the binary dir after build
1110
file(GLOB DEMO_CONFIG_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.json")
1211
if (DEMO_CONFIG_FILES)
13-
add_custom_command(TARGET log4cpp_demo POST_BUILD
14-
COMMAND ${CMAKE_COMMAND} -E copy ${DEMO_CONFIG_FILES} $<TARGET_FILE_DIR:log4cpp_demo>
15-
)
12+
# Copy the configuration files to build/bin so that the program can run
13+
file(COPY ${DEMO_CONFIG_FILES} DESTINATION "${PROJECT_BINARY_DIR}/bin")
1614
endif ()
1715

1816
# Apply ASAN if enabled

test/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
4545

4646
# Copy JSON configs at configure time into runtime output dir and test binary dir
4747
if (TEST_CONFIG_FILES)
48-
file(MAKE_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
49-
file(COPY ${TEST_CONFIG_FILES} DESTINATION "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
48+
# build/bin
49+
#file(MAKE_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
50+
#file(COPY ${TEST_CONFIG_FILES} DESTINATION "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
5051

5152
# CTest runs tests with working dir = ${CMAKE_CURRENT_BINARY_DIR} (build/test),
52-
# copy configs there so discovery / tests can open "./log4cpp.json"
53-
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
53+
#file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
5454
file(COPY ${TEST_CONFIG_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
5555
endif ()
5656

test/app/file_appender_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "log4cpp/log4cpp.hpp"
77

88
void info_logger() {
9-
const std::shared_ptr<log4cpp::logger> log = log4cpp::logger_manager::get_logger("info_logger");
9+
const std::shared_ptr<log4cpp::logger> log = log4cpp::logger_manager::get_logger("aaa");
1010
log->trace("this is a trace");
1111
log->debug("this is a debug");
1212
log->info("this is a info");
@@ -16,7 +16,7 @@ void info_logger() {
1616
}
1717

1818
void warn_logger() {
19-
const std::shared_ptr<log4cpp::logger> log = log4cpp::logger_manager::get_logger("warn_logger");
19+
const std::shared_ptr<log4cpp::logger> log = log4cpp::logger_manager::get_logger("bbb");
2020
log->trace("this is a trace");
2121
log->debug("this is a debug");
2222
log->info("this is a info");

test/app/load_config_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ TEST(load_config_test, auto_load_config) {
151151
auto &log_mgr = log4cpp::supervisor::get_logger_manager();
152152
// Calling get_logger() causes automatic loading
153153
const auto logger = log4cpp::logger_manager::get_logger("root");
154+
logger->info("[load_config_test.auto_load_config] Calling get_logger() causes automatic loading");
154155
const log4cpp::config::log4cpp *config = log_mgr.get_config();
155156
ASSERT_NE(nullptr, config);
156157

test/app/serialize_test.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,46 @@
44

55
#include "log4cpp/log4cpp.hpp"
66

7-
#ifdef _MSC_VER
8-
// clang-format off
9-
#include <winsock2.h>
10-
#include <windows.h>
11-
// clang-format on
12-
13-
#endif
14-
157
#include <gtest/gtest.h>
168

179
#include "config/log4cpp.hpp"
1810

11+
namespace fs = std::filesystem;
12+
1913
void parse_json(const std::string &config_file, nlohmann::json &expected_json) {
2014
std::ifstream ifs(config_file);
21-
ASSERT_EQ(ifs.is_open(), true);
15+
ASSERT_TRUE(ifs.is_open());
2216
expected_json = nlohmann::json::parse(ifs);
2317
ifs.close();
2418
}
2519

26-
TEST(configuration_serialize_test, log4cpp_config_serialize_test) {
27-
// Just to load the configuration file
20+
TEST(configuration_serialize_test, log4cpp_config_roundtrip_test) {
2821
auto &log_mgr = log4cpp::supervisor::get_logger_manager();
29-
ASSERT_NO_THROW(log_mgr.load_config("serialize_test.json"));
30-
std::shared_ptr<log4cpp::logger> logger = log4cpp::logger_manager::get_logger("console_logger");
31-
const log4cpp::config::log4cpp *config = log_mgr.get_config();
32-
const std::string actual_json_str = log4cpp::config::log4cpp::serialize(*config);
33-
nlohmann::json expected_json;
34-
parse_json("serialize_test.json", expected_json);
35-
const nlohmann::json actual_json = nlohmann::json::parse(actual_json_str);
36-
EXPECT_EQ(expected_json, actual_json);
22+
23+
for (const auto &entry: fs::directory_iterator(fs::current_path())) {
24+
if (entry.is_regular_file() && entry.path().extension() == ".json") {
25+
const std::string filename = entry.path().string();
26+
27+
// Load original config
28+
ASSERT_NO_THROW(log_mgr.load_config(filename));
29+
const log4cpp::config::log4cpp original_config = *log_mgr.get_config();
30+
31+
// Serialize to JSON string
32+
const std::string json_str = log4cpp::config::log4cpp::serialize(original_config);
33+
34+
// Write to a temporary file
35+
const std::string tmpfile = (fs::temp_directory_path() / entry.path().filename()).string();
36+
{
37+
std::ofstream ofs(tmpfile);
38+
ofs << json_str;
39+
}
40+
41+
// Load config back from the temporary file
42+
ASSERT_NO_THROW(log_mgr.load_config(tmpfile));
43+
const log4cpp::config::log4cpp roundtrip_config = *log_mgr.get_config();
44+
45+
// Compare the two config objects (you may need to implement operator==)
46+
EXPECT_EQ(original_config, roundtrip_config) << "Roundtrip mismatch in file: " << filename;
47+
}
48+
}
3749
}

0 commit comments

Comments
 (0)