Skip to content

Commit 1fac522

Browse files
SwooshyCuebalanking
authored andcommitted
[#148] Migrate from rodsLog to irods::experimental::log
1 parent aad77be commit 1fac522

2 files changed

Lines changed: 172 additions & 32 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ target_include_directories(
191191
${IRODS_INCLUDE_DIRS}
192192
${IRODS_EXTERNALS_FULLPATH_BOOST}/include
193193
${IRODS_EXTERNALS_FULLPATH_FMT}/include
194+
${IRODS_EXTERNALS_FULLPATH_SPDLOG}/include
194195
)
195196

196197
target_link_libraries(
@@ -246,6 +247,7 @@ target_compile_definitions(
246247
ENABLE_RE
247248
${IRODS_COMPILE_DEFINITIONS}
248249
${IRODS_COMPILE_DEFINITIONS_PRIVATE}
250+
IRODS_ENABLE_SYSLOG
249251
)
250252

251253
target_compile_options(${PLUGIN} PRIVATE -Wno-deprecated-volatile -Wmissing-field-initializers)

src/main.cpp

Lines changed: 170 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// include this first to fix macro redef warnings
22
#include <pyconfig.h>
33

4+
#include <cstdint>
45
#include <ctime>
56
#include <fstream>
67
#include <list>
@@ -25,6 +26,7 @@
2526
#include <irods/rodsErrorTable.h>
2627
#include <irods/irods_default_paths.hpp>
2728
#include <irods/irods_error.hpp>
29+
#include <irods/irods_logger.hpp>
2830
#include <irods/irods_re_plugin.hpp>
2931
#include <irods/irods_re_structs.hpp>
3032
#include <irods/irods_re_ruleexistshelper.hpp>
@@ -51,8 +53,6 @@
5153

5254
#include <fmt/format.h>
5355

54-
namespace logger = irods::experimental::log;
55-
5656
irods::ms_table& get_microservice_table();
5757

5858
// writeLine is not in the microservice table in 4.2.0 - #3408
@@ -101,17 +101,35 @@ namespace bp = boost::python;
101101

102102
static std::recursive_mutex python_mutex;
103103

104+
namespace
105+
{
106+
const char* const rule_engine_name = "python";
107+
using log_re = irods::experimental::log::rule_engine;
108+
}
109+
104110
void register_regexes_from_array(const nlohmann::json& _array, const std::string& _instance_name)
105111
{
106112
try {
107113
for (const auto& elem : _array) {
108114
try {
109115
const auto& tmp = elem.get_ref<const std::string&>();
110116
RuleExistsHelper::Instance()->registerRuleRegex(tmp);
111-
logger::rule_engine::debug("register_regexes_from_array - regex: [{}]", tmp);
117+
// clang-format off
118+
log_re::debug({
119+
{"rule_engine_plugin", rule_engine_name},
120+
{"instance_name", _instance_name},
121+
{"regex", tmp},
122+
});
123+
// clang-format on
112124
}
113125
catch (const boost::bad_any_cast&) {
114-
logger::rule_engine::error("[{}] - failed to cast pep_regex_to_match to string", __FUNCTION__);
126+
// clang-format off
127+
log_re::error({
128+
{"rule_engine_plugin", rule_engine_name},
129+
{"instance_name", _instance_name},
130+
{"log_message", "failed to cast pep_regex_to_match to string"},
131+
});
132+
// clang-format on
115133
continue;
116134
}
117135
}
@@ -196,12 +214,22 @@ namespace
196214
irods::error err = effect_handler("unsafe_ms_ctx", &rei);
197215

198216
if (!err.ok()) {
199-
rodsLog(LOG_ERROR, "Could not retrieve RuleExecInfo_t object from effect handler");
217+
// clang-format off
218+
log_re::error({
219+
{"rule_engine_plugin", rule_engine_name},
220+
{"log_message", "Could not retrieve RuleExecInfo_t object from effect handler"},
221+
});
222+
// clang-format on
200223
return nullptr;
201224
}
202225

203226
if (!rei) {
204-
rodsLog(LOG_ERROR, "RuleExecInfo object is NULL - cannot populate session vars");
227+
// clang-format off
228+
log_re::error({
229+
{"rule_engine_plugin", rule_engine_name},
230+
{"log_message", "RuleExecInfo object is NULL - cannot populate session vars"},
231+
});
232+
// clang-format on
205233
return nullptr;
206234
}
207235

@@ -318,9 +346,20 @@ irods::error start(irods::default_re_ctx&, const std::string& _instance_name)
318346
#ifdef IRODS_PYTHON_SONAME
319347
// Force python library to be loaded
320348
void* p = dlopen(IRODS_PYTHON_SONAME, RTLD_LAZY | RTLD_GLOBAL);
321-
rodsLog(LOG_DEBUG, "dlopen returned: [%p]", p);
349+
350+
// clang-format off
351+
log_re::debug({
352+
{"rule_engine_plugin", rule_engine_name},
353+
{"dlopen_return_value", std::to_string(reinterpret_cast<std::uintptr_t>(p))},
354+
});
355+
// clang-format on
322356
if (!p) {
323-
rodsLog(LOG_DEBUG, "dlerror gives : [%s]", dlerror());
357+
// clang-format off
358+
log_re::debug({
359+
{"rule_engine_plugin", rule_engine_name},
360+
{"dlerror_return_value", dlerror()},
361+
});
362+
// clang-format on
324363
}
325364
#endif
326365

@@ -351,7 +390,14 @@ irods::error start(irods::default_re_ctx&, const std::string& _instance_name)
351390
}
352391
catch (const bp::error_already_set&) {
353392
const std::string formatted_python_exception = extract_python_exception();
354-
rodsLog(LOG_ERROR, "caught python exception: %s", formatted_python_exception.c_str());
393+
// clang-format off
394+
log_re::error({
395+
{"rule_engine_plugin", rule_engine_name},
396+
{"instance_name", _instance_name},
397+
{"log_message", "caught python exception"},
398+
{"python_exception", formatted_python_exception},
399+
});
400+
// clang-format on
355401
std::string err_msg = std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ +
356402
" Caught Python exception.\n" + formatted_python_exception;
357403
return ERROR(RULE_ENGINE_ERROR, err_msg);
@@ -387,10 +433,15 @@ irods::error start(irods::default_re_ctx&, const std::string& _instance_name)
387433
RuleExistsHelper::Instance()->registerRuleRegex(STATIC_PEP_RULE_REGEX);
388434
RuleExistsHelper::Instance()->registerRuleRegex(DYNAMIC_PEP_RULE_REGEX);
389435

390-
rodsLog(LOG_DEBUG,
391-
"No regexes found in server_config for Python RE - using default regexes: [%s], [%s]",
392-
STATIC_PEP_RULE_REGEX.c_str(),
393-
DYNAMIC_PEP_RULE_REGEX.c_str());
436+
// clang-format off
437+
log_re::debug({
438+
{"rule_engine_plugin", rule_engine_name},
439+
{"instance_name", _instance_name},
440+
{"log_message", "No regexes found in server_config for Python RE - using default regexes"},
441+
{"static_pep_rule_regex", STATIC_PEP_RULE_REGEX},
442+
{"dynamic_pep_rule_regex", DYNAMIC_PEP_RULE_REGEX},
443+
});
444+
// clang-format on
394445
}
395446

396447
return SUCCESS();
@@ -407,9 +458,15 @@ irods::error start(irods::default_re_ctx&, const std::string& _instance_name)
407458
return ERROR(KEY_NOT_FOUND, e.what());
408459
}
409460

461+
// clang-format off
462+
log_re::error({
463+
{"rule_engine_plugin", rule_engine_name},
464+
{"instance_name", _instance_name},
465+
{"log_message", "failed to find configuration for plugin"},
466+
});
467+
// clang-format on
410468
std::stringstream msg;
411469
msg << "failed to find configuration for re-python plugin [" << _instance_name << "]";
412-
rodsLog(LOG_ERROR, "%s", msg.str().c_str());
413470
return ERROR(SYS_INVALID_INPUT_PARAM, msg.str());
414471
}
415472

@@ -433,7 +490,13 @@ irods::error rule_exists(const irods::default_re_ctx&, const std::string& rule_n
433490
}
434491
catch (const bp::error_already_set&) {
435492
const std::string formatted_python_exception = extract_python_exception();
436-
rodsLog(LOG_ERROR, "caught python exception: %s", formatted_python_exception.c_str());
493+
// clang-format off
494+
log_re::error({
495+
{"rule_engine_plugin", rule_engine_name},
496+
{"log_message", "caught python exception"},
497+
{"python_exception", formatted_python_exception},
498+
});
499+
// clang-format on
437500
std::string err_msg = std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ +
438501
" Caught Python exception.\n" + formatted_python_exception;
439502
return ERROR(RULE_ENGINE_ERROR, err_msg);
@@ -467,7 +530,13 @@ irods::error list_rules(const irods::default_re_ctx&, std::vector<std::string>&
467530
}
468531
catch (const bp::error_already_set&) {
469532
const std::string formatted_python_exception = extract_python_exception();
470-
rodsLog(LOG_ERROR, "caught python exception: %s", formatted_python_exception.c_str());
533+
// clang-format off
534+
log_re::error({
535+
{"rule_engine_plugin", rule_engine_name},
536+
{"log_message", "caught python exception"},
537+
{"python_exception", formatted_python_exception},
538+
});
539+
// clang-format on
471540
auto start_pos = formatted_python_exception.find(IRODS_ERROR_PREFIX);
472541
int error_code_int = -1;
473542
if (start_pos != std::string::npos) {
@@ -481,7 +550,13 @@ irods::error list_rules(const irods::default_re_ctx&, std::vector<std::string>&
481550
return ERROR(error_code_int, err_msg);
482551
}
483552
catch (const boost::bad_any_cast& e) {
484-
rodsLog(LOG_ERROR, "bad any cast : %s", e.what());
553+
// clang-format off
554+
log_re::error({
555+
{"rule_engine_plugin", rule_engine_name},
556+
{"log_message", "bad any cast"},
557+
{"exception", e.what()},
558+
});
559+
// clang-format on
485560
std::string err_msg =
486561
std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ + " bad_any_cast : " + e.what();
487562
return ERROR(INVALID_ANY_CAST, e.what());
@@ -528,7 +603,13 @@ irods::error exec_rule(const irods::default_re_ctx&,
528603
}
529604
catch (const bp::error_already_set&) {
530605
const std::string formatted_python_exception = extract_python_exception();
531-
rodsLog(LOG_ERROR, "caught python exception: %s", formatted_python_exception.c_str());
606+
// clang-format off
607+
log_re::error({
608+
{"rule_engine_plugin", rule_engine_name},
609+
{"log_message", "caught python exception"},
610+
{"python_exception", formatted_python_exception},
611+
});
612+
// clang-format on
532613
auto start_pos = formatted_python_exception.find(IRODS_ERROR_PREFIX);
533614
int error_code_int = -1;
534615
if (start_pos != std::string::npos) {
@@ -542,13 +623,24 @@ irods::error exec_rule(const irods::default_re_ctx&,
542623
return ERROR(error_code_int, err_msg);
543624
}
544625
catch (const boost::bad_any_cast& e) {
545-
rodsLog(LOG_ERROR, "bad any cast : %s", e.what());
626+
// clang-format off
627+
log_re::error({
628+
{"rule_engine_plugin", rule_engine_name},
629+
{"log_message", "bad any cast"},
630+
{"exception", e.what()},
631+
});
632+
// clang-format on
546633
std::string err_msg =
547634
std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ + " bad_any_cast : " + e.what();
548635
return ERROR(INVALID_ANY_CAST, e.what());
549636
}
550637
catch (...) {
551-
logger::rule_engine::error("Caught unknown exception [ec={}].", SYS_UNKNOWN_ERROR);
638+
// clang-format off
639+
log_re::error({
640+
{"rule_engine_plugin", rule_engine_name},
641+
{"log_message", "Caught unknown exception"},
642+
});
643+
// clang-format on
552644
return ERROR(SYS_UNKNOWN_ERROR, "Caught unknown exception.");
553645
}
554646

@@ -566,7 +658,12 @@ irods::error exec_rule_text(const irods::default_re_ctx&,
566658
const auto rei = get_rei_from_effect_handler(effect_handler);
567659

568660
if (!rei) {
569-
rodsLog(LOG_ERROR, "RuleExecInfo object is NULL - cannot authenticate user");
661+
// clang-format off
662+
log_re::error({
663+
{"rule_engine_plugin", rule_engine_name},
664+
{"log_message", "RuleExecInfo object is NULL - cannot authenticate user"},
665+
});
666+
// clang-format on
570667
return ERROR(NULL_VALUE_ERR, "Null rei pointer in exec_rule_text");
571668
}
572669

@@ -587,7 +684,12 @@ irods::error exec_rule_text(const irods::default_re_ctx&,
587684
}
588685

589686
if ((client_user_authflag < REMOTE_PRIV_USER_AUTH) || (proxy_user_authflag < REMOTE_PRIV_USER_AUTH)) {
590-
rodsLog(LOG_DEBUG, "Insufficient privileges to run irule in Python rule engine plugin");
687+
// clang-format off
688+
log_re::debug({
689+
{"rule_engine_plugin", rule_engine_name},
690+
{"log_message", "Insufficient privileges to run irule in Python rule engine plugin"},
691+
});
692+
// clang-format on
591693
return ERROR(SYS_NO_API_PRIV, "Insufficient privileges to run irule in Python rule engine plugin");
592694
}
593695

@@ -678,28 +780,47 @@ irods::error exec_rule_text(const irods::default_re_ctx&,
678780
return to_irods_error_object(rule_function(rule_arguments_python, CallbackWrapper{effect_handler}, rei));
679781
}
680782
else {
681-
rodsLog(LOG_ERROR,
682-
"[%s:%d] Improperly formatted rule text in Python rule engine plugin",
683-
__FUNCTION__,
684-
__LINE__);
783+
// clang-format off
784+
log_re::error({
785+
{"rule_engine_plugin", rule_engine_name},
786+
{"log_message", "Improperly formatted rule text"},
787+
});
788+
// clang-format on
685789
return ERROR(RULE_ENGINE_ERROR, "Improperly formatted rule_text");
686790
}
687791
}
688792
catch (const bp::error_already_set&) {
689793
const std::string formatted_python_exception = extract_python_exception();
690-
rodsLog(LOG_ERROR, "caught python exception: %s", formatted_python_exception.c_str());
794+
// clang-format off
795+
log_re::error({
796+
{"rule_engine_plugin", rule_engine_name},
797+
{"log_message", "caught python exception"},
798+
{"python_exception", formatted_python_exception},
799+
});
800+
// clang-format on
691801
std::string err_msg = std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ +
692802
" Caught Python exception.\n" + formatted_python_exception;
693803
return ERROR(RULE_ENGINE_ERROR, err_msg);
694804
}
695805
catch (const boost::bad_any_cast& e) {
696-
rodsLog(LOG_ERROR, "bad any cast : %s", e.what());
806+
// clang-format off
807+
log_re::error({
808+
{"rule_engine_plugin", rule_engine_name},
809+
{"log_message", "bad any cast"},
810+
{"exception", e.what()},
811+
});
812+
// clang-format on
697813
std::string err_msg =
698814
std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ + " bad_any_cast : " + e.what();
699815
return ERROR(INVALID_ANY_CAST, e.what());
700816
}
701817
catch (...) {
702-
logger::rule_engine::error("Caught unknown exception [ec={}].", SYS_UNKNOWN_ERROR);
818+
// clang-format off
819+
log_re::error({
820+
{"rule_engine_plugin", rule_engine_name},
821+
{"log_message", "Caught unknown exception"},
822+
});
823+
// clang-format on
703824
return ERROR(SYS_UNKNOWN_ERROR, "Caught unknown exception.");
704825
}
705826

@@ -774,19 +895,36 @@ irods::error exec_rule_expression(irods::default_re_ctx&,
774895
}
775896
catch (const bp::error_already_set&) {
776897
const std::string formatted_python_exception = extract_python_exception();
777-
rodsLog(LOG_ERROR, "caught python exception: %s", formatted_python_exception.c_str());
898+
// clang-format off
899+
log_re::error({
900+
{"rule_engine_plugin", rule_engine_name},
901+
{"log_message", "caught python exception"},
902+
{"python_exception", formatted_python_exception},
903+
});
904+
// clang-format on
778905
std::string err_msg = std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ +
779906
" Caught Python exception.\n" + formatted_python_exception;
780907
return ERROR(RULE_ENGINE_ERROR, err_msg);
781908
}
782909
catch (const boost::bad_any_cast& e) {
783-
rodsLog(LOG_ERROR, "bad any cast : %s", e.what());
910+
// clang-format off
911+
log_re::error({
912+
{"rule_engine_plugin", rule_engine_name},
913+
{"log_message", "bad any cast"},
914+
{"exception", e.what()},
915+
});
916+
// clang-format on
784917
std::string err_msg =
785918
std::string("irods_rule_engine_plugin_python::") + __PRETTY_FUNCTION__ + " bad_any_cast : " + e.what();
786919
return ERROR(INVALID_ANY_CAST, e.what());
787920
}
788921
catch (...) {
789-
logger::rule_engine::error("Caught unknown exception [ec={}].", SYS_UNKNOWN_ERROR);
922+
// clang-format off
923+
log_re::error({
924+
{"rule_engine_plugin", rule_engine_name},
925+
{"log_message", "Caught unknown exception"},
926+
});
927+
// clang-format on
790928
return ERROR(SYS_UNKNOWN_ERROR, "Caught unknown exception.");
791929
}
792930

0 commit comments

Comments
 (0)