瀏覽代碼

add logmonitor

lionared 6 年之前
父節點
當前提交
7ef0ea1003
共有 1 個文件被更改,包括 136 次插入0 次删除
  1. 136 0
      logmonitor.php

+ 136 - 0
logmonitor.php

@@ -0,0 +1,136 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: lionared
+ * Date: 2018/8/30
+ * Time: 下午2:51
+ */
+ini_set('ignore_user_abort', true);
+set_time_limit(0);
+define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
+
+require_once (BASE_ROOT_PATH . '/fooder.php');
+Base::run_util();
+$monitor = new Monitor();
+
+while (true) {
+    /*$monitor->readOne();
+    usleep(100);*/
+    $monitor->run_loop();
+}
+
+class Monitor
+{
+    private $_ptr_pos = 0;
+
+    private $_mark = "]";
+
+    private $date_now;
+
+    private $input_fname;
+
+    private $_fd_input;
+
+    private $_fd_log;
+
+    public function __construct()
+    {
+        $this->date_now = date("Y-m-d");
+        $this->_fd_log = @fopen("logmonitor.log","a+");
+        $this->setInputFd() or exit("unable open file");
+    }
+
+    public function __destruct()
+    {
+        @fclose($this->_fd_input);
+        @fclose($this->_fd_log);
+    }
+
+    public function run_loop()
+    {
+        while (true)
+        {
+            if (!is_resource($this->_fd_input)) {
+                $this->setInputFd();
+                usleep(1000);
+                continue;
+            }
+
+            if (@feof($this->_fd_input)) {
+                if (!$this->switchLogFile()) {
+                    $this->setInputFd();
+                }
+
+                usleep(1000);
+            }
+
+            $pos = @ftell($this->_fd_input);
+            $this->setPtrPos($pos);
+            $line = @fgets($this->_fd_input);
+            if (!$line) continue;
+            if ($line == "") continue;
+            if (strpos($line, $this->_mark) === 0) continue;
+
+            $this->log($line);
+            @fseek($this->_fd_input, $this->_ptr_pos);
+            $this->addMark();
+            @fgets($this->_fd_input);
+            $pos = @ftell($this->_fd_input);
+            $this->_ptr_pos = $pos;
+
+            usleep(1);
+        }
+    }
+
+    private function addMark()
+    {
+        if (@flock($this->_fd_input, LOCK_EX)) {
+            //ftruncate($this->_fd_input, 0);
+            @fwrite($this->_fd_input, $this->_mark);
+            @fflush($this->_fd_input);
+            @flock($this->_fd_input, LOCK_UN);
+        }
+    }
+
+    private function resetPtrPos()
+    {
+        $this->setPtrPos(0);
+    }
+
+    private function setPtrPos($pos)
+    {
+        $this->_ptr_pos = intval($pos);
+    }
+
+    private function switchLogFile()
+    {
+        if (date("Y-m-d") > $this->date_now) {
+            if ($this->setInputFd()) {
+                $this->date_now = date("Y-m-d");
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private function setInputFd()
+    {
+        $date = date("Ymd");
+        $file_name = BASE_DATA_PATH. DS. 'log'. DS. $date. ".log";
+        @fclose($this->_fd_input);
+        $fd = @fopen($file_name, "r+");
+        if ($fd) {
+            $this->input_fname = $file_name;
+            $this->_fd_input = $fd;
+            $this->resetPtrPos();
+            return true;
+        }
+        return false;
+    }
+
+    private function log($msg)
+    {
+        //$msg .= "\r\n";
+        @fwrite($this->_fd_log, $msg);
+    }
+}