| 1 | #============================================================================== | ||
| 2 | # Ham::Fldigi::Debug | ||
| 3 | # v0.002 | ||
| 4 | # (c) 2012 Andy Smith, M0VKG | ||
| 5 | #============================================================================== | ||
| 6 | # DESCRIPTION | ||
| 7 | # Colourful debug messages for Ham::Fldigi | ||
| 8 | #============================================================================== | ||
| 9 | # SYNOPSIS | ||
| 10 | # use Ham::Fldigi; | ||
| 11 | # my $f = new Ham::Fldigi('LogLevel' => 4, | ||
| 12 | # 'LogFile' => './debug.log', | ||
| 13 | # 'LogPrint' => 1, | ||
| 14 | # 'LogWrite' => 1); | ||
| 15 | # my $client = $f->client('Hostname' => 'localhost', | ||
| 16 | # 'Port' => '7362', | ||
| 17 | # 'Name' => 'default'); | ||
| 18 | # $client->modem("BPSK125"); | ||
| 19 | # $client->send("CQ CQ CQ DE M0VKG M0VKG M0VKG KN"); | ||
| 20 | #============================================================================== | ||
| 21 | |||
| 22 | # Perl documentation is provided inline in pod format. | ||
| 23 | # To view, run:- | ||
| 24 | # perldoc Ham::Fldigi::Debug | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | Ham::Fldigi::Debug - Colourful debug messages for Ham::Fldigi | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | use Ham::Fldigi; | ||
| 33 | use base qw(Ham::Fldigi::Debug) | ||
| 34 | |||
| 35 | ... | ||
| 36 | $self->debug("This is a debug message!"); | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | Colourful debug/error messages for Ham::Fldigi. | ||
| 41 | |||
| 42 | By default, none of the calls output anything - either to screen or to a logfile. This is intentional so as not to interfere when Ham::Fldigi is used by third-party scripts. | ||
| 43 | |||
| 44 | If you do want to make use of the logging functions, see the documentation for C<Ham::Fldigi>'s B<new()>. | ||
| 45 | |||
| 46 | =head2 EXPORT | ||
| 47 | |||
| 48 | B<debug>, B<error>, B<warning> and B<notice>. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | package Ham::Fldigi::Debug; | ||
| 53 | |||
| 54 | use 5.012004; | ||
| 55 | use strict; | ||
| 56 | use warnings; | ||
| 57 | |||
| 58 | use Term::ANSIColor; | ||
| 59 | use Data::Dumper; | ||
| 60 | use DateTime; | ||
| 61 | |||
| 62 | our @ISA = qw(Exporter); | ||
| 63 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 64 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 65 | our @EXPORT = qw(debug error warning notice); | ||
| 66 | our $VERSION = 0.002; | ||
| 67 | |||
| 68 | our $debug_level = 0; | ||
| 69 | our $debug_print = 0; | ||
| 70 | our $debug_file = "/dev/null"; | ||
| 71 | our $debug_write = 0; | ||
| 72 | |||
| 73 | # _timestamp | ||
| 74 | # Create timestamps for our log messages | ||
| 75 | sub _timestamp { | ||
| 76 | |||
| 77 | my ($self) = @_; | ||
| 78 | |||
| 79 | my $dt = DateTime->now; | ||
| 80 | |||
| 81 | my $timestamp = $dt->ymd." ".$dt->hms; | ||
| 82 | |||
| 83 | return $timestamp; | ||
| 84 | } | ||
| 85 | |||
| 86 | =head1 METHODS | ||
| 87 | |||
| 88 | =head2 debug(I<message>) | ||
| 89 | |||
| 90 | Print a debug message. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub debug { | ||
| 95 | |||
| 96 | my ($self, $msg) = @_; | ||
| 97 | |||
| 98 | my $class; | ||
| 99 | if(ref($self)) { | ||
| 100 | $class = ref $self; | ||
| 101 | } else { | ||
| 102 | $class = $self; | ||
| 103 | } | ||
| 104 | |||
| 105 | my @caller = split('::', (caller(1))[3]); | ||
| 106 | my $sub = $caller[$#caller]; | ||
| 107 | |||
| 108 | my $logmsg = "[".$self->_timestamp."] ".(color 'blue')."+++".(color 'reset')." ".(color 'magenta').$class.(color 'reset')." (".(color 'green').$sub.color('reset')."): ".$msg."\n"; | ||
| 109 | |||
| 110 | if($debug_level ge 4) { | ||
| 111 | if($debug_print eq 1) { | ||
| 112 | print STDERR "\r".$logmsg; | ||
| 113 | } | ||
| 114 | if($debug_write eq 1) { | ||
| 115 | open (LOGFILE, '>>'.$debug_file); | ||
| 116 | print LOGFILE $logmsg; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | =head2 error(I<message>) | ||
| 122 | |||
| 123 | Print an error message. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub error { | ||
| 128 | |||
| 129 | my ($self, $msg) = @_; | ||
| 130 | |||
| 131 | my $class; | ||
| 132 | if(ref($self)) { | ||
| 133 | $class = ref $self; | ||
| 134 | } else { | ||
| 135 | $class = $self; | ||
| 136 | } | ||
| 137 | |||
| 138 | my @caller = split('::', (caller(1))[3]); | ||
| 139 | my $sub = $caller[$#caller]; | ||
| 140 | |||
| 141 | my $logmsg = "[".$self->_timestamp."] ".(color 'red')."!!!".(color 'reset')." ".(color 'magenta').$class.(color 'reset')." (".(color 'green').$sub.color('reset')."): ".$msg."\n"; | ||
| 142 | |||
| 143 | if($debug_level ge 1) { | ||
| 144 | if($debug_print eq 1) { | ||
| 145 | print STDERR "\r".$logmsg; | ||
| 146 | } | ||
| 147 | if($debug_write eq 1) { | ||
| 148 | open (LOGFILE, '>>'.$debug_file); | ||
| 149 | print LOGFILE $logmsg; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | =head2 warning(I<message>) | ||
| 156 | |||
| 157 | Print a warning message. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub warning { | ||
| 162 | |||
| 163 | my ($self, $msg) = @_; | ||
| 164 | |||
| 165 | my $class; | ||
| 166 | if(ref($self)) { | ||
| 167 | $class = ref $self; | ||
| 168 | } else { | ||
| 169 | $class = $self; | ||
| 170 | } | ||
| 171 | |||
| 172 | my @caller = split('::', (caller(1))[3]); | ||
| 173 | my $sub = $caller[$#caller]; | ||
| 174 | |||
| 175 | my $logmsg = "[".$self->_timestamp."] ".(color 'yellow')."xxx".(color 'reset')." ".(color 'magenta').$class.(color 'reset')." (".(color 'green').$sub.color('reset')."): ".$msg."\n"; | ||
| 176 | |||
| 177 | if($debug_level ge 2) { | ||
| 178 | if($debug_print eq 1) { | ||
| 179 | print STDERR "\r".$logmsg; | ||
| 180 | } | ||
| 181 | if($debug_write eq 1) { | ||
| 182 | open (LOGFILE, '>>'.$debug_file); | ||
| 183 | print LOGFILE $logmsg; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | =head2 notice(I<message>) | ||
| 190 | |||
| 191 | Print a notice message. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub notice { | ||
| 196 | |||
| 197 | my ($self, $msg) = @_; | ||
| 198 | |||
| 199 | my $class; | ||
| 200 | if(ref($self)) { | ||
| 201 | $class = ref $self; | ||
| 202 | } else { | ||
| 203 | $class = $self; | ||
| 204 | } | ||
| 205 | |||
| 206 | my @caller = split('::', (caller(1))[3]); | ||
| 207 | my $sub = $caller[$#caller]; | ||
| 208 | |||
| 209 | my $logmsg = "[".$self->_timestamp."] ".(color 'green')."---".(color 'reset')." ".(color 'magenta').$class.(color 'reset')." (".(color 'green').$sub.color('reset')."): ".$msg."\n"; | ||
| 210 | |||
| 211 | if($debug_level ge 3) { | ||
| 212 | if($debug_print eq 1) { | ||
| 213 | print STDERR "\r".$logmsg; | ||
| 214 | } | ||
| 215 | if($debug_write eq 1) { | ||
| 216 | open (LOGFILE, '>>'.$debug_file); | ||
| 217 | print LOGFILE $logmsg; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | 1; | ||
| 223 | __END__ |