/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk

« back to all changes in this revision

Viewing changes to classes/logger.php

  • Committer: Suren A. Chilingaryan
  • Date: 2019-01-06 05:29:57 UTC
  • Revision ID: csa@suren.me-20190106052957-2hg1ey21voa1mxy4
Fix log file corruption on OpenShift platform

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
 
 
3
2
define ("LOG_PROFILE", LOG_DEBUG + 1);
4
3
 
5
4
class LOGData implements Iterator {
110
109
            $header = fread($this->file, $hdrlen);
111
110
            if ((feof($this->file))||(strlen($header) != $hdrlen)) break;
112
111
 
113
 
 
114
 
#           echo $header . "<br/>\n";
115
112
            if (sscanf($header, "%s %d %s %s %s %s %s %s %lu %s %s %d %d", $iso_time, $prio, $setup, $server, $src, $target, $session, $request, $latency, $pid, $client, $len, $msglen) != 13) break;
116
113
 
117
114
            if (($this->prio !== false)&&($prio > $this->prio)) {
215
212
 var $request;          /* Unique request id */
216
213
 var $ts;               /* Timestamp, when request processing has started */
217
214
 
 
215
 var $lock;
 
216
 
218
217
 const SIZE_RECORD_LENGTH = 5;
 
218
 const TIMEOUT = 2;     /* timeout to write log file lock (seconds) */
219
219
 
220
220
 function __construct() {
221
221
    global $LOGGER_LOG_REQUESTS;
228
228
    $this->ts = gettimeofday();
229
229
    $this->request = uniqid();
230
230
 
 
231
    $this->lock = new LOCK("logger");
 
232
 
231
233
    if ($LOGGER_LOG_REQUESTS) {
232
234
        $this->LogMessage("New request is received", false, NULL, LOG_DEBUG);
233
235
        if ($LOGGER_LOG_OUTPUT) {
529
531
        $client = "-";
530
532
    }
531
533
 
 
534
        // "ip, ip" could be returned
 
535
    $client = preg_replace("/\s+/", "", $client);
 
536
 
 
537
        // check for 2 equal IP case 
 
538
    $clients = preg_split("/,/", $client, 2);
 
539
    if ((count($clients) == 2)&&($clients[0] === $clients[1])) $client=$clients[0];
 
540
 
532
541
    if ($location)
533
542
        $msg = sprintf("%s. %s", $location, $msg);
534
543
    
568
577
    
569
578
    $umask = @umask(0);
570
579
 
571
 
    $f = @fopen($fname, "a+");
572
 
    if ($f) {
573
 
        if (flock($f, LOCK_EX)) {
574
 
            fprintf($f, "%" . LOGGER::SIZE_RECORD_LENGTH . "d %s %s %s\n", $header_size, $header, $msg, $info_str);
575
 
            flock($f, LOCK_UN);
576
 
        }
577
 
 
578
 
        fclose($f);
 
580
        // File position after "a+" may get messed if multiple instances write the log unless we ensure single access already before openning
 
581
    if ($this->lock->Lock(LOCK::BLOCK|LOCK::EXCLUSIVE)) {
 
582
        $f = @fopen($fname, "a+");
 
583
        if ($f) {
 
584
//              No reason to replicate the lock
 
585
//          if (LOCK::timed_flock($f, LOCK_EX, LOGGER::TIMEOUT)) {
 
586
                fprintf($f, "%" . LOGGER::SIZE_RECORD_LENGTH . "d %s %s %s\n", $header_size, $header, $msg, $info_str);
 
587
//              flock($f, LOCK_UN);
 
588
//          }
 
589
            fclose($f);
 
590
        }
 
591
        $this->lock->UnLock();
579
592
    }    
580
593
 
581
594
    @umask($umask);