1
# Copyright (c) 2010, 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
18
package DBServer::DBServer;
21
@EXPORT = ('say', 'sayFile', 'tmpdir', 'safe_exit',
22
'osWindows', 'osLinux', 'osSolaris', 'osMac',
23
'isoTimestamp', 'isoUTCTimestamp',
24
'DBSTATUS_OK','DBSTATUS_FAILURE');
35
require Log::Log4perl;
36
Log::Log4perl->import();
37
$logger = Log::Log4perl->get_logger('randgen.dbserver');
40
use constant DBSTATUS_OK => 0;
41
use constant DBSTATUS_FAILURE => 1;
48
foreach my $tmp ($ENV{TMP}, $ENV{TEMP}, $ENV{TMPDIR}, '/tmp', '/var/tmp', cwd()."/tmp" ) {
58
if (defined $tmpdir) {
63
$tmpdir = $tmpdir.'\\';
65
$tmpdir = $tmpdir.'/';
69
croak("Unable to locate suitable temporary directory.") if not defined $tmpdir;
78
my $obj = bless ([], $class);
80
my $max_arg = (scalar(@_) / 2) - 1;
82
foreach my $i (0..$max_arg) {
83
if (exists $args->{$_[$i * 2]}) {
84
if (defined $obj->[$args->{$_[$i * 2]}]) {
85
carp("Argument '$_[$i * 2]' passed twice to ".$class.'->new()');
87
$obj->[$args->{$_[$i * 2]}] = $_[$i * 2 + 1];
90
carp("Unkown argument '$_[$i * 2]' to ".$class.'->new()');
100
if ($text =~ m{[\r\n]}sio) {
101
foreach my $line (split (m{[\r\n]}, $text)) {
102
if (defined $logger) {
103
$logger->info($line);
105
print "# ".isoTimestamp()." $line\n";
109
if (defined $logger) {
110
$logger->info($text);
112
print "# ".isoTimestamp()." $text\n";
120
say("--------- Contents of $file -------------");
126
say("----------------------------------");
134
my $exit_status = shift;
135
POSIX::_exit($exit_status);
140
($^O eq 'MSWin32') ||
150
if ($^O eq 'linux') {
158
if ($^O eq 'solaris') {
166
if ($^O eq 'darwin') {
174
my $datetime = shift;
176
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? localtime($datetime) : localtime();
177
return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
181
sub isoUTCTimestamp {
182
my $datetime = shift;
184
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
185
return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
190
if (defined $logger) {
191
if (not Log::Log4perl::initialized()) {
193
log4perl.rootLogger = INFO, STDOUT
194
log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen
195
log4perl.appender.STDOUT.layout=PatternLayout
196
log4perl.appender.STDOUT.layout.ConversionPattern=# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n
198
Log::Log4perl::init( \$logconf );
199
say("Using Log::Log4perl");