我正在尝试在当前目录中创建一个新目录,并将 adb 日志保存在我刚刚创建的新目录中。我正在尝试以下代码,但每次运行它时都会以某种方式覆盖日志。我想保存所有日志而不是覆盖它们。
use POSIX;
use Getopt::Long;
our $timestamp;
our $mainlog_filename;
our $ostype = $^O;
our $ProcessObj;
$SIG{'INT'} = 'INT_handler';
$SIG{'TERM'} = 'INT_handler';
$SIG{'ABRT'} = 'INT_handler';
$SIG{'QUIT'} = 'INT_handler';
if($^O eq 'MSWin32') {
$SIG{'BREAK'} = 'INT_handler';
use Win32::Process;
}
$split_val = join(':', @ARGV[0..(@ARGV-1)]);
@split_val = split(':',$split_val);
$counter=1;
foreach $sno(@split_val)
{
print "Serial Number $counter is $sno\n";
$counter++;
}
# Hash to translate number to month name
my $monthhash = {
1 => 'Jan',
2 => 'Feb',
3 => 'Mar',
4 => 'Apr',
5 => 'May',
6 => 'Jun',
7 => 'Jul',
8 => 'Aug',
9 => 'Sep',
10 => 'Oct',
11 => 'Nov',
12 => 'Dec',
};
$currdir = `pwd`;
chomp $currdir; # gets current directory
$currdir =~ s/ \/[^\/]+$//; # removes the last / and everything after it
$new_dir = "adb logs";
$perm = 755;
sub makeDir {
if (-e "$new_dir"){ problem("Directory $new_dir already exists.\n") } # Checks for existing
mkdir ($new_dir,$perm) || problem("Error making Directory $new_dir\n");
print "Content-type: text/html\n\n";
print "$new_dir Directory has been created.\n";
}
sub main
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon = $mon + 1;
$mday = sprintf("%02d", $mday) if $mday <= 9;
my $date = $monthhash->{$mon}.$mday;
$timestamp=$date."_".$hour."_".$min;
foreach $sno(@split_val)
{
chomp($sno);
$mainlog_filename = "adblogs_".$sno."_".$timestamp.".txt";
}
makeDir() unless -d $new_dir;
my $adbcommand_logcat;
my $adb_install_apk;
my $adb_install_testapk;
if($ostype eq 'MSWin32') {
system("title Android");
foreach $sno(@split_val)
{
chomp($sno);
print "$sno\n";
chdir $new_dir;
$adbcommand_logcat = "start \"Android-Logcat\" cmd /c \"adb -s $sno logcat -v time | tee ".$mainlog_filename."\"";
chdir $currdir;
$adb_install_apk = "adb install xyz.apk";
$adb_install_testapk = "adb install xyzTest.apk";
$cmd = "adb -s $sno get\-state";
my $cmdop = qx/$cmd 2>&1/;
print $cmdop;
if($cmdop =~ m/device/i) {
system($adb_install_apk);
system($adb_install_testapk);
system($adbcommand_logcat);
}
else {
print "Device is offline\n";
}
}
}
}
sub terminate
{
print "\nTerminating script ...\n";
system("adb kill-server");
#if($ostype eq 'MSWin32') {
# $ProcessObj->Kill(0);
# }
exit 0;
}
sub INT_handler
{
terminate();
exit 0;
}
main();
任何帮助表示赞赏。