Pārlūkot izejas kodu

Update for logs

Viewing an individual log.
Memory eats like not into itself  :(
Log 7644 lines, 4.46 MB:
[ Generated in 0.366 seconds, 3 queries executed - Memory usage: 46.84 MiB (Peak: 66.83 MiB) ]
Visman 4 gadi atpakaļ
vecāks
revīzija
1a8ab3938f

+ 64 - 0
app/Core/LogViewer.php

@@ -209,4 +209,68 @@ class LogViewer
 
 
         return $result;
         return $result;
     }
     }
+
+    public function parse(string $file): array
+    {
+        $result = [];
+        $handle = \fopen($file, 'rb');
+
+        if ($handle) {
+            $contents = '';
+            $current  = '';
+            $matches1 = [];
+
+            while (! \feof($handle)) {
+                $contents .= \fread($handle, 8192);
+                $contents = \str_replace("\r\n", "\n", $contents);
+                $contents = \str_replace("\r", "\n", $contents);
+
+                while (false !== ($pos = \strpos($contents, "\n"))) {
+                    $line     = \substr($contents, 0, $pos + 1);
+                    $contents = \substr($contents,  $pos + 1);
+
+                    if (\preg_match($this->typePattern, $line, $matches)) {
+                        if ('' !== $current) {
+                            $result[] = $this->toResult($current, $matches1);
+                        }
+
+                        $current  = $line;
+                        $matches1 = $matches;
+                    } else {
+                        $current .= $line;
+                    }
+                }
+            }
+
+            \fclose($handle);
+        }
+
+        if ('' !== $current) {
+            $result[] = $this->toResult($current, $matches1);
+        }
+
+        return $result;
+    }
+
+    protected function toResult(string $current, array $matches1): array
+    {
+        if (\preg_match($this->linePattern, $current, $matches2)) {
+            $result            = $this->clearMatches($matches2);
+            $result['context'] = \json_decode($result['context'], true, 512, \JSON_THROW_ON_ERROR);
+
+            return $result;
+        } else {
+            return $this->clearMatches($matches1) + [
+                'message' => 'LOG PARSER ERROR',
+                'context' => null,
+            ];
+        }
+    }
+
+    protected function clearMatches(array $matches): array
+    {
+        return \array_filter($matches, function($key) {
+            return \is_string($key);
+        }, \ARRAY_FILTER_USE_KEY);
+    }
 }
 }

+ 22 - 2
app/Models/Pages/Admin/Logs.php

@@ -68,7 +68,7 @@ class Logs extends Admin
         $path = $this->c->LogViewer->getPath($args['hash']);
         $path = $this->c->LogViewer->getPath($args['hash']);
 
 
         if (
         if (
-            null === $path
+            ! \is_string($path)
             || ! \is_file($path)
             || ! \is_file($path)
         ) {
         ) {
             return $this->c->Message->message('Not Found', true, 404);
             return $this->c->Message->message('Not Found', true, 404);
@@ -76,7 +76,7 @@ class Logs extends Admin
 
 
         $this->c->Lang->load('admin_logs');
         $this->c->Lang->load('admin_logs');
 
 
-        $this->aIndex   = 'logs';
+        $this->aIndex = 'logs';
 
 
         switch ($args['action']) {
         switch ($args['action']) {
             case 'view':
             case 'view':
@@ -163,4 +163,24 @@ class Logs extends Admin
 
 
         return $form;
         return $form;
     }
     }
+
+    protected function view(string $path, array $args, string $method): Page
+    {
+        $data = $this->c->LogViewer->parse($path);
+
+        foreach ($data as &$cur) {
+            $cur['context'] = \print_r($cur['context'], true);
+        }
+        unset($cur);
+
+        $this->nameTpl    = 'admin/logs';
+        $this->logData    = $data;
+        $this->logName    = $this->c->LogViewer->getName($path);
+        $this->aCrumbs[]  = [
+            $this->c->Router->link('AdminLogsAction', $args),
+            __('Log %s', $this->logName),
+        ];
+
+        return $this;
+    }
 }
 }

+ 3 - 0
app/lang/en/admin_logs.po

@@ -59,3 +59,6 @@ msgstr "Log deleted."
 
 
 msgid "Failed to delete log redirect"
 msgid "Failed to delete log redirect"
 msgstr "Failed to delete log."
 msgstr "Failed to delete log."
+
+msgid "Log %s"
+msgstr "Log %s"

+ 3 - 0
app/lang/ru/admin_logs.po

@@ -59,3 +59,6 @@ msgstr "Лог удален."
 
 
 msgid "Failed to delete log redirect"
 msgid "Failed to delete log redirect"
 msgstr "Лог не удалось удалить."
 msgstr "Лог не удалось удалить."
+
+msgid "Log %s"
+msgstr "Лог %s"

+ 29 - 0
app/templates/admin/logs.forkbb.php

@@ -41,3 +41,32 @@
         </div>
         </div>
       </section>
       </section>
 @endisset
 @endisset
+@isset ($p->logData)
+      <section id="fork-logview" class="f-admin">
+        <h2>{!! __('Log %s', $p->logName) !!}</h2>
+        <div>
+          <fieldset>
+            <ul>
+    @foreach ($p->logData as $cur)
+              <li class="f-lgli">
+        @if ($cur['context'])
+                <details class="f-lgdtl">
+                  <summary class="f-lgsu">
+        @endif
+                    <span class="f-lgdt"><span class="f-lgdts">{{ $cur['datetime'] }}</span></span>
+                    <span class="f-lglv @if ('emergency' === $cur['level_name']) f-llvem @elseif ('alert' === $cur['level_name']) f-llval @elseif ('critical' === $cur['level_name']) f-llvcr @elseif ('error' === $cur['level_name']) f-llver @elseif ('warning' === $cur['level_name']) f-llvwa @elseif ('notice' === $cur['level_name']) f-llvno @elseif ('info' === $cur['level_name']) f-llvin @elseif ('debug' === $cur['level_name']) f-llvde @endif"><span class="f-lglvs">{{ $cur['level_name'] }}</span></span>
+                    <span class="f-lgmes"><span class="f-lgmess">{{ $cur['message'] }}</span></span>
+        @if ($cur['context'])
+                  </summary>
+                  <pre class="f-lgpre">
+{{ $cur['context'] }}
+                  </pre>
+                </details>
+        @endif
+              </li>
+    @endforeach
+            </ul>
+          </fieldset>
+        </div>
+      </section>
+@endisset

+ 51 - 2
public/style/ForkBB/style.css

@@ -2374,8 +2374,8 @@ html[lang="ru"] #fork details[open] > summary::after {
   flex-direction: column;
   flex-direction: column;
 }
 }
 
 
-#forka .f-editforums-form .f-fbtn[data-name=new],
-#forka .f-editforum-form .f-fbtn[name=reset] {
+#forka .f-editforums-form .f-fbtn[data-name="new"],
+#forka .f-editforum-form .f-fbtn[name="reset"] {
   width: auto;
   width: auto;
   margin-bottom: 0.625rem;
   margin-bottom: 0.625rem;
   margin-left: auto;
   margin-left: auto;
@@ -3392,6 +3392,55 @@ html[lang="ru"] #fork details[open] > summary::after {
   width: auto;
   width: auto;
 }
 }
 
 
+#fork-logview .f-lgli {
+  font-size: 0.875rem;
+}
+
+#fork-logview .f-lgsu {
+  display: flex;
+}
+
+#fork-logview .f-lgdt {
+  width: 9.5rem;
+  flex-shrink: 0;
+}
+
+#fork-logview .f-lgdts {
+  color: #F8F4E3;
+  background-color: #AA7939;
+  margin: 0.125rem 0.1875rem;
+  display: block;
+  text-align: center;
+}
+
+#fork-logview .f-lglv {
+  width: 5rem;
+  flex-shrink: 0;
+}
+
+#fork-logview .f-lglvs {
+  margin: 0.125rem 0.1875rem;
+  display: block;
+  text-align: center;
+}
+
+#fork #fork-logview summary.f-lgsu {
+  margin: 0;
+}
+
+#fork #fork-logview summary.f-lgsu::after {
+  content: none;
+}
+
+#fork-logview .f-lgdtl {
+  width: 100%;
+}
+
+#fork-logview .f-lgpre {
+  margin-top: 0.625rem;
+  overflow: auto;
+}
+
 /****************************************/
 /****************************************/
 @media screen and (min-width: 81rem) {
 @media screen and (min-width: 81rem) {
   #fork.f-with-nav {
   #fork.f-with-nav {