[英]Sending output to logfile or STDOUT
I am trying to come up with some logic to give my script an option of where to send the output. 我正在尝试提出一些逻辑,以使我的脚本可以选择将输出发送到哪里。
This is a test script I started to write, I started to fizzle out trying to think of the combonations of the two options. 这是我开始编写的测试脚本,我开始尝试解决这两个选项的组合问题。 I know I am overthinking this too much.
我知道我对此考虑过多。
#!/usr/bin/perl
use strict;
use warnings;
# Modules to load
use Getopt::Long;
use Term::ANSIColor qw(:constants);
my $output = 0;
my $logfile = '';
GetOptions(
'o' => \$output,
'l' => \$logfile
);
if (($output == 1) && (! $logfile eq '')){
} elsif (($output == 0)($logfile eq '')){
}
If this is of any use be my guest. 如果这有什么用,请成为我的客人。
Pretty much I want 3 options 我几乎想要3个选择
0 = off 1 = stdout 2 = logfile 0 =关闭1 =标准输出2 =日志文件
Where I threw a bit of a wrench at myself if when I wanted to add a custom logfile argument. 如果我想添加自定义日志文件参数,那会给我带来一些麻烦。 I am under the impression I cannot combine the 2 arg into the same arg, can I?
我的印象是我无法将2个arg合并为同一个arg,可以吗?
The places where I will have output to write, I will control with simple if
statments based on a condition, in my first interation which just allows for output to stdout. 在那里我将有输出,写入的地方,我会用简单的控制
if
根据条件statments,在我的第一个互为作用刚刚允许输出到标准输出。 I just used the -o
option and specified 0
or 1
. 我只使用了
-o
选项并指定了0
或1
。 If it was 1 it wrote the line, if it was 0 it did not. 如果为1,则写入行;如果为0,则不行。
If any has an easier solution than the one above I am open to anything. 如果有比上述解决方案更简单的解决方案,那么我愿意接受任何解决方案。
Thanks in advance. 提前致谢。
I would use $verbose
as an option, and an optional $logfile
. 我将使用
$verbose
作为选项,以及一个可选的$logfile
。 So, if $verbose
is set, you print, if $logfile
is set, you print to the log. 因此,如果设置了
$verbose
,则进行打印;如果设置了$logfile
,则将打印至日志。
Then use: 然后使用:
if ($logfile) {
open STDOUT, '>', $logfile or die $!;
}
print "Yada\n" if $verbose;
Or for simplicity: 或为简单起见:
sub myprint {
return unless $verbose;
print @_;
}
myprint("Yada\n");
In the vein of TLP I suggest a $verbose
and a $logfile
option, I would also recommend that $verbose
be implicitly set to true if $logfile
is used. 在TLP的静脉我建议
$verbose
和$logfile
选项,我也建议$verbose
隐式地设置为true,如果$logfile
被使用。 Use $verbose
to control print
commands as usual. 照常使用
$verbose
控制print
命令。 The big magic is to use select
to control where the print
sends its output if no filehandle is given. 如果没有提供文件句柄,最大的魔术就是使用
select
来控制print
输出的输出位置。
#!/usr/bin/perl
use strict;
use warnings;
# Modules to load
use Getopt::Long;
use Term::ANSIColor qw(:constants);
my $verbose = 0;
my $logfile;
GetOptions(
'verbose' => \$verbose,
'logfile=s' => \$logfile,
);
if (defined $logfile) {
$verbose = 1;
open my $log_handle, '>', $logfile or die "Could not open $logfile";
# select makes print point to LOGFILE
select($log_handle);
}
# do stuff
print "Stuff" if $verbose;
Also since Getopt::Long
gives you long options, I have changed the options names to the human readable verbose
and logfile
, however you can use short -v
or long --verbose
to your taste. 同样,由于
Getopt::Long
为您提供了长选项,因此我将选项名称更改为可读的verbose
和logfile
,但是您可以根据需要使用short -v
或long --verbose
。
Ok, first off, forgive me if this syntax is wrong. 好的,首先,如果此语法错误,请原谅我。 Its been a LONG time since I've done any perl, so take this more as a "do this sortof thing" rather than a "copy and paste my answer".
自完成任何Perl以来,这已经是很长的时间了,因此,将其更多地看作是“做这种事情”,而不是“复制并粘贴我的答案”。 With that said, I'd probably just do this:
话虽如此,我可能只会这样做:
#!/usr/bin/perl
use strict;
use warnings;
# Modules to load
use Getopt::Long;
use Term::ANSIColor qw(:constants);
my $output = 0;
my $logfile = '';
GetOptions(
'o' => \$output,
'l' => \$logfile
);
if (($output == 1) && (! $logfile eq '')){
open STDOUT, $logfile
} elsif (($output == 0)($logfile eq '')){
open STDOUT, "/dev/null"
}
... (do stuff normally here)
All I am doing is changing where STDOUT is going by either sending it to the log file or sending it to /dev/null
我要做的就是通过将STDOUT发送到日志文件或将其发送到
/dev/null
来更改STDOUT的运行方向
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.