Added build/log/core
This commit is contained in:
parent
4245df37c7
commit
0cbb21ece8
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
|
||||
namespace log\core;
|
||||
|
||||
|
||||
class FileDriver{
|
||||
|
||||
|
||||
|
||||
/* CREATES A FILE WITH ITS NEEDED DIRECTORIES
|
||||
*
|
||||
* @path<String> Path of the needed file
|
||||
*
|
||||
* @return created<Boolean> If the file has been created successfully
|
||||
*
|
||||
*/
|
||||
public static function create($file){
|
||||
/* (0) Checks arguments */
|
||||
if( !is_string($file) || is_dir($file) )
|
||||
return false;
|
||||
|
||||
/* (1) Creates file */
|
||||
try{
|
||||
return fclose( fopen($file, 'w') );
|
||||
}catch(\Exception $e){ return false; }
|
||||
}
|
||||
|
||||
|
||||
/* READS FILE'S CONTENT
|
||||
*
|
||||
* @file<String> File to read
|
||||
*
|
||||
*/
|
||||
public static function read($file){
|
||||
/* (0) Checks arguments */
|
||||
if( !is_string($file) )
|
||||
throw new \Exception('Wrong argument for read(<String>).');
|
||||
|
||||
/* (1) Initializing driver on file (read-flag) */
|
||||
$driver = new \SplFileObject($file, 'r');
|
||||
|
||||
/* (2) Read lines */
|
||||
$read = '';
|
||||
|
||||
$line = 0;
|
||||
while( $driver->current() ){
|
||||
$read .= $driver->current();
|
||||
$driver->next();
|
||||
}
|
||||
|
||||
/* (3) Returns result */
|
||||
return $read;
|
||||
}
|
||||
|
||||
/* READS A FILE'S SPECIFIC LINE
|
||||
*
|
||||
* @file<String> File to read
|
||||
* @line<int> Line to read
|
||||
*
|
||||
*/
|
||||
public static function readline($file, $line){
|
||||
/* (0) Checks arguments */
|
||||
if( !is_string($file) || intval($line) !== $line )
|
||||
throw new \Exception('Wrong argument for readline(<String>, <int>).');
|
||||
|
||||
/* (1) Initializing driver on file (read-flag) */
|
||||
$driver = new \SplFileObject($file, 'r');
|
||||
|
||||
/* (2) Goto specific line */
|
||||
$driver->seek($line);
|
||||
|
||||
/* (3) Return line's content */
|
||||
if( $driver->key() == $line )
|
||||
return $driver->current();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/* WRITES CONTENT TO A FILE
|
||||
*
|
||||
* @file<String> File to write to
|
||||
* @content<String> Content to write
|
||||
*
|
||||
* @return written<Boolean> Returns if the content have been written successfully
|
||||
*
|
||||
* @note: Creates file if it is possible with `fopen()`
|
||||
*
|
||||
*/
|
||||
public static function write($file, $content){
|
||||
/* (0) Checks arguments */
|
||||
if( !is_string($file) || !is_string($content) )
|
||||
return false;
|
||||
|
||||
/* (1) Erase file */
|
||||
try{
|
||||
fclose( fopen($file, 'w') );
|
||||
}catch(\Exception $e){ return false; }
|
||||
|
||||
/* (2) Get driver (write-flag) */
|
||||
$driver = new \SplFileObject($file, 'r+');
|
||||
|
||||
/* (3) Writes content */
|
||||
return !is_null( $driver->fwrite($content) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* APPENDS CONTENT TO A FILE
|
||||
*
|
||||
* @file<String> File to append content to
|
||||
* @content<String> Content to append
|
||||
*
|
||||
* @return append<Boolean> Returns if the content have been append successfully
|
||||
*
|
||||
* @note: If file doesn't exists, returns false
|
||||
*
|
||||
*/
|
||||
public static function append($file, $content){
|
||||
/* (0) Checks arguments */
|
||||
if( !is_file($file) || !is_string($content) )
|
||||
return false;
|
||||
|
||||
/* (1) Get driver (append-flag) */
|
||||
$driver = new \SplFileObject($file, 'a');
|
||||
|
||||
/* (2) append content */
|
||||
return !is_null( $driver->fwrite($content.PHP_EOL) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace log\core;
|
||||
|
||||
class Log{
|
||||
|
||||
private static $maxlen = 50;
|
||||
|
||||
/* [1] Attributes
|
||||
=========================================================*/
|
||||
private $label; // label of the log system
|
||||
private $file; // absolute path to logfile
|
||||
|
||||
|
||||
/* [2] Constructor
|
||||
*
|
||||
* @label<String> Label of the logger
|
||||
*
|
||||
=========================================================*/
|
||||
private function __construct($label){
|
||||
/* (1) Set logfile and label */
|
||||
$this->label = $label;
|
||||
$this->file = __BUILD__."/log/log/$label.log";
|
||||
|
||||
/* (2) Create file if doesn't exist already */
|
||||
if( !is_file($this->file) )
|
||||
FileDriver::create($this->file);
|
||||
}
|
||||
|
||||
|
||||
/* [3] Writes a new log to the file
|
||||
*
|
||||
* @content<String> Content to log
|
||||
* @tag<String> [OPT] Tag of the log (category)
|
||||
*
|
||||
* @return outputname<outputtype> outputdesc
|
||||
*
|
||||
=========================================================*/
|
||||
public function log($content="...", $tag="default"){
|
||||
/* (1) Get time data */
|
||||
$timestamp = time();
|
||||
$date = date('Y-m-s H:i:s', $timestamp);
|
||||
|
||||
/* (2) Create beginning string */
|
||||
$body = "$timestamp | $date | [$tag] ";
|
||||
$headerl = strlen($body);
|
||||
|
||||
/* (3) Split content by length */
|
||||
$body .= substr($content, 0, self::$maxlen);
|
||||
|
||||
for( $i = self::$maxlen, $l = strlen($content) ; $i < $l ; $i += self::$maxlen )
|
||||
$body .= "\n".str_repeat(" ", $headerl).substr($content, $i, self::$maxlen);
|
||||
|
||||
/* (4) Append to log file */
|
||||
FileDriver::append($this->file, $body);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [4] Multiton attributes
|
||||
=========================================================*/
|
||||
private static $instance = []; // instances
|
||||
|
||||
/* [5] Multiton Manager
|
||||
*
|
||||
* @label<String> Label of the logger you want
|
||||
*
|
||||
* @return instance<Log> instance of the wanted logger
|
||||
*
|
||||
=========================================================*/
|
||||
public static function get($label="default"){
|
||||
/* (1) Create if doesn't exist */
|
||||
if( !isset(self::$instance[$label]) )
|
||||
self::$instance[$label] = new Log($label);
|
||||
|
||||
/* (2) Return instance for all cases */
|
||||
return self::$instance[$label];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue