1#! @PERL@ 2# Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved. 3# 4# This program is free software; you can redistribute it and/or 5# modify it under the terms of the GNU General Public License 6# as published by the Free Software Foundation; either version 2 7# of the License, or (at your option) any later version. 8# 9# This program is distributed in the hope that it will be useful, but 10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12# for more details. 13# 14# You should have received a copy of the GNU General Public License along 15# with this program; if not, write to the Free Software Foundation, Inc., 16# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17# 18# Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300 19# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com 20 21package Amanda::Message; 22 23 24use Data::Dumper; 25 26require Amanda::Debug; 27 28use overload 29 '""' => sub { $_[0]->message(); }, 30 'cmp' => sub { $_[0]->message() cmp $_[1]; }; 31 32 33=head1 NAME 34 35Amanda::Message - Amanda object use to return a message 36 37Most API use or should be converted to use it. 38 39=head1 SYNOPSIS 40 41 # create a message 42 my $msg = Amanda::Message->new(source_filename => __FILE__, 43 source_line => __LINE__, 44 severity => $CRITICAL; 45 code => 1, 46 message => "This is a message", 47 label => $label); 48 49 print $msg->message(); 50 51=head1 Message Objects 52 53'source_filename' and 'source_line' are use for debuging to find where the 54message was generated. 55 56The 'severity' of the message, the default is G_CRITICAL, it must be one of 57these predefined constants: 58 ERROR 59 CRITICAL 60 WARNING 61 MESSAGE 62 INFO 63 DEBUG 64 65The 'code' must be unique, it identify the message (0 to 3 are used for message 66not handled by Amanda::Message): 67 0 GOOD message 68 1 ERROR with a message 69 2 ERROR without a message 70 3 Amanda::Changer::Error #You should never create it 71 1000000 Amanda::Label message 72 1100000 Amanda::Changer::Message 73 1200000 Amanda::Recovery::Message 74 1300000 Amanda::Curinfo::Message 75 1400000 Amanda::Disklist::Message 76 1500000 Amanda::Config::Message 77 1600000 Amanda::Tapelist::Message 78 1700000 Amanda::Device::Message 79 1800000 Amanda::Status::Message 80 1900000 Amanda::Report::Message 81 2000000 Amanda::Amdump::Message 82 2100000 Amanda::Cmdfile::Message 83 2200000 Amanda::Amflush::Message 84 2400000 Amanda::Index::Message 85 2500000 Amanda::Amvault::Message 86 2600000 Amanda::DB::Message 87 2700000 Amanda::CheckDump::Message 88 2800000 amcheck 89 2900000 senddiscover 90 3000000 Amanda::Amvmware::Message 91 3100000 Amanda::Service::Message 92 3200000 amanda-extensions 93 94general keys: 95 code => 96 source_filename => 97 source_line => 98 message => 'default message' #optional 99 100each code can have it's own set of keys: 101 filename => 102 errno => 103 label => 104 config => 105 barcode => 106 storage => 107 pool => 108 meta => 109 dev_error => 110 111'message' is required only for code 0 and 1. 112 113You must add all required fields to be able to rebuild the message string, 114this can include the label, config, barcode, errno, errorstr or any other 115fields. 116 117=head1 Using as subclass 118 119Each Amanda perl module should have an Amanda::Message subclass to describe 120all messages from the module. 121 122eg. class C<Amanda::Label::Message> is used by class C<Amanda::Label>. 123 124The subclass (C<Amanda::Label::Message>) must overload the local_message 125method to return a string version of the message. 126 127=cut 128 129$ERROR = 32; 130$CRITICAL = 16; 131$WARNING = 8; 132$MESSAGE = 4; 133$INFO = 2; 134$DEBUG = 1; 135 136use strict; 137use warnings; 138 139sub new { 140 my $class = shift @_; 141 my %params = @_; 142 143 if (!defined $params{'code'}) { 144 Amanda::Debug::debug(Data::Dumper::Dumper(\%params)); 145 } 146 die("no code") if !defined $params{'code'}; 147 die("no source_filename") if !defined $params{'source_filename'}; 148 die("no source_line") if !defined $params{'source_line'}; 149 150 my $self = \%params; 151 bless $self, $class; 152 153 $self->{'message'} = "" if $self->{'code'} == 1 and !defined $self->{'message'}; 154 $self->{'message'} = "" if $self->{'code'} == 2 and !defined $self->{'message'}; 155 $self->{'message'} = $self->message() if !defined $self->{'message'}; 156 $self->{'severity'} = $Amanda::Message::CRITICAL if !defined $self->{'severity'}; 157 158 Amanda::Debug::debug("$params{'source_filename'}:$params{'source_line'}:$self->{'severity'}:$self->{'code'} $self->{'message'}"); 159 160 return $self; 161} 162 163sub message { 164 my $self = shift; 165 166 return $self->{'message'} if defined $self->{'message'}; 167 168 my $message = $self->local_message(); 169 return $message if $message; 170 171 return Data::Dumper::Dumper($self); 172} 173 174# Should be overloaded 175sub local_message { 176 return; 177} 178 1791; 180