Permalink
Browse files

The "Fatal error handler" now will write a log file in var/logs/, it'…

…s named error_fatal.log.

New driver to write logs files, driverLogTXT.
The "Fatal error handler" don't translate her strings, have conflict with driverUser.
  • Loading branch information...
1 parent fd2cac6 commit 37b73cad859d5726b792b816c9e917053e5c402f @PSF1 committed Jan 29, 2016
View
@@ -84,7 +84,8 @@ public static function fatal_error_handler($buffer) {
$error = error_get_last();
if ($error['type'] == 1) {
// type, message, file, line
- $newBuffer = '<html><header><title>'.__('Fatal Error').' </title></header>
+ $txtLog = "Fatal error\n";
+ $newBuffer = '<html><header><title>Fatal Error </title></header>
<style>
.error_content{
background: ghostwhite;
@@ -104,17 +105,22 @@ public static function fatal_error_handler($buffer) {
</style>
<body style="text-align: center;">
<div class="error_content">
- <label >'.__('Fatal Error').' </label>
+ <label >'.'Fatal Error'.' </label>
<ul>
- <li><b>'.__('Line').':</b> ' . $error['line'] . '</li>
- <li><b>'.__('Message').':</b> ' . $error['message'] . '</li>
- <li><b>'.__('File').':</b> ' . $error['file'] . '</li>
+ <li><b>'.'Line'.':</b> ' . $error['line'] . '</li>
+ <li><b>'.'Message'.':</b> ' . $error['message'] . '</li>
+ <li><b>'.'File'.':</b> ' . $error['file'] . '</li>
</ul>';
+ $txtLog .= "Line: {$error['line']}\n";
+ $txtLog .= "Message: {$error['message']}\n";
+ $txtLog .= "File: {$error['file']}\n";
if (is_array(self::$loadingHook)) {
- $newBuffer .= '<label >'.__('Ofender').' </label>';
+ $txtLog .= 'Ofender'."\n";
+ $newBuffer .= '<label >'.'Ofender'.' </label>';
$newBuffer .= '<ul>';
foreach(self::$loadingHook as $key => $val) {
$newBuffer .= "<li><b>$key:</b> $val</li>";
+ $txtLog .= "\t* $key => $val\n";
}
$newBuffer .= '</ul>';
// Remove ofender
@@ -125,11 +131,13 @@ public static function fatal_error_handler($buffer) {
driverHook::saveHandler(self::$loadingHook['hook'], self::$loadingHook['file'], self::$loadingHook['handler'], false);
// Restore normal permanent list
driverHook::$permanent = 'etc/hookHandlers.inc';
- $newBuffer .= '<p>'.__('Ofender hook was removed.').'</p>';
+ $newBuffer .= '<p>'.'Ofender hook was removed.'.'</p>';
+ $txtLog .= 'Ofender hook was removed.'."\n";
}
$newBuffer .= '</div>
</body></html>';
+ driverLogTXT::logNow('error_fatal.log', $txtLog);
return $newBuffer;
}
@@ -0,0 +1,78 @@
+<?php
+
+/*
+ * Pharinix Copyright (C) 2016 Pedro Pelaez <aaaaa976@gmail.com>
+ * Sources https://github.com/PSF1/pharinix
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+class driverLogTXT {
+ protected $handle;
+ protected $path;
+ protected $disposed = false;
+ protected $uid;
+
+ public function __construct($path) {
+ $this->setPath($path);
+ $this->uid = uniqid();
+ }
+
+ public function __destruct() {
+ if ($this->disposed) return;
+ $this->close();
+ }
+
+ /**
+ * Validate the log file path. If haven't a folder path it will write the log file into var/logs/ folder. <br/>
+ * This method is compatible with fatal error handler of driverHooks, getting absolute path from current script if not is in gived path.
+ * @param string $path It can, or not, include a folder path.
+ */
+ public function setPath($path) {
+ $fInfo = driverTools::pathInfo($path);
+ if (!$fInfo['$fInfo']) {
+ // Don't have a path defined
+ $curdir = __FILE__;
+ $curdir = str_replace('\\', '/', $curdir);
+ $curdir = str_replace('etc/drivers/txtlog.php', 'var/logs/', $curdir);
+ if (!is_dir($curdir)) {
+ mkdir($curdir, 0777, true);
+ }
+ $path = $curdir.$path;
+ }
+ $this->path = $path;
+ }
+
+ public function log($msg) {
+ if ($this->disposed) return;
+ $this->handle = fopen($this->path, "ab+");
+ if ($msg != '') {
+ fwrite($this->handle, "[".date("Y-m-d H:i:s")." - {$this->uid}] - ".$msg."\n");
+ } else {
+ fwrite($this->handle, "\n");
+ }
+ fclose($this->handle);
+ }
+
+ public function close() {
+ $this->disposed = true;
+ }
+
+ public static function logNow($path, $msg) {
+ $l = new driverLogTXT($path);
+ $l->log($msg);
+ $l->close();
+ }
+}
View
@@ -21,6 +21,7 @@
// Requires
include_once("etc/drivers/tools.php");
+include_once('etc/drivers/txtlog.php');
include_once 'etc/drivers/config.php';
driverConfig::getCFG();
@@ -70,6 +70,10 @@ public function dummyAfterManHook($params) {
public function dummyBeforeNothingHookFail($params) {
throw new Exception('Test message');
}
+
+ public function dummyAfterNothingHookFail($params) {
+ trigger_error("Dummy fatal error", E_ERROR);
+ }
public function testBeforeHook() {
// We can register a hook handler
@@ -146,4 +150,12 @@ public function testAutoPermanentHookFailMethod() {
$resp = driverCommand::run('nothing');
$this->assertNotTrue(driverHook::HasHookHandler('beforenothingHook'));
}
+
+// public function testAutoPermanentHookFatalErrorMethod() {
+// new driverHook('tests/drivers/etc/hookHandlersTest.inc');
+//
+// $this->assertTrue(driverHook::HasHookHandler('afternothingHook'));
+// $resp = driverCommand::run('nothing');
+// $this->assertNotTrue(driverHook::HasHookHandler('afternothingHook'));
+// }
}
@@ -25,6 +25,7 @@
}
// Requires
include_once("etc/drivers/tools.php");
+include_once('etc/drivers/txtlog.php');
include_once 'etc/drivers/config.php';
driverConfig::getCFG();
@@ -1,3 +1,4 @@
beforemanHook;tests/drivers/commandTest/driverHookTest.php;hookTest::dummyBeforeManHook
aftermanHook;tests/drivers/commandTest/driverHookTest.php;hookTest::dummyAfterManHook
-beforenothingHook;tests/drivers/commandTest/driverHookTest.php;hookTest::dummyBeforeNothingHookFail
+beforenothingHook;tests/drivers/commandTest/driverHookTest.php;hookTest::dummyBeforeNothingHookFail
+afternothingHook;tests/drivers/commandTest/driverHookTest.php;hookTest::dummyAfterNothingHookFail

0 comments on commit 37b73ca

Please sign in to comment.