From 952e34d4082e459c57d70c6db981dff87b2ca87d Mon Sep 17 00:00:00 2001 From: Maarten Vanraes Date: Thu, 5 May 2016 14:59:39 +0200 Subject: Module: automatically create a logging object --- lib/ManaTools/LoggingRole.pm | 98 ------------------------------------------- lib/ManaTools/MainDisplay.pm | 42 ++++--------------- lib/ManaTools/Module.pm | 35 ++++++++++++++++ lib/ManaTools/Module/Users.pm | 41 ++++-------------- 4 files changed, 49 insertions(+), 167 deletions(-) delete mode 100644 lib/ManaTools/LoggingRole.pm diff --git a/lib/ManaTools/LoggingRole.pm b/lib/ManaTools/LoggingRole.pm deleted file mode 100644 index 273a5d65..00000000 --- a/lib/ManaTools/LoggingRole.pm +++ /dev/null @@ -1,98 +0,0 @@ -# vim: set et ts=4 sw=4: -package ManaTools::LoggingRole; -#============================================================= -*-perl-*- - -=head1 NAME - - Manatools::LoggingRole - Role to manage configuration directory - -=head1 SYNOPSIS - - package Foo; - - use Moose; - with 'Manatools::LoggingRole'; - - sub identifier { - return "logger_identifier"; - } - - ... - $self->logger->I("info message"); - ... - - 1; - -=head1 DESCRIPTION - - LoggingRole just define a role in which a ManaTools::Shared::Logging object can be used to log. - -=head1 SUPPORT - - You can find documentation for this module with the perldoc command: - - perldoc Manatools::LoggingRole - -=head1 SEE ALSO - - ManaTools::Shared::Logging - -=head1 AUTHOR - -Angelo Naselli - -=head1 COPYRIGHT and LICENSE - -Copyright (C) 2015-2016, Angelo Naselli. - -This module is free software. You can redistribute it and/or -modify it under the terms of the Artistic License 2.0. - -This program is distributed in the hope that it will be -useful, but without any warranty; without even the implied -warranty of merchantability or fitness for a particular purpose - -=cut - -use Moose::Role; -use ManaTools::Shared::Logging; - -=head2 requires - -=head3 definitions - - identifier: a string that is used as logging identifier - -=cut -#============================================================= - -requires 'identifier'; - -#============================================================= - -=head2 logger - - logger attribute defines the Logging object - see ManaTools::Shared::Logging for details and usage. - -=cut - -#============================================================= -has 'logger' => ( - is => 'ro', - isa => 'ManaTools::Shared::Logging', - init_arg => undef, - lazy => 1, - builder => '_loggerInitialize', -); - -sub _loggerInitialize{ - my $self = shift; - - return ManaTools::Shared::Logging->new(ident => $self->identifier()); -} - - -no Moose::Role; - -1; diff --git a/lib/ManaTools/MainDisplay.pm b/lib/ManaTools/MainDisplay.pm index ecdbda55..eafa3c41 100644 --- a/lib/ManaTools/MainDisplay.pm +++ b/lib/ManaTools/MainDisplay.pm @@ -71,7 +71,6 @@ use ManaTools::Shared::Locales; use File::ShareDir ':ALL'; use yui; -with 'ManaTools::LoggingRole'; has 'configDir' => ( is => 'ro', @@ -242,33 +241,6 @@ sub configName { return $self->name(); } -#============================================================= - -=head2 identifier - -=head3 INPUT - - $self: this object - -=head3 OUTPUT - - name: application name - -=head3 DESCRIPTION - - Returns the application name as logging identifier. - This method is required by LoggingRole - -=cut - -#============================================================= -sub identifier { - my $self = shift; - - return $self->name(); -} - - sub _showAboutDialog { my $self = shift; @@ -594,7 +566,7 @@ sub _localizedValue { return if !defined($hash->{$key}); if (ref($hash->{$key}) ne "HASH") { - $self->logger()->W($self->loc()->N("Bad configuration file, %s has not xml:lang attribute, guessing it is a string", $key)); + $self->W("Bad configuration file, %s has not xml:lang attribute, guessing it is a string", $key); # Force array is set for "title" return $hash->{$key}[0]; } @@ -618,11 +590,11 @@ sub _loadSettings { if (! -e $fileName) { my $err = $self->loc()->N("Configuration file %s is missing", $fileName); - $self->logger()->E($err); + $self->E($err); die $err; } - $self->logger()->I($self->loc()->N("Reading configuration file %s", $fileName)); + $self->I("Reading configuration file %s", $fileName); if (! scalar %{$self->settings()} || $force_load) { my $settingsReader = ManaTools::SettingsReader->new({fileName => $fileName}); @@ -639,7 +611,7 @@ sub _loadSettings { $read, $key ); - $self->logger()->I($self->loc()->N("Load settings: %s content is <<%s>>", $key, $settings->{$key})); + $self->I("Load settings: %s content is <<%s>>", $key, $settings->{$key}); } elsif (($key eq "icon" || $key eq "logo") && (substr( $read->{$key}, 0, 1) ne '/')) { # icon with relative path? @@ -795,7 +767,7 @@ sub _loadCategories { my $currCategory; foreach $fileName (@categoryFiles) { - $self->logger()->I($self->loc()->N("Parsing category file %s", $fileName)); + $self->I("Parsing category file %s", $fileName); my $inFile = new ManaTools::ConfigReader({fileName => $fileName}); my $tmpCat; my $tmp; @@ -806,7 +778,7 @@ sub _loadCategories { $tmp, 'title' ); - $self->logger()->D($self->loc()->N("Load categories: title content is <<%s>>", $title)); + $self->D("Load categories: title content is <<%s>>", $title); my $icon = $tmp->{icon}; if ((substr( $icon, 0, 1) ne '/')) { # icon with relative path? @@ -841,7 +813,7 @@ sub _loadCategories { $icon = File::ShareDir::dist_file(ManaTools::Shared::distName(), $tmp->{icon}); } - $self->logger()->D($self->loc()->N("Load categories: module title is <<%s>>", $title)); + $self->D("Load categories: module title is <<%s>>", $title); if (not $currCategory->moduleLoaded($title)) { $tmpMod = ManaTools::Module->create( name => $title, diff --git a/lib/ManaTools/Module.pm b/lib/ManaTools/Module.pm index b800d0b1..5bc8c064 100644 --- a/lib/ManaTools/Module.pm +++ b/lib/ManaTools/Module.pm @@ -155,6 +155,41 @@ has 'loc' => ( ); +#============================================================= + +=head2 logger + + logger attribute defines logging object that uses the loc attribute + and goes to Syslog. (see ManaTools::Shared::Logging for details). + You can use this attribute to log various messages: + + $log->D("debugstuff: %s", $somestring); + $log->I("infostuff: %s", $somestring); + $log->W("warnstuff: %s", $somestring); + $log->E("errorstuff: %s", $somestring); + + if you wish to trace (goes to STDERR): + + $log->trace(1); + +=cut + +#============================================================= +has 'logger' => ( + is => 'rw', + isa => 'ManaTools::Shared::Logging', + lazy => 1, + init_arg => undef, + required => 0, + default => sub { + my $self = shift; + # make sure to trigger loc & name first + return ManaTools::Shared::Logging->new(loc => $self->loc(), ident => $self->name()); + }, + handles => ['D','I','W','E'], +); + + #============================================================= =head2 commandline diff --git a/lib/ManaTools/Module/Users.pm b/lib/ManaTools/Module/Users.pm index 863f0266..6ae9e2c2 100644 --- a/lib/ManaTools/Module/Users.pm +++ b/lib/ManaTools/Module/Users.pm @@ -79,7 +79,6 @@ use MDK::Common::DataStructure qw(member); use feature 'state'; extends qw( ManaTools::Module ); -with 'ManaTools::LoggingRole'; has 'configDir' => ( is => 'ro', isa => 'Str', @@ -205,32 +204,6 @@ has 'groups_users_tab' => ( #============================================================= -=head2 identifier - -=head3 INPUT - - $self: this object - -=head3 OUTPUT - - name: module name - -=head3 DESCRIPTION - - Returns the module name as logging identifier. - This method is required by LoggingRole - -=cut - -#============================================================= -sub identifier { - my $self = shift; - - return $self->name(); -} - -#============================================================= - =head2 configName =head3 INPUT @@ -483,7 +456,7 @@ sub _deleteGroupDialog { } else { if ($self->sh_users->deleteGroup($groupname)) { - $self->logger->I($self->loc->N("Removing group: %s", $groupname)); + $self->I("Removing group: %s", $groupname); } $self->_refresh(); } @@ -570,7 +543,7 @@ sub _deleteUserDialog { last; } elsif ($widget == $deleteButton) { - $self->logger->I($self->loc->N("Removing user: %s", $username)); + $self->I("Removing user: %s", $username); my $option = undef; $option->{clean_home} = $checkhome->isChecked() if $checkhome->isChecked(); $option->{clean_spool} = $checkspool->isChecked() if $checkspool->isChecked(); @@ -687,7 +660,7 @@ sub _addGroupDialog { $self->sh_gui->msgBox({text => $errorString}) if ($errorString); } else { - $self->logger->I($self->loc->N("Adding group: %s ", $groupname)); + $self->I("Adding group: %s ", $groupname); my $groupParams = { groupname => $groupname, is_system => $is_system, @@ -1020,8 +993,7 @@ sub addUserDialog { $gid = $self->sh_users->groupID($username); } elsif ($groupchoice == 1) { # Put it in 'users' group - $self->logger->I($self->loc->N("Putting %s to 'users' group", - $username)); + $self->I("Putting %s to 'users' group", $username); $gid = $self->sh_users->Add2UsersGroup($username); } else { @@ -1034,7 +1006,7 @@ sub addUserDialog { groupname => $username, is_system => $is_system, }); - $self->logger->I($self->loc->N("Creating new group: %s", $username)); + $self->I("Creating new group: %s", $username); } } } else { @@ -1047,8 +1019,9 @@ sub addUserDialog { } else { ## OK let's create the user + # TODO: will this output to STDOUT disrupt ncurses? print $self->loc->N("Adding user: ") . $username . " \n"; - $self->logger->I($self->loc->N("Adding user: %s", $username)); + $self->I("Adding user: %s", $username); my $loginshell = $userData->{ login_shell }->value(); my $fullname = $userData->{ full_name }->value(); utf8::decode($fullname); -- cgit v1.2.1