| 1 |
|
| #include "xlog.h" |
| 2 |
|
| #include <xs/function.h> |
| 3 |
|
| |
| 4 |
|
| using namespace xs; |
| 5 |
|
| using namespace panda; |
| 6 |
|
| using namespace panda::log; |
| 7 |
|
| |
| 8 |
|
| struct PerlSubLogger : ILogger { |
| 9 |
|
| using fn_t = function<void(const string&, Level)>; |
| 10 |
|
| fn_t fn; |
| 11 |
|
| |
| 12 |
16 |
▶ | PerlSubLogger (const Sub& sub) : fn(xs::in<fn_t>(sub)) {} |
Branch: 1 branch
| Branch | True | False | Total |
| PerlSubLogger (const Sub& sub) : fn(xs::in<fn_t>(sub)) {} |
16 |
0 |
16 |
|
| 13 |
|
| |
| 14 |
19 |
| void log (const string& msg, const Info& info) override { |
| 15 |
19 |
▶ | if (!is_perl_thread()) throw std::logic_error("can't call pure-perl logging callback: log() called from perl-foreign thread"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!is_perl_thread()) throw std::logic_error("can't call pure-perl logging callback: log() called from perl-foreign thread"); |
0 |
19 |
19 |
| if (!is_perl_thread()) throw std::logic_error("can't call pure-perl logging callback: log() called from perl-foreign thread"); |
0 |
0 |
0 |
|
| 16 |
19 |
| fn(msg, info.level); |
| 17 |
19 |
| } |
| 18 |
|
| }; |
| 19 |
|
| |
| 20 |
|
| struct PerlSubFormatter : IFormatter { |
| 21 |
|
| using fn_t = function<string(const std::string&, Level, const string&, string_view, uint32_t, string_view)>; |
| 22 |
|
| fn_t fn; |
| 23 |
|
| |
| 24 |
3 |
▶ | PerlSubFormatter (const Sub& sub) : fn(xs::in<fn_t>(sub)) {} |
Branch: 1 branch
| Branch | True | False | Total |
| PerlSubFormatter (const Sub& sub) : fn(xs::in<fn_t>(sub)) {} |
3 |
0 |
3 |
|
| 25 |
|
| |
| 26 |
7 |
| string format (std::string& msg, const Info& info) const override { |
| 27 |
7 |
▶ | if (!is_perl_thread()) throw std::logic_error("can't call pure-perl formatting callback: log() called from perl-foreign thread"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!is_perl_thread()) throw std::logic_error("can't call pure-perl formatting callback: log() called from perl-foreign thread"); |
0 |
7 |
7 |
| if (!is_perl_thread()) throw std::logic_error("can't call pure-perl formatting callback: log() called from perl-foreign thread"); |
0 |
0 |
0 |
|
| 28 |
7 |
| return fn(msg, info.level, info.module->name(), info.file, info.line, info.func); |
| 29 |
|
| } |
| 30 |
|
| }; |
| 31 |
|
| |
| 32 |
8 |
| static bool _init () { |
| 33 |
8 |
▶ | xs::at_perl_destroy([]{ |
Branch: 2 branches
| Branch | True | False | Total |
| xs::at_perl_destroy([]{ |
8 |
0 |
8 |
| xs::at_perl_destroy([]{ |
8 |
0 |
8 |
|
| 34 |
|
| // remove all perl loggers and formatters from C++ modules as they are no longer available |
| 35 |
8 |
▶ | auto modules = get_modules(); |
Branch: 1 branch
| Branch | True | False | Total |
| auto modules = get_modules(); |
8 |
0 |
8 |
|
| 36 |
22 |
▶ | for (auto module : modules) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (auto module : modules) { |
14 |
8 |
22 |
|
| 37 |
14 |
▶ | if (dyn_cast<PerlSubLogger*>(module->get_logger().get())) module->set_logger(nullptr); |
Branch: 5 branches
| Branch | True | False | Total |
| if (dyn_cast<PerlSubLogger*>(module->get_logger().get())) module->set_logger(nullptr); |
14 |
0 |
14 |
| if (dyn_cast<PerlSubLogger*>(module->get_logger().get())) module->set_logger(nullptr); |
14 |
0 |
14 |
| if (dyn_cast<PerlSubLogger*>(module->get_logger().get())) module->set_logger(nullptr); |
0 |
14 |
14 |
| if (dyn_cast<PerlSubLogger*>(module->get_logger().get())) module->set_logger(nullptr); |
0 |
0 |
0 |
| if (dyn_cast<PerlSubLogger*>(module->get_logger().get())) module->set_logger(nullptr); |
0 |
0 |
0 |
|
| 38 |
14 |
▶ | if (dyn_cast<PerlSubFormatter*>(module->get_formatter().get())) module->set_formatter(nullptr); |
Branch: 5 branches
| Branch | True | False | Total |
| if (dyn_cast<PerlSubFormatter*>(module->get_formatter().get())) module->set_formatter(nullptr); |
14 |
0 |
14 |
| if (dyn_cast<PerlSubFormatter*>(module->get_formatter().get())) module->set_formatter(nullptr); |
14 |
0 |
14 |
| if (dyn_cast<PerlSubFormatter*>(module->get_formatter().get())) module->set_formatter(nullptr); |
0 |
14 |
14 |
| if (dyn_cast<PerlSubFormatter*>(module->get_formatter().get())) module->set_formatter(nullptr); |
0 |
0 |
0 |
| if (dyn_cast<PerlSubFormatter*>(module->get_formatter().get())) module->set_formatter(nullptr); |
0 |
0 |
0 |
|
| 39 |
|
| } |
| 40 |
8 |
| }); |
| 41 |
8 |
| return true; |
| 42 |
|
| } |
| 43 |
|
| static bool __init = _init(); |
| 44 |
|
| |
| 45 |
|
| namespace xs { |
| 46 |
|
| |
| 47 |
67 |
| ILoggerSP Typemap<ILoggerSP>::in (Sv sv) { |
| 48 |
67 |
▶ | if (sv.is_sub_ref()) return new PerlSubLogger(sv); |
Branch: 4 branches
| Branch | True | False | Total |
| if (sv.is_sub_ref()) return new PerlSubLogger(sv); |
16 |
51 |
67 |
| if (sv.is_sub_ref()) return new PerlSubLogger(sv); |
16 |
0 |
16 |
| if (sv.is_sub_ref()) return new PerlSubLogger(sv); |
16 |
0 |
16 |
| if (sv.is_sub_ref()) return new PerlSubLogger(sv); |
0 |
0 |
0 |
|
| 49 |
51 |
| return xs::in<ILogger*>(sv); |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
58 |
| IFormatterSP Typemap<IFormatterSP>::in (Sv sv) { |
| 53 |
58 |
▶ | if (sv.is_sub_ref()) return new PerlSubFormatter(sv); |
Branch: 4 branches
| Branch | True | False | Total |
| if (sv.is_sub_ref()) return new PerlSubFormatter(sv); |
3 |
55 |
58 |
| if (sv.is_sub_ref()) return new PerlSubFormatter(sv); |
3 |
0 |
3 |
| if (sv.is_sub_ref()) return new PerlSubFormatter(sv); |
3 |
0 |
3 |
| if (sv.is_sub_ref()) return new PerlSubFormatter(sv); |
0 |
0 |
0 |
|
| 54 |
55 |
▶ | if (sv.is_string()) return new PatternFormatter(xs::in<string_view>(sv)); |
Branch: 4 branches
| Branch | True | False | Total |
| if (sv.is_string()) return new PatternFormatter(xs::in<string_view>(sv)); |
30 |
25 |
55 |
| if (sv.is_string()) return new PatternFormatter(xs::in<string_view>(sv)); |
30 |
0 |
30 |
| if (sv.is_string()) return new PatternFormatter(xs::in<string_view>(sv)); |
30 |
0 |
30 |
| if (sv.is_string()) return new PatternFormatter(xs::in<string_view>(sv)); |
0 |
0 |
0 |
|
| 55 |
25 |
| return xs::in<IFormatter*>(sv); |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| } |
| 59 |
|
| |