From 66c97bb570d58efce8ab840950b77f62ea0250a1 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 24 Feb 2020 11:54:58 +0100 Subject: [PATCH] Logging fixes --- debian/changelog | 2 +- vn-vmware.pl | 61 +++++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/debian/changelog b/debian/changelog index bbd9464..16bec8a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -vn-vmware (1.1.15) stable; urgency=low +vn-vmware (1.1.17) stable; urgency=low * Initial Release. diff --git a/vn-vmware.pl b/vn-vmware.pl index e81c2e1..b432f0f 100755 --- a/vn-vmware.pl +++ b/vn-vmware.pl @@ -181,39 +181,44 @@ my $backup_disks; my $time_pattern = '%Y-%m-%d_%H-%M'; my $local_backup_dir = $config{local_backup_dir}; +sub stringify_message { + my ($message) = @_; + + if ($message->isa('SoapFault')) { + my $detail = ref $message->detail; + $message = "SoapFault: $detail: $message->{fault_string}"; + } + unless (substr($message, -1) eq "\n") { + $message .= "\n"; + } + + return ($message); +} + sub log_to_file { my ($message) = @_; - my $time = Time::Piece->new; - my $time_mark = $time->strftime('%Y-%m-%d %H:%M:%S'); - print $log_fh "$time_mark $message"; + + if ($log_fh) { + my $time = Time::Piece->new; + my $time_mark = $time->strftime('%Y-%m-%d %H:%M:%S'); + print $log_fh "$time_mark $message"; + } } sub log_message { my ($message) = @_; - Util::trace(1, "$message\n"); - - if ($log_fh) { - log_to_file "LOG: $message\n"; - } + $message = stringify_message($message); + Util::trace(1, $message); + log_to_file "LOG: $message"; } sub log_error { my ($error) = @_; - if ($error->isa('SoapFault')) { - my $detail = ref $error->detail; - $error = "SoapFault: $detail: $error->{fault_string}"; - } - unless (substr($error, -1) eq "\n") { - $error .= "\n"; - } - + $error = stringify_message($error); print STDERR $error; - - if ($log_fh) { - log_to_file "ERR: $error"; - } + log_to_file "ERR: $error"; } if (exists $config{log_file}) { @@ -313,16 +318,18 @@ sub backup_job() { my $backup_job = $config{backup_jobs}{$job}; my @machines = @{$backup_job->{machines}}; + + my $default_rotation; + + if (exists $backup_job->{rotation}) { + $default_rotation = $backup_job->{rotation}; + } elsif (exists $config{rotation}) { + $default_rotation = $config{rotation}; + } foreach my $machine (@machines) { eval { - my $rotation_name; - - if (exists $backup_job->{rotation}) { - $rotation_name = $backup_job->{rotation}; - } elsif (exists $config{rotation}) { - $rotation_name = $config{rotation}; - } + my $rotation_name = $default_rotation; if (ref($machine) eq 'HASH') { $vm_name = $machine->{name};