1# tRNAscanSE/LogFile.pm 2# This class defines the log file used in tRNAscan-SE. 3# 4# -------------------------------------------------------------- 5# This module is part of the tRNAscan-SE program. 6# Copyright (C) 2017 Patricia Chan and Todd Lowe 7# -------------------------------------------------------------- 8# 9 10package tRNAscanSE::LogFile; 11 12use strict; 13use POSIX 'strftime'; 14 15sub new { 16 my $class = shift; 17 my $log_name = shift; 18 my $self = { 19 file_name => $log_name, 20 FILE_H => undef, 21 log_file => 0 22 }; 23 if ($log_name ne "") 24 { 25 $self->{log_file} = 1; 26 } 27 28 $self->{process_id} = $$; 29 $self->{quiet_mode} = 0; 30 31 bless ($self, $class); 32 return $self; 33} 34 35sub DESTROY 36{ 37 my $self = shift; 38} 39 40sub quiet_mode 41{ 42 my $self = shift; 43 if (@_) { $self->{quiet_mode} = shift; } 44 return $self->{quiet_mode}; 45} 46 47sub file_name 48{ 49 my $self = shift; 50 if (@_) { $self->{file_name} = shift; } 51 return $self->{file_name}; 52} 53 54sub open_file 55{ 56 my $self = shift; 57 my $file = shift; 58 59 my $success = 0; 60 61 if (($file eq "-") || ($file eq "/dev/null")) 62 { 63 $success = open($self->{FILE_H}, ">$file"); 64 $self->{file_name} = $file; 65 } 66 else 67 { 68 open($self->{FILE_H}, ">$file") || die "Unable to open $file for writing. Aborting program.\n"; 69 $|=1; 70 $self->{file_name} = $file; 71 $success = 1; 72 } 73 return $success; 74} 75 76sub close_file 77{ 78 my $self = shift; 79 80 if (defined $self->{FILE_H}) 81 { 82 close($self->{FILE_H}); 83 } 84} 85 86sub write_line 87{ 88 my $self = shift; 89 my $line = shift; 90 91 my $fh = $self->{FILE_H}; 92 93 print $fh $line . "\n"; 94} 95 96sub initialize_log 97{ 98 my $self = shift; 99 my $app = shift; 100 my $log_dir = shift; 101 my $cmd = shift; 102 103 if ($self->{log_file}) 104 { 105 my $time_str = strftime('%Y%m%d-%H:%M:%S', localtime); 106 my $file = $log_dir."/$app-$time_str-".$self->{process_id}.".log"; 107 if ($self->{file_name} ne "default") 108 { 109 $file = $self->{file_name}; 110 } 111 if (open_file($self, $file)) 112 { 113 write_line($self, "Application: $app"); 114 write_line($self, "User: ".getlogin); 115 write_line($self, "Host: ".`hostname`); 116 write_line($self, "Start Time: ".localtime); 117 write_line($self, ""); 118 if ($cmd ne "") 119 { 120 write_line($self, "Command: $cmd"); 121 write_line($self, ""); 122 } 123 } 124 } 125} 126 127sub finish_process() 128{ 129 my $self = shift; 130 131 if ($self->{log_file}) 132 { 133 write_line($self, ""); 134 write_line($self, "End Time: ".localtime); 135 close_file($self); 136 } 137} 138 139sub command 140{ 141 my $self = shift; 142 my $line = shift; 143 144 if ($self->{log_file}) 145 { 146 write_line($self, "Command: ".$line); 147 } 148} 149 150sub broadcast 151{ 152 my $self = shift; 153 my $line = shift; 154 155 if ($self->{log_file}) 156 { 157 write_line($self, $line); 158 } 159} 160 161sub status 162{ 163 my $self = shift; 164 my $line = shift; 165 if ($self->{log_file}) 166 { 167 write_line($self, "Status: ".$line); 168 } 169 if (!$self->{quiet_mode}) 170 { 171 print "Status: ".$line."\n"; 172 } 173} 174 175sub error 176{ 177 my $self = shift; 178 my $line = shift; 179 180 if ($self->{log_file}) 181 { 182 write_line($self, "Error: ".$line); 183 } 184 if (!$self->{quiet_mode}) 185 { 186 print "Error: ".$line."\n"; 187 } 188} 189 190sub warning 191{ 192 my $self = shift; 193 my $line = shift; 194 195 if ($self->{log_file}) 196 { 197 write_line($self, "Warning: ".$line); 198 } 199 if (!$self->{quiet_mode}) 200 { 201 print "Warning: ".$line."\n"; 202 } 203} 204 205sub debug 206{ 207 my $self = shift; 208 my $line = shift; 209 210 if ($self->{log_file}) 211 { 212 write_line($self, "Debug: ".$line); 213 } 214} 215 2161; 217