File Coverage

src/xs/xlog.cc
Criterion Covered Total %
statement 28 28 100.0
branch 31 62 50.0
condition n/a
subroutine n/a
pod n/a
total 59 90 65.5


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