xs/XLog.xsi

filter: Statementstmt 100% 29 / 29 Branchbran 44.5% 33 / 74 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.1% 62 / 103 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« t/lib/MyTest_xsgen.cc ↑ Summary xs/Logger.xsi »
1 MODE: INLINE
2 #include "util.h"
3 using namespace xs::xlog;
4  
5 namespace {
6 static bool no_format_warnings;
7  
8 struct PerlObjectFormatter : IFormatter, Backref {
9 string format (std::string& msg, const Info& i) const override {
10 if (!is_perl_thread()) throw std::logic_error("can't call pure-perl formatting callback: log() called from perl-foreign thread");
11 Object o = xs::out(this);
12 auto ret = o.call("format", xs::out(msg), xs::out(i.level), xs::out(i.module->name()), xs::out(i.file), xs::out(i.line), xs::out(i.func));
13 return xs::in<string>(ret);
14 }
15
16 ~PerlObjectFormatter () { Backref::dtor(); }
17 };
18
19 struct PerlObjectLogger : ILogger, Backref {
20 void log_format (std::string& msg, const Info& i, const IFormatter& fmt) override {
21 if (!is_perl_thread()) throw std::logic_error("can't call pure-perl logging callback: log() called from perl-foreign thread");
22 Object o = xs::out(this);
23 auto sub = o.method("log_format");
24 if (!sub) return ILogger::log_format(msg, i, fmt);
25 sub.call(o.ref(), xs::out(msg), xs::out(i.level), xs::out(i.module->name()), xs::out(i.file), xs::out(i.line), xs::out(i.func), xs::out(&fmt));
26 }
27
28 void log (const string& msg, const Info& info) override {
29 if (!is_perl_thread()) throw std::logic_error("can't call pure-perl logging callback: log() called from perl-foreign thread");
30 Object o = xs::out(this);
31 o.call("log", xs::out(msg), xs::out(info.level));
32 }
33
34 ~PerlObjectLogger () { Backref::dtor(); }
35 };
36
37 struct NoWarningsGuard {
38 NoWarningsGuard (bool enabled) : _enabled(enabled), _cop_warnings(), _dowarn() {
39 if (!_enabled) return;
40 _cop_warnings = PL_curcop->cop_warnings;
41 _dowarn = PL_dowarn;
42 PL_curcop->cop_warnings = pWARN_STD;
43 PL_dowarn &= ~G_WARN_ON;
44 }
45
46 ~NoWarningsGuard () {
47 if (!_enabled) return;
48 PL_curcop->cop_warnings = _cop_warnings;
49 PL_dowarn = _dowarn;
50 }
51
52 private:
53 bool _enabled;
54 decltype(PL_curcop->cop_warnings) _cop_warnings;
55 U8 _dowarn;
56 };
57 }
58  
59 namespace xs {
60 template <class TYPE> struct Typemap<PerlObjectLogger*, TYPE> : Typemap<ILogger*, TYPE> {};
61 template <class TYPE> struct Typemap<PerlObjectFormatter*, TYPE> : Typemap<IFormatter*, TYPE> {};
62 }
63  
64 #undef PANDA_LOG_CODE_POINT
65 #define PANDA_LOG_CODE_POINT make_code_point()
66  
67 static inline CV* get_context_sub () {
68 int depth = 0;
69 auto ctx = caller_cx(depth, nullptr);
70 while (ctx) {
71 if (CxTYPE(ctx) == CXt_SUB) return ctx->blk_sub.cv;
72 ctx = caller_cx(++depth, nullptr);
73 }
74 return nullptr;
75 }
76  
77 static inline CodePoint make_code_point () {
78 auto cop = PL_curcop;
79
80 string_view func;
81 auto cv = get_context_sub();
82 if (cv) {
83 GV* gv = CvGV(cv);
84 if (gv) func = string_view(GvNAME(gv), GvNAMELEN(gv));
85 }
86
87 return CodePoint{CopFILE(cop), CopLINE(cop), func};
88 }
89  
90 static inline Sv format_args (SV** args, int items) {
91 if (!items) return Simple(default_message);
92
93 if (items == 1) {
94 Sv arg(args[0]);
95 if (arg.is_simple()) { return arg; }
96 else { return Simple(SvPV_nolen(args[0])); }
97 }
98
99 NoWarningsGuard g(no_format_warnings); (void)g;
100
101 STRLEN patlen;
102 auto pat = SvPV(args[0], patlen);
103 Sv ret = Simple::create(patlen * 1.5);
104 bool stub = false;
105 sv_vcatpvfn(ret, pat, patlen, nullptr, args + 1, items - 1, &stub);
106 return ret;
107 }
108  
109 static inline void peep_args (SV**& args, I32& items, const Module*& module, Sub& sub) {
110 if (items && SvROK(args[0])) {
111 auto first = SvRV(args[0]);
112 if (has_module(first)) {
113 module = xs::in<Module*>(first);
114 ++args;
115 --items;
116 }
117 else if (SvTYPE(first) == SVt_PVCV) {
118 if (items > 1) throw exception("no arguments should follow subref when logging");
119 sub = first;
120 }
121 }
122  
123 if (!module) module = resolve_module(0); // auto detect module by namespace
124 }
125  
126 template<typename Log>
127 void forward(Log&& log, SV**& args, I32& items, Sub& sub) {
128 Simple msg;
129 if (sub) {
130 Sv ret = sub.call();
131 SV* sv = ret;
132 msg = format_args(&sv, 1);
133 }
134 else {
135 msg = format_args(args, items);
136 }
137 log << msg.as_string<string_view>();
138 }
139  
140 static void xslog (Level level, SV** args, I32 items) {
141 const Module* module = nullptr;
142 Sub sub;
143 peep_args(args, items, module, sub);
144 panda_log(level, *module, [&]{ forward(log, args, items, sub); });
145 }
146  
147 MODULE = XLog PACKAGE = XLog
148 PROTOTYPES: DISABLE
149  
150 BOOT {
151 8 ▶ Stash stash(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
Stash stash(__PACKAGE__); 8 0 8
152
153 80 ▶ xs::exp::create_constants(stash, {
Branch: 4 branches
BranchTrueFalseTotal
xs::exp::create_constants(stash, { 8 0 8
xs::exp::create_constants(stash, { 8 0 8
xs::exp::create_constants(stash, { 72 8 80
xs::exp::create_constants(stash, { 0 0 0
154 {"VERBOSE_DEBUG", (int)Level::VerboseDebug},
155 {"DEBUG", (int)Level::Debug},
156 {"INFO", (int)Level::Info},
157 {"NOTICE", (int)Level::Notice},
158 {"WARNING", (int)Level::Warning},
159 {"ERROR", (int)Level::Error},
160 {"CRITICAL", (int)Level::Critical},
161 {"ALERT", (int)Level::Alert},
162 {"EMERGENCY", (int)Level::Emergency},
163 });
164
165 8 ▶ auto root_module = xs::out(&::panda_log_module);
Branch: 1 branch
BranchTrueFalseTotal
auto root_module = xs::out(&::panda_log_module); 8 0 8
166 8 root_module.readonly(1);
167 8 ▶ stash.add_const_sub("default_format", xs::out(default_format));
Branch: 2 branches
BranchTrueFalseTotal
stash.add_const_sub("default_format", xs::out(default_format)); 8 0 8
stash.add_const_sub("default_format", xs::out(default_format)); 8 0 8
168 8 ▶ stash.add_const_sub("root", root_module);
Branch: 1 branch
BranchTrueFalseTotal
stash.add_const_sub("root", root_module); 8 0 8
169 8 ▶ stash.store("root_module", root_module);
Branch: 1 branch
BranchTrueFalseTotal
stash.store("root_module", root_module); 8 0 8
170
171 8 ▶ xs::at_perl_destroy([]{
Branch: 2 branches
BranchTrueFalseTotal
xs::at_perl_destroy([]{ 8 0 8
xs::at_perl_destroy([]{ 8 0 8
172 8 ▶ if (dyn_cast<PerlObjectLogger*>(get_logger().get())) set_logger(nullptr);
Branch: 5 branches
BranchTrueFalseTotal
if (dyn_cast<PerlObjectLogger*>(get_logger().get())) set_logger(nullptr); 8 0 8
if (dyn_cast<PerlObjectLogger*>(get_logger().get())) set_logger(nullptr); 8 0 8
if (dyn_cast<PerlObjectLogger*>(get_logger().get())) set_logger(nullptr); 0 8 8
if (dyn_cast<PerlObjectLogger*>(get_logger().get())) set_logger(nullptr); 0 0 0
if (dyn_cast<PerlObjectLogger*>(get_logger().get())) set_logger(nullptr); 0 0 0
173 8 ▶ if (dyn_cast<PerlObjectFormatter*>(get_formatter().get())) set_formatter(nullptr);
Branch: 5 branches
BranchTrueFalseTotal
if (dyn_cast<PerlObjectFormatter*>(get_formatter().get())) set_formatter(nullptr); 8 0 8
if (dyn_cast<PerlObjectFormatter*>(get_formatter().get())) set_formatter(nullptr); 8 0 8
if (dyn_cast<PerlObjectFormatter*>(get_formatter().get())) set_formatter(nullptr); 0 8 8
if (dyn_cast<PerlObjectFormatter*>(get_formatter().get())) set_formatter(nullptr); 0 0 0
if (dyn_cast<PerlObjectFormatter*>(get_formatter().get())) set_formatter(nullptr); 0 0 0
174 8 });
175 8 }
176  
177 31 ▶void set_level (Level level, string_view module = {})
Branch: 1 branch
BranchTrueFalseTotal
void set_level (Level level, string_view module = {}) 31 0 31
178  
179 55 ▶void set_logger (ILoggerSP logger)
Branch: 2 branches
BranchTrueFalseTotal
void set_logger (ILoggerSP logger) 55 0 55
void set_logger (ILoggerSP logger) 55 0 55
180  
181 void set_formatter (IFormatterSP fmt) : ALIAS(set_format=1) {
182 (void)ix;
183 52 ▶ set_formatter(fmt);
Branch: 2 branches
BranchTrueFalseTotal
set_formatter(fmt); 52 0 52
set_formatter(fmt); 52 0 52
184 }
185  
186 1 ILoggerSP get_logger ()
187 1 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
1 0 1
1 0 1
188 1 IFormatterSP get_formatter ()
189 1 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
1 0 1
1 0 1
190 3 Module* get_module (string_view name)
191 3 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
3 0 3
3 0 3
192 void log (Level level, ...) {
193 84 xslog(level, &ST(1), items-1);
194 81 ▶ if (!(PL_op->op_spare & 1)) xlog::optimize();
Branch: 1 branch
BranchTrueFalseTotal
if (!(PL_op->op_spare & 1)) xlog::optimize(); 81 0 81
195 }
196  
197 Module* resolve_module (size_t depth = 0) {
198 20 RETVAL = resolve_module(depth);
199 20 ▶}
Branch: 2 branches
BranchTrueFalseTotal
} 20 0 20
} 20 0 20
200  
201 void disable_format_warnings () : ALIAS(enable_format_warnings=1) {
202 2 no_format_warnings = !ix;
203 }
204  
205 void __logXXX (...) : ALIAS(verbose_debug=0, debug=1, info=2, notice=3, warning=4, error=5, critical=6, alert=7, emergency=8) {
206 114 Level level = Level((int)Level::VerboseDebug + ix);
207 114 xslog(level, &ST(0), items);
208 112 ▶ if (!(PL_op->op_spare & 1)) xlog::optimize(level);
Branch: 1 branch
BranchTrueFalseTotal
if (!(PL_op->op_spare & 1)) xlog::optimize(level); 112 0 112
209 }
« t/lib/MyTest_xsgen.cc ↑ Summary xs/Logger.xsi »