[pve-devel] [PATCH guest-common 1/1] VZDump/Plugin: avoid cyclic dependency

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Sep 13 10:30:14 CEST 2017


pve-guest-common is above qemu-server, pve-container and thus also
pve-manager in the package hierarchy.
The latter hosts PVE::VZDump, so using it here adds a cyclic
dependency between pve-manager and pve-guest-common.

Move the log method to the base plugin class and inline the
run_command function directly do the plugins cmd method.

pve-manager's PVE::VZDump may use this plugins static log function
then instead of its own copy.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 PVE/VZDump/Plugin.pm | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/PVE/VZDump/Plugin.pm b/PVE/VZDump/Plugin.pm
index 9891352..772b619 100644
--- a/PVE/VZDump/Plugin.pm
+++ b/PVE/VZDump/Plugin.pm
@@ -2,7 +2,36 @@ package PVE::VZDump::Plugin;
 
 use strict;
 use warnings;
-use PVE::VZDump;
+
+use POSIX qw(strftime);
+
+use PVE::Tools;
+use PVE::SafeSyslog;
+
+my $log_level = {
+    err =>  'ERROR:',
+    info => 'INFO:',
+    warn => 'WARN:',
+};
+
+sub debugmsg {
+    my ($mtype, $msg, $logfd, $syslog) = @_;
+
+    chomp $msg;
+
+    return if !$msg;
+
+    my $pre = $log_level->{$mtype} || $log_level->{'err'};
+
+    my $timestr = strftime ("%F %H:%M:%S", CORE::localtime);
+
+    syslog ($mtype eq 'info' ? 'info' : 'err', "$pre $msg") if $syslog;
+
+    foreach my $line (split (/\n/, $msg)) {
+	print STDERR "$pre $line\n";
+	print $logfd "$timestr $pre $line\n" if $logfd;
+    }
+}
 
 sub set_logfd {
     my ($self, $logfd) = @_;
@@ -13,7 +42,12 @@ sub set_logfd {
 sub cmd {
     my ($self, $cmdstr, %param) = @_;
 
-    return PVE::VZDump::run_command($self->{logfd}, $cmdstr, %param);   
+    my $logfunc = sub {
+	my $line = shift;
+	debugmsg ('info', $line, $self->{logfd});
+    };
+
+    PVE::Tools::run_command($cmdstr, %param, logfunc => $logfunc);
 }
 
 sub cmd_noerr {
@@ -28,13 +62,13 @@ sub cmd_noerr {
 sub loginfo {
     my ($self, $msg) = @_;
 
-    PVE::VZDump::debugmsg('info', $msg, $self->{logfd}, 0);
+    debugmsg('info', $msg, $self->{logfd}, 0);
 }
 
 sub logerr {
     my ($self, $msg) = @_;
 
-    PVE::VZDump::debugmsg('err', $msg, $self->{logfd}, 0);
+    debugmsg('err', $msg, $self->{logfd}, 0);
 }
 
 sub type {
-- 
2.11.0





More information about the pve-devel mailing list