1
# Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21
@EXPORT = ('say', 'sayFile', 'tmpdir', 'safe_exit',
22
'osWindows', 'osLinux', 'osSolaris', 'osMac',
23
'isoTimestamp', 'isoUTCTimestamp', 'isoUTCSimpleTimestamp',
24
'rqg_debug', 'unix2winPath',
25
'setLoggingToFile','setLogConf');
36
require Log::Log4perl;
37
Log::Log4perl->import();
38
$logger = Log::Log4perl->get_logger('randgen.gentest');
46
foreach my $tmp ($ENV{TMP}, $ENV{TEMP}, $ENV{TMPDIR}, '/tmp', '/var/tmp', cwd()."/tmp" ) {
56
if (defined $tmpdir) {
61
$tmpdir = $tmpdir.'\\';
63
$tmpdir = $tmpdir.'/';
67
croak("Unable to locate suitable temporary directory.") if not defined $tmpdir;
76
my $obj = bless ([], $class);
78
my $max_arg = (scalar(@_) / 2) - 1;
80
foreach my $i (0..$max_arg) {
81
if (exists $args->{$_[$i * 2]}) {
82
if (defined $obj->[$args->{$_[$i * 2]}]) {
83
carp("Argument '$_[$i * 2]' passed twice to ".$class.'->new()');
85
$obj->[$args->{$_[$i * 2]}] = $_[$i * 2 + 1];
88
carp("Unkown argument '$_[$i * 2]' to ".$class.'->new()');
98
if ($text =~ m{[\r\n]}sio) {
99
foreach my $line (split (m{[\r\n]}, $text)) {
100
if (defined $logger) {
101
$logger->info($line);
103
print "# ".isoTimestamp()." $line\n";
107
if (defined $logger) {
108
$logger->info($text);
110
print "# ".isoTimestamp()." $text\n";
118
say("--------- Contents of $file -------------");
124
say("----------------------------------");
132
my $exit_status = shift;
133
POSIX::_exit($exit_status);
138
($^O eq 'MSWin32') ||
148
if ($^O eq 'linux') {
156
if ($^O eq 'solaris') {
164
if ($^O eq 'darwin') {
172
my $datetime = shift;
174
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? localtime($datetime) : localtime();
175
return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
178
sub isoUTCTimestamp {
179
my $datetime = shift;
181
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
182
return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
185
sub isoUTCSimpleTimestamp {
186
my $datetime = shift;
188
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
189
return sprintf("%04d%02d%02dT%02d%02d%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
194
# Converts the given file path from unix style to windows native style
195
# by replacing all forward slashes to backslashes.
198
$path =~ s/\//\\/g; # replace "/" with "\"
203
if ($ENV{RQG_DEBUG}) {
211
if (defined $logger) {
212
if (not Log::Log4perl::initialized()) {
214
log4perl.rootLogger = INFO, STDOUT
215
log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen
216
log4perl.appender.STDOUT.layout=PatternLayout
217
log4perl.appender.STDOUT.layout.ConversionPattern=# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n
219
Log::Log4perl::init( \$logconf );
220
say("Using Log::Log4perl");
226
sub setLoggingToFile {
229
'log4perl.logger.randgen' => 'INFO, STDOUT, FILE',
231
'log4perl.appender.STDOUT' => 'Log::Log4perl::Appender::Screen',
232
'log4perl.appender.STDOUT.layout'=>'PatternLayout',
233
'log4perl.appender.STDOUT.layout.ConversionPattern'=>"# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n",
235
'log4perl.appender.FILE'=>'Log::Log4perl::Appender::File',
236
'log4perl.appender.FILE.filename'=>$logfile,
237
'log4perl.appender.FILE.mode'=>'append',
238
'log4perl.appender.FILE.layout'=>'PatternLayout',
239
'log4perl.appender.FILE.layout.ConversionPattern'=>"# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n"
241
Log::Log4perl::init($logconf);
242
say("Logging to stdout and $logfile");
247
Log::Log4perl::init($logfile);
248
say("Logging defined by $logfile");