1- #
1+ #
22# Licensed under the Apache License, Version 2.0 (the "License");
33# you may not use this file except in compliance with the License.
44# You may obtain a copy of the License at
5- #
5+ #
66# http://www.apache.org/licenses/LICENSE-2.0
7- #
7+ #
88# Unless required by applicable law or agreed to in writing, software
99# distributed under the License is distributed on an "AS IS" BASIS,
1010# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -29,8 +29,9 @@ use Date::Manip;
2929use JSON;
3030use Toolkit_helpers qw ( logger) ;
3131
32+
3233# constructor
33- # parameters
34+ # parameters
3435# - debug - debug flag (debug on if defined)
3536
3637sub new {
@@ -43,23 +44,23 @@ sub new {
4344 _lines => \@lines ,
4445 _debug => $debug
4546 };
46-
47+
4748 bless ($self ,$classname );
4849 return $self ;
4950}
5051
5152
5253# procedure sortbycolumn
53- # parameters
54+ # parameters
5455# - column number
5556# Sort output by column
5657
5758sub sortbynumcolumn {
5859 my $self = shift ;
5960 my $columnno = shift ;
6061 my $order = shift ;
61-
62- logger($self -> {_debug }, " Entering Formater::sortbynumcolumn" ,1);
62+
63+ logger($self -> {_debug }, " Entering Formater::sortbynumcolumn" ,1);
6364 my $oldlines = $self -> {_lines };
6465 my @sortedlines = sort { Toolkit_helpers::sortcol_by_number($a , $b , $columnno , $order ) } @{$oldlines };
6566
@@ -68,7 +69,7 @@ sub sortbynumcolumn {
6869}
6970
7071# procedure sortbytextcolumn
71- # parameters
72+ # parameters
7273# - column number
7374# Sort output by column
7475
@@ -78,17 +79,17 @@ sub sortbytextcolumn {
7879 my $columnno2 = shift ;
7980 my $columnno3 = shift ;
8081
81- logger($self -> {_debug }, " Entering Formater::sortbytextcolumn" ,1);
82+ logger($self -> {_debug }, " Entering Formater::sortbytextcolumn" ,1);
8283
8384 my $oldlines = $self -> {_lines };
8485 my @sortedlines ;
8586
8687 if ( defined ($columnno1 ) && defined ($columnno2 ) && defined ($columnno3 ) ) {
8788 @sortedlines = sort { ( $a -> [$columnno1 ] . $a -> [$columnno2 ] . $a -> [$columnno3 ] ) cmp ( $b -> [$columnno1 ] . $b -> [$columnno2 ] . $b -> [$columnno3 ] ) } @{$oldlines };
88- }
89+ }
8990 elsif ( defined ($columnno1 ) && defined ($columnno2 ) ) {
9091 @sortedlines = sort { ( $a -> [$columnno1 ] . $a -> [$columnno2 ] ) cmp ( $b -> [$columnno1 ] . $b -> [$columnno2 ] ) } @{$oldlines };
91- }
92+ }
9293 else {
9394 @sortedlines = sort { $a -> [$columnno1 ] cmp $b -> [$columnno1 ] } @{$oldlines };
9495 }
@@ -99,7 +100,7 @@ sub sortbytextcolumn {
99100}
100101
101102# Procedure print
102- # parameters:
103+ # parameters:
103104# -nohead - skip header
104105# Print data into screen using formating defined in header
105106
@@ -118,6 +119,8 @@ sub print {
118119 $FD = \*STDOUT ;
119120 };
120121
122+ binmode ($FD , " :encoding(UTF-8)" );
123+
121124 print $FD " \n " ;
122125
123126 if ( ! defined ($nohead ) ) {
@@ -131,7 +134,7 @@ sub print {
131134
132135
133136# Procedure savejson
134- # parameters:
137+ # parameters:
135138# -nohead - skip header
136139# Print data into screen using formating defined in header
137140
@@ -149,8 +152,10 @@ sub savejson {
149152 $FD = \*STDOUT ;
150153 };
151154
155+ binmode ($FD , " :encoding(UTF-8)" );
156+
152157 my @results ;
153-
158+
154159 for my $line ( @{$self -> {_lines }} ) {
155160 my %json_line ;
156161 for (my $i =0; $i < scalar (@{$line }); $i ++) {
@@ -167,7 +172,7 @@ sub savejson {
167172}
168173
169174# Procedure savecsv
170- # parameters:
175+ # parameters:
171176# -nohead - skip header
172177# Print data into screen using formating defined in header
173178
@@ -185,8 +190,8 @@ sub savecsv {
185190 } else {
186191 $FD = \*STDOUT ;
187192 };
188-
189193
194+ binmode ($FD , " :encoding(UTF-8)" );
190195
191196 if ( ! defined ($nohead ) ) {
192197 print $FD " #" . join (' ,' ,@{$self -> {_header }});
@@ -210,7 +215,7 @@ sub sendtosyslog {
210215 logger($self -> {_debug }, " Entering Formater::sendtosyslog" ,1);
211216 logger($self -> {_debug }, " Format " . $self -> {_format },2);
212217 my $ret = 0;
213-
218+
214219 my $json = new JSON();
215220 # this is for sort if necessary
216221 # $json->canonical();
@@ -229,38 +234,38 @@ sub sendtosyslog {
229234 if (!defined ($timestamp )) {
230235 $timestamp = time ;
231236 }
232-
237+
233238 my $json_data = $json -> encode( \%json_line );
234239 $handler -> send ($json_data , $timestamp );
235240 }
236-
241+
237242 return $ret ;
238-
243+
239244}
240245
241246
242247# Procedure addLine
243- # parameters:
248+ # parameters:
244249# - array of columns
245250# Adding a line with columns defined in parameter to internal array
246251
247252sub addLine {
248253 my $self = shift ;
249254 my @columns = @_ ;
250- logger($self -> {_debug }, " Entering Formater::addLine" ,1);
255+ logger($self -> {_debug }, " Entering Formater::addLine" ,1);
251256
252257 push (@{$self -> {_lines }}, \@columns );
253258}
254259
255260# Procedure addHeader
256- # parameters:
261+ # parameters:
257262# - array of hashes { col_name : size }
258263# Adding a header and format defined in parameter to internal array
259264
260265sub addHeader {
261266 my $self = shift ;
262267 my @columns = @_ ;
263- logger($self -> {_debug }, " Entering Formater::addHeader" ,1);
268+ logger($self -> {_debug }, " Entering Formater::addHeader" ,1);
264269 my $format = ' ' ;
265270 my $sepline = ' ' ;
266271 my @header ;
@@ -282,15 +287,15 @@ sub addHeader {
282287}
283288
284289# Procedure getHeaderSize
285- # parameters:
290+ # parameters:
286291# Return a number of Header columns
287292
288293sub getHeaderSize {
289294 my $self = shift ;
290- logger($self -> {_debug }, " Entering Formater::getHeaderSize" ,1);
295+ logger($self -> {_debug }, " Entering Formater::getHeaderSize" ,1);
291296 return scalar (@{$self -> {_header }});
292297}
293298
294299
295300# End of module;
296- 1;
301+ 1;
0 commit comments