String/Syslog.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« String/Rot13.pm ↑ Summary String/Uri.pm »
1 #
2 # $Id$
3 #
4 # string::syslog Brik
5 #
6 package Metabrik::String::Syslog;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 hostname => [ qw(hostname) ],
21 process => [ qw(name) ],
22 pid => [ qw(id) ],
23 do_rfc3164 => [ qw(0|1) ],
24 timestamp => [ qw(timestamp) ],
25 },
26 attributes_default => {
27 process => 'metabrik',
28 pid => $$,
29 do_rfc3164 => 0,
30 },
31 commands => {
32 encode => [ qw($data hostname|OPTIONAL process|OPTIONAL pid|OPTIONAL) ],
33 decode => [ qw($data) ],
34 date => [ qw(timestamp|OPTIONAL) ],
35 },
36 };
37 }
38  
39 sub brik_use_properties {
40 my $self = shift;
41  
42 return {
43 attributes_default => {
44 hostname => defined($self->global) && $self->global->hostname || 'hostname',
45 },
46 };
47 }
48  
49 sub encode {
50 my $self = shift;
51 my ($data, $hostname, $process, $pid) = @_;
52  
53 $hostname ||= $self->hostname;
54 $process ||= $self->process;
55 $pid ||= $self->pid;
56 $self->brik_help_run_undef_arg('encode', $data) or return;
57 $self->brik_help_set_undef_arg('hostname', $hostname) or return;
58 $self->brik_help_set_undef_arg('process', $process) or return;
59 $self->brik_help_set_undef_arg('pid', $pid) or return;
60 my $ref = $self->brik_help_run_invalid_arg('encode', $data, 'HASH', 'SCALAR')
61 or return;
62  
63 my $timestamp = $self->timestamp;
64  
65 # Convert to key=value
66 if ($ref eq 'HASH') {
67 my $kv = '';
68 for my $k (sort { $a cmp $b } keys %$data) {
69 if ($k !~ m{\s}) { # If there is no space char, we don't put between double quotes
70 $kv .= "$k=\"".$data->{$k}."\" ";
71 }
72 else {
73 $kv .= "\"$k\"=\"".$data->{$k}."\" ";
74 }
75 }
76 $kv =~ s{\s*$}{};
77 $data = $kv;
78 }
79  
80 my $message = '';
81 if ($self->do_rfc3164) {
82 my $date = $self->date($timestamp);
83 $message = "$date $hostname $process\[$pid\]: $data";
84 }
85 else {
86 $message = "$process\[$pid\]: $data";
87 }
88  
89 return $message;
90 }
91  
92 sub decode {
93 my $self = shift;
94 my ($data) = @_;
95  
96 $self->brik_help_run_undef_arg('encode', $data) or return;
97 $self->brik_help_run_invalid_arg('encode', $data, 'SCALAR') or return;
98  
99 my ($timestamp, $hostname, $process, $pid, $message);
100 # May 17 18:18:06
101 if ($data =~ m{^(\S+\s+\d+\s+\S+)\s+(\S+)\s+(\S+)\[(\d+)\]:\s+(.*)$}) {
102 $timestamp = $1;
103 $hostname = $2;
104 $process = $3;
105 $pid = $4;
106 $message = $5;
107 }
108 # Wed May 17 18:18:06 2017
109 elsif ($data =~ m{^(\S+\s+\S+\s+\d+\s+\S+\s+\S+)\s+(\S+)\s+(\S+)\[(\d+)\]:\s+(.*)$}) {
110 $timestamp = $1;
111 $hostname = $2;
112 $process = $3;
113 $pid = $4;
114 $message = $5;
115 }
116  
117 if (! defined($timestamp)) {
118 return $self->log->error("decode: unable to decode message [$data]");
119 }
120  
121 return {
122 timestamp => $timestamp,
123 hostname => $hostname,
124 process => $process,
125 pid => $pid,
126 message => $message,
127 };
128 }
129  
130 sub date {
131 my $self = shift;
132 my ($timestamp) = @_;
133  
134 my @month = qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec};
135  
136 #
137 # Courtesy of Net::Syslog
138 #
139 # But not RDC3164 compliant in regards to time format.
140 # RFC3164: "Wed May 17 18:18:06 2017"
141 # Not RFC3164: "May 17 18:18:06"
142 #
143 my @time = defined($timestamp) ? localtime($timestamp) : localtime();
144 my $date =
145 $month[$time[4]].
146 ' '.
147 (($time[3] < 10) ? (' '.$time[3]) : $time[3]).
148 ' '.
149 (($time[2] < 10 ) ? ('0'.$time[2]) : $time[2]).
150 ':'.
151 (($time[1] < 10) ? ('0'.$time[1]) : $time[1]).
152 ':'.
153 (($time[0] < 10) ? ('0'.$time[0]) : $time[0]);
154  
155 return $date;
156 }
157  
158 1;
159  
160 __END__
« String/Rot13.pm ↑ Summary String/Uri.pm »