Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#138] Create log directory for testing before opening log file (main) #143

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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}
)
Expand Down
33 changes: 26 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "irods/private/audit_amqp.hpp"
#include "irods/private/audit_b64enc.hpp"
#include "irods/private/amqp_sender.hpp"
#include <irods/irods_at_scope_exit.hpp>
#include <irods/irods_logger.hpp>
#include <irods/irods_re_plugin.hpp>
#include <irods/irods_re_serialization.hpp>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Loading