| 83 | | sub diff_command { |
| 84 | | |
| 85 | | my ($cmd) = @_; |
| 86 | | |
| 87 | | push_el(\@main::el, 'diff_command()', 'Starting...'); |
| 88 | | |
| 89 | | my $result = system($cmd); |
| 90 | | |
| 91 | | my $exit_value = $? >> 8; |
| 92 | | |
| 93 | | my $signal_num = $? & 127; |
| 94 | | |
| 95 | | my $dumped_core = $? & 128; |
| 96 | | |
| 97 | | if ($exit_value == 2) { |
| 98 | | |
| 99 | | push_el(\@main::el, 'diff_command()', "ERORR: '$cmd' returned '$exit_value' !"); |
| 100 | | |
| 101 | | return $exit_value; |
| 102 | | |
| 103 | | } |
| 104 | | |
| 105 | | push_el(\@main::el, 'diff_command()', 'Ending...'); |
| 106 | | |
| 107 | | return $exit_value; |
| 108 | | |
| 109 | | } |
| 110 | | |
| | 123 | # Open the destination file |
| | 124 | $rs = open(DST_FILE, '>', $dest_file); |
| | 125 | if (!defined($rs)) { |
| | 126 | push_el(\@main::el, 'gen_log_file()', "ERROR: Can't open '$dest_file' for writing !"); |
| | 127 | close(SRC_FILE); |
| | 128 | return (-1, ''); |
| | 129 | } |
| | 130 | |
| | 131 | # Retrieve the current first line |
| | 132 | my $curLine = <SRC_FILE> || ''; |
| | 133 | $curLine =~ s/\n//; |
| | 134 | seek(SRC_FILE, 0, SEEK_SET); |
| 179 | | if (-e $dest_file_prev) { |
| 180 | | |
| 181 | | #There is almost no chance two log files to have the same size and not to be identical. However if you find your logs too precious, keep the byte by byte cmp ;) |
| 182 | | |
| 183 | | if (-s $dest_file == -s $dest_file_prev) { |
| 184 | | |
| 185 | | $rs = del_file($dest_file); |
| 186 | | |
| 187 | | push_el(\@main::el, 'gen_log_file()', "No changes in $dest_file , msg: Ending..."); |
| 188 | | |
| 189 | | return ($rs, '') if ($rs != 0); |
| 190 | | |
| 191 | | return (0, '_no_'); |
| 192 | | |
| 193 | | } |
| 194 | | |
| 195 | | $log_file = $dest_file.".diff"; |
| 196 | | |
| 197 | | $rs = open(DESTLOG,'<',$dest_file); |
| 198 | | |
| 199 | | if (!defined($rs)) { |
| 200 | | |
| 201 | | push_el(\@main::el, 'gen_log_file()', "ERROR: Can't open '$dest_file' for reading !"); |
| 202 | | |
| 203 | | return (-1, ''); |
| 204 | | |
| 205 | | } |
| 206 | | |
| 207 | | $rs = open(LOGFILE, '>',$log_file); |
| 208 | | |
| 209 | | if (!defined($rs)) { |
| 210 | | |
| 211 | | push_el(\@main::el, 'gen_log_file()', "ERROR: Can't open '$log_file' for writing !"); |
| 212 | | |
| 213 | | return (-1, ''); |
| 214 | | |
| 215 | | } |
| 216 | | |
| 217 | | #We check if we have last saved position for this domain log |
| 218 | | |
| 219 | | if (exists($logdb{$fname})) { |
| 220 | | |
| 221 | | $pos = $logdb{$fname}; |
| 222 | | |
| 223 | | #The log is not rotated so we just copy the remaining entries from our last saved position to the end |
| 224 | | |
| 225 | | if (-s $dest_file > -s $dest_file_prev) { |
| 226 | | |
| 227 | | push_el(\@main::el, 'gen_log_file'," Parsing for $dest_file begins at position $pos."); |
| 228 | | |
| 229 | | seek(DESTLOG,$pos,0); |
| 230 | | |
| 231 | | while (<DESTLOG>) { |
| 232 | | |
| 233 | | print LOGFILE $_; |
| 234 | | |
| 235 | | $pos = tell(DESTLOG) if (eof(DESTLOG)); |
| 236 | | |
| | 136 | # Rotated log detection conditions: |
| | 137 | # 1. The current logfile is smaller than the previously recorded offset position |
| | 138 | # 2. The first line of the current log is different from the previously recorded first line |
| | 139 | # If one of these conditions is fullfilled, try to read from the rotated logfile first. |
| | 140 | if (-s $src_file < $recPos || $curLine ne $recLine) { |
| | 141 | my $old_src_file = "$src_file.1"; |
| | 142 | if (-e $old_src_file) { |
| | 143 | push_el(\@main::el, 'gen_log_file', "Logfile has been rotated, parsing '$old_src_file' from position $recPos."); |
| | 144 | $rs = open(OLD_SRC_FILE, '<', $old_src_file); |
| | 145 | if (defined($rs)) { |
| | 146 | while(<OLD_SRC_FILE>) { |
| | 147 | print DST_FILE $_; |
| 242 | | |
| 243 | | #The log was rotated so we need to use the old log and copy the remaining entries and then continue from the new one. |
| 244 | | |
| 245 | | else { |
| 246 | | |
| 247 | | my $old_src_file = "$src_file.1"; |
| 248 | | |
| 249 | | my $old_dest_file = "$dest_file.1"; |
| 250 | | |
| 251 | | if (-e $old_src_file) { |
| 252 | | |
| 253 | | push_el(\@main::el, 'gen_log_file',"$dest_file was rotated. Parsing begins at position $pos in $old_src_file"); |
| 254 | | |
| 255 | | $rs = sys_command("$main::cfg{'CMD_CP'} $old_src_file $old_dest_file"); |
| 256 | | |
| 257 | | return ($rs, '') if ($rs != 0); |
| 258 | | |
| 259 | | $rs = open (OLDLOG,'<',$old_dest_file); |
| 260 | | |
| 261 | | if (!defined($rs)) { |
| 262 | | |
| 263 | | push_el(\@main::el, 'gen_log_file()', "ERROR: Can't open '$old_dest_file' for reading !"); |
| 264 | | |
| 265 | | return (-1, ''); |
| 266 | | |
| 267 | | } |
| 268 | | |
| 269 | | seek(OLDLOG,$pos,0); |
| 270 | | |
| 271 | | while (<OLDLOG>) { |
| 272 | | |
| 273 | | print LOGFILE $_; |
| 274 | | |
| 275 | | } |
| 276 | | |
| 277 | | close OLDLOG; |
| 278 | | |
| 279 | | $rs = del_file($old_dest_file); |
| 280 | | |
| 281 | | return ($rs, '') if ($rs != 0); |
| 282 | | |
| 283 | | } |
| 284 | | |
| 285 | | else { |
| 286 | | |
| 287 | | push_el(\@main::el, 'gen_log_file',"$dest_file was rotated but no $old_src_file found!"); |
| 288 | | |
| 289 | | } |
| 290 | | |
| 291 | | while (<DESTLOG>) { |
| 292 | | |
| 293 | | print LOGFILE $_; |
| 294 | | |
| 295 | | $pos = tell(DESTLOG) if (eof(DESTLOG)); |
| 296 | | |
| 297 | | } |
| 298 | | |
| 299 | | $logdb{$fname} = $pos; |
| 300 | | } |
| 301 | | |
| | 153 | } else { |
| | 154 | push_el(\@main::el, 'gen_log_file',"Logfile has been rotated, but '$old_src_file' doesn't exist."); |
| 303 | | |
| 304 | | #We have no entry for this domain log. Lets create one! |
| 305 | | else { |
| 306 | | |
| 307 | | if (-s $dest_file > -s $dest_file_prev) { |
| 308 | | |
| 309 | | |
| 310 | | $rs = open(DESTLOGPREV,'<',$dest_file_prev); |
| 311 | | |
| 312 | | if (!defined($rs)) { |
| 313 | | |
| 314 | | push_el(\@main::el, 'gen_log_file()', "ERROR: Can't open '$dest_file' for reading !"); |
| 315 | | |
| 316 | | return (-1, ''); |
| 317 | | |
| 318 | | } |
| 319 | | |
| 320 | | push_el(\@main::el, 'gen_log_file',"No log db entry for $dest_file. Checking last position in $dest_file_prev."); |
| 321 | | |
| 322 | | seek(DESTLOGPREV,0,2); |
| 323 | | |
| 324 | | $pos = tell(DESTLOGPREV); |
| 325 | | |
| 326 | | close(DESTLOGPREV); |
| 327 | | |
| 328 | | } |
| 329 | | |
| 330 | | seek(DESTLOG,$pos,0); |
| 331 | | |
| 332 | | while (<DESTLOG>) { |
| 333 | | |
| 334 | | print LOGFILE $_; |
| 335 | | |
| 336 | | $pos = tell(DESTLOG) if (eof(DESTLOG)); |
| 337 | | |
| 338 | | } |
| 339 | | |
| 340 | | $logdb{$fname} = $pos; |
| 341 | | |
| 342 | | } |
| 343 | | |
| 344 | | close DESTLOG; |
| 345 | | |
| 346 | | close LOGFILE; |
| 347 | | |
| 348 | | $rs = sys_command("$main::cfg{'CMD_MV'} $dest_file $dest_file_prev"); |
| 349 | | |
| 350 | | return ($rs, '') if ($rs != 0); |
| 351 | | |
| 352 | | } else { |
| 353 | | |
| 354 | | $rs = sys_command("$main::cfg{'CMD_CP'} $dest_file $dest_file_prev"); |
| 355 | | |
| 356 | | return ($rs, '') if ($rs != 0); |
| 357 | | |
| 358 | | $rs = open(DESTLOG,'<',$dest_file); |
| 359 | | |
| 360 | | if (!defined($rs)) { |
| 361 | | |
| 362 | | push_el(\@main::el, 'gen_log_file()', "ERROR: Can't open '$dest_file' for reading !"); |
| 363 | | |
| 364 | | return (-1, ''); |
| 365 | | |
| 366 | | } |
| 367 | | |
| 368 | | push_el(\@main::el, 'gen_log_file',"We have no .prev file. Parsing the whole '$dest_file'."); |
| 369 | | |
| 370 | | seek(DESTLOG,0,2); |
| 371 | | |
| 372 | | $pos = tell(DESTLOG); |
| 373 | | |
| 374 | | |
| 375 | | $logdb{$fname} = $pos; |
| 376 | | |
| | 156 | $recPos = 0; |