1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
   |  
 
#!/usr/bin/perl
 
use strict;
use Tk;
use File::Path;
use Math::BigFloat;
 
 
############################   GLOBAL VARIABLE   #########################
my @path_log_files = ();
my $message = "choose log file:";
my %hash_log_files = ();
########################### START FOLDER For log files  ###################
 
my @directories =  ("\\\\Rsimpcr\\log",
                    "\\\\Rsimpcr\\SiteAdmin\\log",
                    "\\\\Rsimpcr\\temp\\loader_cache");
 
 
#############################   MAIN FUNCTION   ###############################
 
  # Create a new windows 
  my $window_log = new MainWindow;
 
  # Windows configuration 
  $window_log->configure(-title => 'Log files Viewer');
  $window_log->minsize( qw(600 150));
  $window_log->geometry('+10+10');
 
  # Create widgets
  my $lst = $window_log->Listbox(-background => 'white');
  my $label_title_message = $window_log->Label(-textvariable => \$message,
                                               -font => 'Helvetica 10 bold');
  my $scroll = $lst->Scrollbar(-command => ['yview', $lst]);
  my $show = $window_log->Button(-text => 'Show log',
                                 -font => 'Helvetica 10 bold',
                                 -width => 25,
                                 -command => \&show_log);
  my $exit = $window_log->Button(-text => 'Exit',                                
                                 -font => 'Helvetica 10 bold',
                                 -width => 25,
                                 -command => [$window_log => 'destroy']);
  my $refresh = $window_log->Button(-text => 'Refresh',
                                    -font => 'Helvetica 10 bold',
                                    -width => 25,
                                    -command => \&refresh);
 
  $lst->configure(-yscrollcommand => ['set', $scroll]);
 
  # disposition of widgets
  $label_title_message->pack(-side => 'top', -anchor => 'w', -padx =>5);
  $lst->pack(-pady => 5,-padx => 5,-side => 'left', -fill => 'both', -expand => 1);
  $scroll->pack(-side => 'right', -fill => 'y');
  $refresh->pack( -pady => 5,-padx => 5);
  $show->pack( -pady => 10,-padx => 5);
  $exit->pack(  -anchor => 'n',-pady => 5,-padx => 5);
 
  MainLoop;
 
#############################   SUB FOUNCTION   #################################  
 
  sub get_files{
 
      my $path = $_[0];
 
 
      opendir(DIR, $path) || die ("can't open $path: $!\n");
      my @files = readdir(DIR);
      closedir(DIR);
 
      foreach my $file (@files) {
      	next if $file =~ /^\.\.?$/;
      	$file = $path."\\".$file;
      	if (-d $file) {
      	    get_files($file);	#recurse!
      	}
      	if (-f $file) {   
          if ($file =~ /.*\\(.*\.txt)/ || $file =~ /.*\\(.*\.log)/ ){
              push  @path_log_files,$file;
          }      
        }
      }
  } # end get_files_
 
  sub show_log {
 
      my @selections = $lst->curselection;
      $message = "You selected: \n";
      foreach (@selections) {
        $message .= @{$hash_log_files{$lst->get($_)}}[0] . "\n";
        system(@{$hash_log_files{$lst->get($_)}}[0]);
      }
      $lst->selectionClear(0, 'end'); 
 
  } # End Show log
 
  sub create_list{  
 
    my  (@log_file_name) = ();
    my  ($log_file) = ();
 
 
      # List insertion:
      tie @log_file_name, "Tk::Listbox", $lst;
      # Get all log files from  directory
      foreach my $directory (@directories){
         &get_files($directory);
      } 
      # Create the hash table of the log files
      foreach  $log_file (@path_log_files){  
         $_ = $log_file;
         my $date_file = Math::BigFloat->new((-M $log_file) * 24);
         $date_file->fround(4);
         @hash_log_files{$date_file.'  heures  '.$1}=[$log_file, $date_file]  if /.*\\(.*)/; 
         push(@log_file_name, $date_file.'  heures  '.$1);
      }       
      # Sort list by crescent number 
      @log_file_name = sort  { $a <=> $b } @log_file_name;
  }#End create list
 
  sub refreshlist{     
        @path_log_files = ();#init path table   
        $lst->delete(0,'end'); #clear old list
        &create_list();   #create new list          
  } #End of refreshlist
 
  sub cursor_arrow{
    $window_log->configure(-cursor => "arrow");
  }
  sub cursor_watch{
    $window_log->configure(-cursor => "watch");
  }
  sub refresh{
    &cursor_watch,
    &refreshlist,
    &cursor_arrow;
  } | 
Partager