logHandler.pl untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 79 CC 6 SCAR 32.6
CC
6
Cov
0%
logHandler
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 26.0
1 #!/usr/bin/env perl
2  
3 #------------------------------------------------------------------------------
4 # 设定模块依赖
5 #------------------------------------------------------------------------------
6 use 5.018;
7 use warnings;
8 use IO::File;
9 use IO::Select;
10  
11 #------------------------------------------------------------------------------
12 # 接受脚本传参
13 #------------------------------------------------------------------------------
14 &logHandler();
15  
16 #------------------------------------------------------------------------------
17 # 生成特定样式的路径
18 #------------------------------------------------------------------------------
19 sub logHandler {
20 my $basePath = $ARGV[0];
21 my $lastPath;
22  
23 # 将系统日志信息转储到 logHandler,并读取到另外的 FileHandle
24 my $ioStdin = IO::File->new;
25 $ioStdin->open("<-") or die "ERROR: can't read from STDIN: $!\n";
26 my $ioSelect = IO::Select->new();
27 $ioSelect->add($ioStdin);
28  
29 # 设定文件句柄和缓存
30 my $fileHandler = IO::File->new;
31 my $cacheSize = 1024 * 1024;
32  
33 my $cache = "";
34 while (1) {
35 my @ready = $ioSelect->can_read(1);
36 if (@ready > 0) {
37  
38 # 判定是否需要刷新日志文件
39 my $baseTime;
40 my $logParams = {
41 init => sub { $baseTime = time() },
42 '%%' => sub { return ('%') },
43 '%d' => sub { return (sprintf("%02d", (localtime($baseTime))[3])) },
44 '%m' => sub { return (sprintf("%02d", (localtime($baseTime))[4] + 1)) },
45 '%Y' => sub { return (sprintf("%04d", (localtime($baseTime))[5] + 1900)) },
46 };
47 $logParams->{init}();
48 $basePath =~ s/(\%[a-zA-Z\%])/$logParams->{$1}()/ge;
49  
50 if (not defined $lastPath or $lastPath ne $basePath) {
51 $lastPath = $basePath;
52 $fileHandler->open(">> $basePath") or die "ERROR: write log $basePath failed: $!\n";
53 $fileHandler->autoflush();
54 }
55  
56 # 读取日志到 $fileHandler
57 my $buffer;
58 my $readSize = sysread($ioStdin, $buffer, 1024);
59 if ($readSize == 0) {
60 if (length $cache > 0) {
61 $fileHandler->print($cache);
62 }
63 $fileHandler->close();
64 exit();
65 }
66 elsif ($readSize > 0) {
67 $cache .= $buffer;
68 while ($cache =~ s/^([^\n]*\n)//) {
69 $fileHandler->print($1);
70 }
71 while (length $cache >= $cacheSize) {
72 $fileHandler->print(substr($cache, 0, $cacheSize));
73 $cache = substr($cache, $cacheSize);
74 }
75 }
76 else {
77 exit();
78 }
79 }
80 }
81 }
82  
83 =wildcard
84 %% a literal %
85 %a locale's abbreviated weekday name (Sun..Sat)
86 %A locale's full weekday name, variable length (Sunday..Saturday)
87 %b locale's abbreviated month name (Jan..Dec)
88 %B locale's full month name, variable length (January..December)
89 %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)
90 %C century (year divided by 100 and truncated to an integer) [00-99]
91 %d day of month (01..31)
92 %D date (mm/dd/yy)
93 %e day of month, blank padded ( 1..31)
94 %F same as %Y-%m-%d
95 %g the 2-digit year corresponding to the %V week number
96 %G the 4-digit year corresponding to the %V week number
97 %h same as %b
98 %H hour (00..23)
99 %I hour (01..12)
100 %j day of year (001..366)
101 %k hour ( 0..23)
102 %l hour ( 1..12)
103 %m month (01..12)
104 %M minute (00..59)
105 %n a newline
106 %N nanoseconds (000000000..999999999)
107 %p locale's upper case AM or PM indicator (blank in many locales)
108 %P locale's lower case am or pm indicator (blank in many locales)
109 %r time, 12-hour (hh:mm:ss [AP]M)
110 %R time, 24-hour (hh:mm)
111 %s seconds since `00:00:00 1970-01-01 UTC' (a GNU extension)
112 %S second (00..60); the 60 is necessary to accommodate a leap second
113 %t a horizontal tab
114 %T time, 24-hour (hh:mm:ss)
115 %u day of week (1..7); 1 represents Monday
116 %U week number of year with Sunday as first day of week (00..53)
117 %V week number of year with Monday as first day of week (01..53)
118 %w day of week (0..6); 0 represents Sunday
119 %W week number of year with Monday as first day of week (00..53)
120 %x locale's date representation (mm/dd/yy)
121 %X locale's time representation (%H:%M:%S)
122 %y last two digits of year (00..99)
123 %Y year (1970...)
124 %z RFC-2822 style numeric timezone (-0500) (a nonstandard extension)
125 %Z time zone (e.g., EDT), or nothing if no time zone is determinable
126 =cut
127  
128 1;