| 176 | | |
| 177 | | } |
| 178 | | |
| 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 | | |
| 237 | | } |
| 238 | | |
| 239 | | $logdb{$fname} = $pos; |
| 240 | | |
| 241 | | } |
| 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 | | |
| 302 | | } |
| 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 | | |
| 377 | | } |
| 378 | | |
| 379 | | #Let's write the new position back to the log db |
| 380 | | |
| | 110 | } |
| | 111 | close(LOGDB); |
| | 112 | if (exists($logdb{$fname})) { |
| | 113 | ($recPos, $recLine) = split(' ', $logdb{$fname}, 2); |
| | 114 | $recLine = '' if (!defined($recLine)) ; |
| | 115 | } |
| | 116 | |
| | 117 | # Open the relevant files |
| | 118 | $rs = open(SRC_FILE, '<', $src_file); |
| | 119 | if (!defined($rs)) { |
| | 120 | push_el(\@main::el, 'gen_log_file()', "ERROR: Can't open '$src_file' for reading !"); |
| | 121 | return (-1, ''); |
| | 122 | } |
| | 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); |
| | 135 | |
| | 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 $_; |
| | 148 | } |
| | 149 | close(OLD_SRC_FILE); |
| | 150 | } else { |
| | 151 | push_el(\@main::el, 'gen_log_file',"WARNING: Can't open '$old_src_file' for reading !"); |
| | 152 | } |
| | 153 | } else { |
| | 154 | push_el(\@main::el, 'gen_log_file',"Logfile has been rotated, but '$old_src_file' doesn't exist."); |
| | 155 | } |
| | 156 | $recPos = 0; |
| | 157 | } |
| | 158 | |
| | 159 | # Read the lines from the last position to the end |
| | 160 | seek(SRC_FILE, $recPos, SEEK_SET); |
| | 161 | while(<SRC_FILE>) { |
| | 162 | print DST_FILE $_; |
| | 163 | } |
| | 164 | $recPos = tell(SRC_FILE); |
| | 165 | close(SRC_FILE); |
| | 166 | close(DST_FILE); |
| | 167 | |
| | 168 | # Let's write the new position back to the log db |
| | 169 | $logdb{$fname} = "$recPos $curLine"; |