diff --git a/CMakeLists.txt b/CMakeLists.txt index 34c9697ac..af5718ad1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,7 @@ target_link_libraries( nlohmann_json::nlohmann_json fmt::fmt ${IRODS_EXTERNALS_FULLPATH_QPID_PROTON}/lib/libqpid-proton-cpp.so + ${IRODS_EXTERNALS_FULLPATH_BOOST}/lib/libboost_filesystem.so ${IRODS_EXTERNALS_FULLPATH_BOOST}/lib/libboost_system.so ${CMAKE_DL_LIBS} ) diff --git a/src/main.cpp b/src/main.cpp index 76fe39c5d..240ea0f13 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "irods/private/audit_amqp.hpp" #include "irods/private/audit_b64enc.hpp" #include "irods/private/amqp_sender.hpp" +#include #include #include #include @@ -212,6 +213,31 @@ namespace irods::plugin::rule_engine::audit_amqp std::string msg_str; + irods::at_scope_exit write_msg_to_test_log{[&] { + log_re::trace("{}: RUNNING AT_SCOPE_EXIT FOR WRITING TO FSTREAM.", __func__); + if (test_mode) { + if (log_file_path.empty()) { + log_re::trace("{}: log_file_path is empty. cannot log audit message to test file.", __func__); + return; + } + + if (!log_file_ofstream.is_open()) { + boost::system::error_code ec; + boost::filesystem::create_directories(log_file_path.parent_path(), ec); + + log_re::trace("{}: opening log_file_ofstream [{}].", __func__, log_file_path.c_str()); + log_file_ofstream.open(log_file_path); + } + + if (!log_file_ofstream) { + log_re::trace("{}: log_file_ofstream not in a good state.", __func__); + } + + log_re::trace("{}: writing amqp message to log_file_ofstream [{}].", __func__, log_file_path.c_str()); + log_file_ofstream << msg_str << std::endl; + } + }}; + try { std::uint64_t time_ms = ts_clock::now().time_since_epoch() / std::chrono::milliseconds(1); json_obj["@timestamp"] = time_ms; @@ -251,13 +277,6 @@ namespace irods::plugin::rule_engine::audit_amqp return ERROR(SYS_UNKNOWN_ERROR, "an unknown error occurred"); } - if (test_mode) { - if (!log_file_ofstream.is_open()) { - log_file_ofstream.open(log_file_path); - } - log_file_ofstream << msg_str << std::endl; - } - return SUCCESS(); }