From 88a4a846e0afcec11bf5c7c9b888194cb4df0644 Mon Sep 17 00:00:00 2001 From: H Plato Date: Thu, 2 Mar 2017 18:16:26 -0700 Subject: [PATCH] Remove some object logger debug print statements --- lib/Generic_Item.pm | 467 +++++++++++++++++++++++++++----------------- 1 file changed, 286 insertions(+), 181 deletions(-) diff --git a/lib/Generic_Item.pm b/lib/Generic_Item.pm index 9ba7d753f..578d2a9c0 100644 --- a/lib/Generic_Item.pm +++ b/lib/Generic_Item.pm @@ -132,13 +132,19 @@ sub new { $$self{state_now} = undef; $$self{state_changed} = undef; $self->restore_data('sort_order'); - $self->{logger_enable} = $main::config_parms{object_logger_enable} if (defined $main::config_parms{object_logger_enable}); - $self->{logger_mintime} = 1; + $self->{logger_enable} = $main::config_parms{object_logger_enable} + if ( defined $main::config_parms{object_logger_enable} ); + $self->{logger_mintime} = 1; $self->{logger_updatetime} = 0; - $self->restore_data('active_state', 'schedule_count'); - for my $index (1..20) { - $self->restore_data('schedule_'.$index, 'schedule_label_'.$index, 'schedule_once_'.$index); - } + $self->restore_data( 'active_state', 'schedule_count' ); + + for my $index ( 1 .. 20 ) { + $self->restore_data( + 'schedule_' . $index, + 'schedule_label_' . $index, + 'schedule_once_' . $index + ); + } $self->_initialize_schedule; return $self; @@ -486,8 +492,9 @@ sub hidden { $self->{hidden} = $flag; } else { # Return it, but this currently only will work on $Reload. - return $self->{hidden} - ; # HP - really, no reason why this can't be a read-only method any time? + return + $self + ->{hidden}; # HP - really, no reason why this can't be a read-only method any time? } } @@ -1139,9 +1146,10 @@ sub set_state_log { $state = '' unless defined $state; $set_by_name = '' unless defined $set_by_name; $target = '' unless defined $target; -# - $self->logger($state,$set_by_name,$target) if ($self->{logger_enable}); - + # + $self->logger( $state, $set_by_name, $target ) + if ( $self->{logger_enable} ); + unshift( @{ $$self{state_log} }, "$main::Time_Date $state set_by=$set_by_name" @@ -1161,25 +1169,57 @@ sub set_state_log { TODO =cut + sub logger { - my ($self,$state,$set_by_name,$target) = @_; - my $object_name = $self->{object_name}; - $object_name =~ s/^\$//; - return if ($object_name eq ""); - return if ($state eq ""); - my $tickcount = int(&::get_tickcount()); #log in milliseconds - return if ($tickcount < ($self->{logger_updatetime} + $self->{logger_mintime})); - - #create directory structure if it doesn't exist - mkdir ($::config_parms{data_dir} . "/object_logs") unless (-d $::config_parms{data_dir} . "/object_logs"); - mkdir ($::config_parms{data_dir} . "/object_logs/" . $object_name) unless (-d $::config_parms{data_dir} . "/object_logs/" . $object_name); - mkdir ($::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . $::Year) unless (-d $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . $::Year); - mkdir ($::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . $::Year . "/" . $::Month) unless (-d $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . $::Year . "/" . $::Month); - #write the data to the log; time, ticks (milliseconds), object, state, set_by, target - &::logit ($::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . $::Year . "/" . $::Month . "/" . $::Mday . ".log", "$main::Time_Date,$tickcount,$object_name,$state,$set_by_name," . ( ($target) ? "$target" : '') ."\n",0); - $self->{logger_updatetime} = $tickcount; -} - + my ( $self, $state, $set_by_name, $target ) = @_; + my $object_name = $self->{object_name}; + $object_name =~ s/^\$//; + return if ( $object_name eq "" ); + return if ( $state eq "" ); + my $tickcount = int( &::get_tickcount() ); #log in milliseconds + return + if ( + $tickcount < ( $self->{logger_updatetime} + $self->{logger_mintime} ) ); + + #create directory structure if it doesn't exist + mkdir( $::config_parms{data_dir} . "/object_logs" ) + unless ( -d $::config_parms{data_dir} . "/object_logs" ); + mkdir( $::config_parms{data_dir} . "/object_logs/" . $object_name ) + unless ( -d $::config_parms{data_dir} . "/object_logs/" . $object_name ); + mkdir( $::config_parms{data_dir} + . "/object_logs/" + . $object_name . "/" + . $::Year ) + unless ( -d $::config_parms{data_dir} + . "/object_logs/" + . $object_name . "/" + . $::Year ); + mkdir( $::config_parms{data_dir} + . "/object_logs/" + . $object_name . "/" + . $::Year . "/" + . $::Month ) + unless ( -d $::config_parms{data_dir} + . "/object_logs/" + . $object_name . "/" + . $::Year . "/" + . $::Month ); + + #write the data to the log; time, ticks (milliseconds), object, state, set_by, target + &::logit( + $::config_parms{data_dir} + . "/object_logs/" + . $object_name . "/" + . $::Year . "/" + . $::Month . "/" + . $::Mday . ".log", + "$main::Time_Date,$tickcount,$object_name,$state,$set_by_name," + . ( ($target) ? "$target" : '' ) . "\n", + 0 + ); + $self->{logger_updatetime} = $tickcount; +} + =item C TODO @@ -1196,8 +1236,8 @@ sub reset_states2 { $ref->{set_by} = $set_by; $ref->{target} = $target; $ref->{legacy_target} = &main::set_by_to_target($set_by) - unless $ref->{target} - ; # just for old code and will be phased out along with old respond calls (done for speed in said and state_now methods) + unless $ref + ->{target}; # just for old code and will be phased out along with old respond calls (done for speed in said and state_now methods) if ( ( defined $state and !defined $ref->{state_prev} ) @@ -1288,10 +1328,13 @@ Will start logging state changes to a historical log file sub logger_enable { my ( $self, $enable ) = @_; - if ($self->isa('Group') and (defined $main::config_parms{object_logger_group})) { - $self->{logger_enable} = $main::config_parms{object_logger_group}; - } else { - $self->{logger_enable} = 1; + if ( $self->isa('Group') + and ( defined $main::config_parms{object_logger_group} ) ) + { + $self->{logger_enable} = $main::config_parms{object_logger_group}; + } + else { + $self->{logger_enable} = 1; } } @@ -1325,7 +1368,7 @@ Returns 1 if logger is enabled on the object. Otherwise 0. sub get_logger_status { my ( $self, $enable ) = @_; - return ($self->{logger_enable} ? 1 : 0); + return ( $self->{logger_enable} ? 1 : 0 ); } =item C @@ -1336,26 +1379,42 @@ Date format is epoch sub get_logger_data { my ( $self, $epoch, $days ) = @_; - $days = 0 unless (defined $days); - my $object_name = $self->{object_name}; - $object_name =~ s/^\$//; - my $data = ""; - $epoch = $epoch - ($days * 60 * 60 * 24); - for (my $i = 0; $i <= $days; $i++) { - print "db i=$i, days=$days, epoch=$epoch\n"; - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoch + ($i * 60 * 60 * 24)); - print "Epoch: $epoch is " . $mday . "/" . ($mon + 1) . "/" . ($year+1900) . "\n"; - print "Checking " . $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . "\n"; - print "Reading " . $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . "\n" if ( -e $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . ".log"); - $data .= ::file_read($::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . ".log") if ( -e $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . ".log"); -# $epoch = $epoch + (60*60*24); - } - - return $data; + $days = 0 unless ( defined $days ); + my $object_name = $self->{object_name}; + $object_name =~ s/^\$//; + my $data = ""; + $epoch = $epoch - ( $days * 60 * 60 * 24 ); + for ( my $i = 0; $i <= $days; $i++ ) { + + #print "db i=$i, days=$days, epoch=$epoch\n"; + my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = + localtime( $epoch + ( $i * 60 * 60 * 24 ) ); + + #print "Epoch: $epoch is " . $mday . "/" . ($mon + 1) . "/" . ($year+1900) . "\n"; + #print "Checking " . $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . "\n"; + #print "Reading " . $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . "\n" if ( -e $::config_parms{data_dir} . "/object_logs/" . $object_name . "/" . ($year + 1900) . "/" . ($mon + 1) . "/" . $mday . ".log"); + $data .= + ::file_read( $::config_parms{data_dir} + . "/object_logs/" + . $object_name . "/" + . ( $year + 1900 ) . "/" + . ( $mon + 1 ) . "/" + . $mday + . ".log" ) + if (-e $::config_parms{data_dir} + . "/object_logs/" + . $object_name . "/" + . ( $year + 1900 ) . "/" + . ( $mon + 1 ) . "/" + . $mday + . ".log" ); + + # $epoch = $epoch + (60*60*24); + } + + return $data; } - - =item C If the state of the generic_item changes, then code will trigger, with the lexical variables $state and $object getting set. The code is a string that will be eval'd and the variables are available to it, but not to any subroutines called by it unless you pass them. You can also set the state variable explicitly since you usually know the item. The code is a string that will be eval'd. @@ -1820,177 +1879,223 @@ sub android_xml_tag { return $xml_objects; } - -sub _initialize_schedule { - my ($self) = @_; - $self->{'initialize_timer'} = ::Timer::new(); - $self->{'initialize_timer'}->set(5, sub { - if (defined($self->{schedule_object}) && ($self->{schedule_object})) { return } - if ($self->{'schedule_count'} > 1) { - ::print_log("[SCHEDULE] Initialize schedule for ". $self->get_object_name." Schedule count is ".$self->{'schedule_count'}); - $self->{'check_date_handler'} = sub { Generic_Item::check_date($self); }; - ::MainLoop_post_add_hook( $self->{'check_date_handler'}); - } -}); +sub _initialize_schedule { + my ($self) = @_; + $self->{'initialize_timer'} = ::Timer::new(); + $self->{'initialize_timer'}->set( + 5, + sub { + if ( defined( $self->{schedule_object} ) + && ( $self->{schedule_object} ) ) + { + return; + } + if ( $self->{'schedule_count'} > 1 ) { + ::print_log( "[SCHEDULE] Initialize schedule for " + . $self->get_object_name + . " Schedule count is " + . $self->{'schedule_count'} ); + $self->{'check_date_handler'} = + sub { Generic_Item::check_date($self); }; + ::MainLoop_post_add_hook( $self->{'check_date_handler'} ); + } + } + ); } - - sub _set_schedule_active_state { - my ($self, $state) = @_; - $self->{'active_state'} = $state if defined($state); + my ( $self, $state ) = @_; + $self->{'active_state'} = $state if defined($state); } - - sub set_schedule { - my ($self,$index,$entry,$label) = @_; - unless ( defined($self->{'check_date_handler'}) ) { - $self->{'check_date_handler'} = sub { Generic_Item::check_date($self); }; - #::MainLoop_post_add_hook( $self->{'check_date_handler'}, 'persistent'); - ::MainLoop_post_add_hook( $self->{'check_date_handler'}); + my ( $self, $index, $entry, $label ) = @_; + unless ( defined( $self->{'check_date_handler'} ) ) { + $self->{'check_date_handler'} = + sub { Generic_Item::check_date($self); }; + + #::MainLoop_post_add_hook( $self->{'check_date_handler'}, 'persistent'); + ::MainLoop_post_add_hook( $self->{'check_date_handler'} ); } - if ($index > $self->{'schedule_count'}) { $self->{'schedule_count'} = $index; } - $self->{'schedule_'.$index} = $entry if (defined($entry)); - $self->{'schedule_label_'.$index} = $label if (defined($label)); - if (defined($self->{'schedule_'.$index})) { # the UI deletes all entries and adds them back which sets this flag to 2. - $self->{'schedule_once_'.$index} = 1 if ($self->{'schedule_once_'.$index} eq 2); # We only want real deleted entries set to 2, so set to 1. + if ( $index > $self->{'schedule_count'} ) { + $self->{'schedule_count'} = $index; + } + $self->{ 'schedule_' . $index } = $entry if ( defined($entry) ); + $self->{ 'schedule_label_' . $index } = $label if ( defined($label) ); + if ( defined( $self->{ 'schedule_' . $index } ) ) + { # the UI deletes all entries and adds them back which sets this flag to 2. + $self->{ 'schedule_once_' . $index } = 1 + if ( $self->{ 'schedule_once_' . $index } eq 2 ) + ; # We only want real deleted entries set to 2, so set to 1. } unless ($entry) { - undef $self->{'schedule_label_'.$index}; - undef $self->{'schedule_'.$index}; - $self->{'schedule_once_'.$index} = 2 if ($self->{'schedule_once_'.$index}); + undef $self->{ 'schedule_label_' . $index }; + undef $self->{ 'schedule_' . $index }; + $self->{ 'schedule_once_' . $index } = 2 + if ( $self->{ 'schedule_once_' . $index } ); } $self->{set_time} = $::Time; } sub set_schedule_once { - my ($self,$index,$entry,$label) = @_; - unless ($self->{'schedule_once_'.$index} eq 1) { - if ((defined($self->{'set_timer_'.$index})) && ($self->{'set_timer_'.$index}->expired)) { - $self->{'schedule_once_'.$index} = 1; - $self->set_schedule($index,$entry,$label); - } else { - $self->{'set_timer_'.$index} = ::Timer::new(); - $self->{'set_timer_'.$index}->set(10, sub { - $self->set_schedule_once($index,$entry,$label); - }); + my ( $self, $index, $entry, $label ) = @_; + unless ( $self->{ 'schedule_once_' . $index } eq 1 ) { + if ( ( defined( $self->{ 'set_timer_' . $index } ) ) + && ( $self->{ 'set_timer_' . $index }->expired ) ) + { + $self->{ 'schedule_once_' . $index } = 1; + $self->set_schedule( $index, $entry, $label ); + } + else { + $self->{ 'set_timer_' . $index } = ::Timer::new(); + $self->{ 'set_timer_' . $index }->set( + 10, + sub { + $self->set_schedule_once( $index, $entry, $label ); + } + ); } } } - sub delete_schedule { - my ($self,$index) = @_; + my ( $self, $index ) = @_; $self->set_schedule($index); } - sub reset_schedule { my ($self) = @_; my $count = $self->{'schedule_count'}; - for my $index (1..$count) { - $self->set_schedule($index); - } - $self->{'schedule_count'} = 0; - $self->{set_time} = $::Time; + for my $index ( 1 .. $count ) { + $self->set_schedule($index); + } + $self->{'schedule_count'} = 0; + $self->{set_time} = $::Time; } - sub get_schedule { - my ($self) = @_; - if ( ( defined($self->{'initialize_timer'}) ) && ( $self->{'initialize_timer'}->active ) ) { return } - my @schedule; - my $count = $self->{'schedule_count'}; - my @states = &get_states($self); - - - $schedule[0][0] = 0; #Index - $schedule[0][1] = '0 0 5 1 1'; #schedule - $schedule[0][2] = 0; #Label - $schedule[0][3] = \@states; - my $nullcount = 0; - for my $index (1..$count) { - unless(defined($self->{'schedule_'.$index})) { - if ($self->{'schedule_once_'.$index}) { - $self->{'schedule_once_'.$index} = 2; - $self->{'schedule_label_'.$index} = undef; - } else { + my ($self) = @_; + if ( ( defined( $self->{'initialize_timer'} ) ) + && ( $self->{'initialize_timer'}->active ) ) + { + return; + } + my @schedule; + my $count = $self->{'schedule_count'}; + my @states = &get_states($self); + + $schedule[0][0] = 0; #Index + $schedule[0][1] = '0 0 5 1 1'; #schedule + $schedule[0][2] = 0; #Label + $schedule[0][3] = \@states; + my $nullcount = 0; + for my $index ( 1 .. $count ) { + unless ( defined( $self->{ 'schedule_' . $index } ) ) { + if ( $self->{ 'schedule_once_' . $index } ) { + $self->{ 'schedule_once_' . $index } = 2; + $self->{ 'schedule_label_' . $index } = undef; + } + else { $nullcount++; - $self->{'schedule_label_'.$index} = undef; - $self->{'schedule_once_'.$index} = undef; + $self->{ 'schedule_label_' . $index } = undef; + $self->{ 'schedule_once_' . $index } = undef; next; - } - } - - if (defined($self->{'schedule_'.$index})) { # the UI deletes all entries and adds them back which sets this flag to 2. - $self->{'schedule_once_'.$index} = 1 if ($self->{'schedule_once_'.$index} eq 2); # We only want real deleted entries set to 2, so set to 1. - } - - if ((defined($self->{'schedule_'.$index})) || ($self->{'schedule_once_'.$index} eq 2)) { - $self->{'schedule_'.($index-$nullcount)} = $self->{'schedule_'.$index}; - $self->{'schedule_label_'.($index-$nullcount)} = $self->{'schedule_label_'.$index}; - $self->{'schedule_once_'.($index-$nullcount)} = $self->{'schedule_once_'.$index}; - $schedule[($index-$nullcount)][0] = ($index-$nullcount); - if ($self->{'schedule_once_'.$index} eq 2) { $schedule[($index-$nullcount)][1] = undef } - else { $schedule[($index-$nullcount)][1] = $self->{'schedule_'.$index} } - - if (defined($self->{'schedule_label_'.$index}) ) { $schedule[($index-$nullcount)][2] = $self->{'schedule_label_'.$index} } - else { $schedule[($index-$nullcount)][2] = ($index-$nullcount) } - - unless (($index-$nullcount) eq $index) { - $self->{'schedule_'.$index} = undef; - $self->{'schedule_label_'.$index} = undef; - $self->{'schedule_once_'.$index} = undef; - } - } - } - $self->{'schedule_count'} = scalar @schedule; - return \@schedule; -} + } + } -sub check_date { - my ($self) = @_; - if ($::New_Minute) { - - unless ($self->{'schedule_count'} > 1) { - if ( $self->{'schedule_delete_count'} eq 2 ) { - ::print_log("[SCHEDULE] Dropping schedule for ". $self->get_object_name ." check count ". $self->{'schedule_delete_count'}); - ::MainLoop_post_drop_hook( $self->{'check_date_handler'} ); - undef $self->{'check_date_handler'}; - undef $self->{'schedule_delete_count'}; + if ( defined( $self->{ 'schedule_' . $index } ) ) + { # the UI deletes all entries and adds them back which sets this flag to 2. + $self->{ 'schedule_once_' . $index } = 1 + if ( $self->{ 'schedule_once_' . $index } eq 2 ) + ; # We only want real deleted entries set to 2, so set to 1. } - $self->{'schedule_delete_count'}++; - } + if ( ( defined( $self->{ 'schedule_' . $index } ) ) + || ( $self->{ 'schedule_once_' . $index } eq 2 ) ) + { + $self->{ 'schedule_' . ( $index - $nullcount ) } = + $self->{ 'schedule_' . $index }; + $self->{ 'schedule_label_' . ( $index - $nullcount ) } = + $self->{ 'schedule_label_' . $index }; + $self->{ 'schedule_once_' . ( $index - $nullcount ) } = + $self->{ 'schedule_once_' . $index }; + $schedule[ ( $index - $nullcount ) ][0] = ( $index - $nullcount ); + if ( $self->{ 'schedule_once_' . $index } eq 2 ) { + $schedule[ ( $index - $nullcount ) ][1] = undef; + } + else { + $schedule[ ( $index - $nullcount ) ][1] = + $self->{ 'schedule_' . $index }; + } + + if ( defined( $self->{ 'schedule_label_' . $index } ) ) { + $schedule[ ( $index - $nullcount ) ][2] = + $self->{ 'schedule_label_' . $index }; + } + else { + $schedule[ ( $index - $nullcount ) ][2] = + ( $index - $nullcount ); + } - for my $index (1..$self->{'schedule_count'}) { - if (defined($self->{'schedule_'.$index})) { - ::print_log("[SCHEDULE] Checking time for ". $self->get_object_name. " schedule is " . $self->{'schedule_'.$index} ." time_cron return ". &main::time_cron($self->{'schedule_'.$index})); - if (&main::time_cron($self->{'schedule_'.$index})) { $self->set_action($self->{'schedule_label_'.$index}) } - } - } - } + unless ( ( $index - $nullcount ) eq $index ) { + $self->{ 'schedule_' . $index } = undef; + $self->{ 'schedule_label_' . $index } = undef; + $self->{ 'schedule_once_' . $index } = undef; + } + } + } + $self->{'schedule_count'} = scalar @schedule; + return \@schedule; } +sub check_date { + my ($self) = @_; + if ($::New_Minute) { + + unless ( $self->{'schedule_count'} > 1 ) { + if ( $self->{'schedule_delete_count'} eq 2 ) { + ::print_log( "[SCHEDULE] Dropping schedule for " + . $self->get_object_name + . " check count " + . $self->{'schedule_delete_count'} ); + ::MainLoop_post_drop_hook( $self->{'check_date_handler'} ); + undef $self->{'check_date_handler'}; + undef $self->{'schedule_delete_count'}; + } + $self->{'schedule_delete_count'}++; + } -sub set_action { - my ($self,$state) = @_; - return if &main::check_for_tied_filters( $self, $state ); - $self->_set_schedule_active_state($state); - my $sub = 'set'; - $sub = $self->{sub} if defined($self->{sub}); - $self->$sub($state,'schedule',1); + for my $index ( 1 .. $self->{'schedule_count'} ) { + if ( defined( $self->{ 'schedule_' . $index } ) ) { + ::print_log( "[SCHEDULE] Checking time for " + . $self->get_object_name + . " schedule is " + . $self->{ 'schedule_' . $index } + . " time_cron return " + . &main::time_cron( $self->{ 'schedule_' . $index } ) ); + if ( &main::time_cron( $self->{ 'schedule_' . $index } ) ) { + $self->set_action( $self->{ 'schedule_label_' . $index } ); + } + } + } + } } +sub set_action { + my ( $self, $state ) = @_; + return if &main::check_for_tied_filters( $self, $state ); + $self->_set_schedule_active_state($state); + my $sub = 'set'; + $sub = $self->{sub} if defined( $self->{sub} ); + $self->$sub( $state, 'schedule', 1 ); +} sub set_sub { - my ($self, $sub) = @_; - $self->{sub} = $sub; + my ( $self, $sub ) = @_; + $self->{sub} = $sub; } - =back =head2 INI PARAMETERS