1 |
package WebPAC::Common; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use Log::Log4perl qw(get_logger :levels); |
7 |
|
8 |
=head1 NAME |
9 |
|
10 |
WebPAC::Common - internal methods called from other WebPAC modules |
11 |
|
12 |
=head1 VERSION |
13 |
|
14 |
Version 0.01 |
15 |
|
16 |
=cut |
17 |
|
18 |
our $VERSION = '0.01'; |
19 |
|
20 |
=head1 INTERNAL METHODS |
21 |
|
22 |
Here is a quick list of internal methods, mostly useful to turn debugging |
23 |
on them (see L<LOGGING> below for explanation). |
24 |
|
25 |
=cut |
26 |
|
27 |
=head2 _eval |
28 |
|
29 |
Internal function to eval code without C<strict 'subs'>. |
30 |
|
31 |
=cut |
32 |
|
33 |
sub _eval { |
34 |
my $self = shift; |
35 |
|
36 |
my $code = shift || return; |
37 |
|
38 |
my $log = $self->_get_logger(); |
39 |
|
40 |
no strict 'subs'; |
41 |
my $ret = eval $code; |
42 |
if ($@) { |
43 |
$log->error("problem with eval code [$code]: $@"); |
44 |
} |
45 |
|
46 |
$log->debug("eval: ",$code," [",$ret,"]"); |
47 |
|
48 |
return $ret || undef; |
49 |
} |
50 |
|
51 |
=head2 _sort_by_order |
52 |
|
53 |
Sort xml tags data structure accoding to C<order=""> attribute. |
54 |
|
55 |
=cut |
56 |
|
57 |
sub _sort_by_order { |
58 |
my $self = shift; |
59 |
|
60 |
my $va = $self->{'import_xml'}->{'indexer'}->{$a}->{'order'} || |
61 |
$self->{'import_xml'}->{'indexer'}->{$a}; |
62 |
my $vb = $self->{'import_xml'}->{'indexer'}->{$b}->{'order'} || |
63 |
$self->{'import_xml'}->{'indexer'}->{$b}; |
64 |
|
65 |
return $va <=> $vb; |
66 |
} |
67 |
|
68 |
=head2 _x |
69 |
|
70 |
Convert string from UTF-8 to code page defined in C<import_xml>. |
71 |
|
72 |
my $text = $webpac->_x('utf8 text'); |
73 |
|
74 |
Default application code page is C<ISO-8859-2>. You will probably want to |
75 |
change that when creating new instance of object based on this one. |
76 |
|
77 |
=cut |
78 |
|
79 |
sub _x { |
80 |
my $self = shift; |
81 |
my $utf8 = shift || return; |
82 |
|
83 |
# create UTF-8 convertor for import_xml files |
84 |
$self->{'utf2cp'} ||= Text::Iconv->new('UTF-8' ,$self->{'code_page'} || 'ISO-8859-2'); |
85 |
|
86 |
return $self->{'utf2cp'}->convert($utf8) || |
87 |
$self->_get_logger()->logwarn("can't convert '$utf8'"); |
88 |
} |
89 |
|
90 |
=head2 _init_logger |
91 |
|
92 |
This function will init C<Log::Log4perl> using provided configuration file. |
93 |
|
94 |
$webpac->_init_logger('/path/to/log.conf'); |
95 |
|
96 |
=cut |
97 |
|
98 |
sub _init_logger { |
99 |
my $self = shift; |
100 |
my $file = shift; |
101 |
if ($file) { |
102 |
Log::Log4perl->init($file); |
103 |
} else { |
104 |
my $conf = q( ); |
105 |
Log::Log4perl->init( \$conf ); |
106 |
} |
107 |
} |
108 |
|
109 |
|
110 |
=head2 _get_logger |
111 |
|
112 |
Get C<Log::Log4perl> object with a twist: domains are defined for each |
113 |
method |
114 |
|
115 |
my $log = $webpac->_get_logger(); |
116 |
|
117 |
=cut |
118 |
|
119 |
sub _get_logger { |
120 |
my $self = shift; |
121 |
|
122 |
$self->{'_logger_ok'} ||= $self->_init_logger; |
123 |
|
124 |
my $name = (caller(1))[3] || caller; |
125 |
return get_logger($name); |
126 |
} |
127 |
|
128 |
|
129 |
=head1 LOGGING |
130 |
|
131 |
Logging in WebPAC is performed by L<Log::Log4perl> with config file |
132 |
C<log.conf>. |
133 |
|
134 |
Methods defined above have different levels of logging, so |
135 |
it's descriptions will be useful to turn (mostry B<debug> logging) on |
136 |
or off to see why WabPAC isn't perforing as you expect it (it might even |
137 |
be a bug!). |
138 |
|
139 |
B<This is different from normal Log4perl behaviour>. To repeat, you can |
140 |
also use method names, and not only classes (which are just few) |
141 |
to filter logging. |
142 |
|
143 |
|