GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
MainLogger Class Reference

Public Member Functions

 setSendMsg ($b)
 
 getMessages ()
 
 __construct ($logFile, $logDebug=false)
 
 emergency ($message, $name="EMERGENCY")
 
 alert ($message, $name="ALERT")
 
 critical ($message, $name="CRITICAL")
 
 error ($message, $name="ERROR")
 
 warning ($message, $name="WARNING")
 
 notice ($message, $name="NOTICE")
 
 info ($message, $name="INFO")
 
 debug ($message, $name="DEBUG")
 
 setLogDebug ($logDebug)
 
 logException (\Throwable $e, $trace=null)
 
 log ($level, $message)
 
 shutdown ()
 
 run ()
 
 setWrite ($write)
 
 setConsoleCallback ($callback)
 
- Public Member Functions inherited from AttachableThreadedLogger
 addAttachment (\ThreadedLoggerAttachment $attachment)
 
 removeAttachment (\ThreadedLoggerAttachment $attachment)
 
 removeAttachments ()
 
 getAttachments ()
 
- Public Member Functions inherited from Thread
 getCreatorId ()
 
 getThreadId ()
 
 isJoined ()
 
 isStarted ()
 
 join ()
 
 start (int $options=PTHREADS_INHERIT_ALL)
 
- Public Member Functions inherited from Threaded
 chunk ($size, bool $preserve=false)
 
 count ()
 
 isRunning ()
 
 isTerminated ()
 
 merge ($from, $overwrite=true)
 
 notify ()
 
 notifyOne ()
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 pop ()
 
 run ()
 
 shift ()
 
 synchronized (\Closure $function, $args=null)
 
 wait ($timeout)
 
 getRefCount ()
 
 addRef ()
 
 delRef ()
 
 isGarbage ()
 
- Public Member Functions inherited from Logger
 emergency ($message)
 
 alert ($message)
 
 critical ($message)
 
 error ($message)
 
 warning ($message)
 
 notice ($message)
 
 info ($message)
 
 debug ($message)
 

Static Public Member Functions

static getLogger ()
 
- Static Public Member Functions inherited from Thread
static getCurrentThread ()
 
static getCurrentThreadId ()
 
- Static Public Member Functions inherited from Threaded
static extend ($obj)
 

Data Fields

 $shouldSendMsg = ""
 
 $shouldRecordMsg = false
 

Static Public Attributes

static $logger = null
 

Protected Member Functions

 send ($message, $level, $prefix, $color)
 

Protected Attributes

 $logFile
 
 $logStream
 
 $shutdown
 
 $logDebug
 
 $write = false
 
- Protected Attributes inherited from AttachableThreadedLogger
 $attachment = null
 

Constructor & Destructor Documentation

◆ __construct()

__construct (   $logFile,
  $logDebug = false 
)
Parameters
string$logFile
bool$logDebug
Exceptions
70  {
71  if(static::$logger instanceof MainLogger){
72  throw new \RuntimeException("MainLogger has been already created");
73  }
74  static::$logger = $this;
75  touch($logFile);
76  $this->logFile = $logFile;
77  $this->logDebug = (bool) $logDebug;
78  $this->logStream = new \Threaded;
79  $this->start();
80  }

Member Function Documentation

◆ alert()

alert (   $message,
  $name = "ALERT" 
)
Parameters
string$message
string$name
101  {
102  $this->send($message, \LogLevel::ALERT, $name, TextFormat::RED);
103  }

◆ critical()

critical (   $message,
  $name = "CRITICAL" 
)
Parameters
string$message
string$name
109  {
110  $this->send($message, \LogLevel::CRITICAL, $name, TextFormat::RED);
111  }

◆ debug()

debug (   $message,
  $name = "DEBUG" 
)
Parameters
string$message
string$name
149  {
150  if($this->logDebug === false){
151  return;
152  }
153  $this->send($message, \LogLevel::DEBUG, $name, TextFormat::GRAY);
154  }

◆ emergency()

emergency (   $message,
  $name = "EMERGENCY" 
)
Parameters
string$message
string$name
93  {
94  $this->send($message, \LogLevel::EMERGENCY, $name, TextFormat::RED);
95  }

◆ error()

error (   $message,
  $name = "ERROR" 
)
Parameters
string$message
string$name
117  {
118  $this->send($message, \LogLevel::ERROR, $name, TextFormat::DARK_RED);
119  }

◆ getLogger()

static getLogger ( )
static
Returns
MainLogger
85  {
86  return static::$logger;
87  }

◆ getMessages()

getMessages ( )
Returns
string
57  {
58  $msg = $this->shouldSendMsg;
59  $this->shouldSendMsg = "";
60  $this->lastGet = time();
61  return $msg;
62  }

◆ info()

info (   $message,
  $name = "INFO" 
)
Parameters
string$message
string$name
141  {
142  $this->send($message, \LogLevel::INFO, $name, TextFormat::WHITE);
143  }

◆ log()

log (   $level,
  $message 
)
Parameters
mixed$level
string$message

Implements Logger.

214  {
215  switch($level){
216  case LogLevel::EMERGENCY:
217  $this->emergency($message);
218  break;
219  case LogLevel::ALERT:
220  $this->alert($message);
221  break;
222  case LogLevel::CRITICAL:
223  $this->critical($message);
224  break;
225  case LogLevel::ERROR:
226  $this->error($message);
227  break;
228  case LogLevel::WARNING:
229  $this->warning($message);
230  break;
231  case LogLevel::NOTICE:
232  $this->notice($message);
233  break;
234  case LogLevel::INFO:
235  $this->info($message);
236  break;
237  case LogLevel::DEBUG:
238  $this->debug($message);
239  break;
240  }
241  }

◆ logException()

logException ( \Throwable  $e,
  $trace = null 
)
Parameters
\Throwable$e
null$trace

Implements Logger.

167  {
168  if($trace === null){
169  $trace = $e->getTrace();
170  }
171  $errstr = $e->getMessage();
172  $errfile = $e->getFile();
173  $errno = $e->getCode();
174  $errline = $e->getLine();
175 
176  $errorConversion = [
177  0 => "EXCEPTION",
178  E_ERROR => "E_ERROR",
179  E_WARNING => "E_WARNING",
180  E_PARSE => "E_PARSE",
181  E_NOTICE => "E_NOTICE",
182  E_CORE_ERROR => "E_CORE_ERROR",
183  E_CORE_WARNING => "E_CORE_WARNING",
184  E_COMPILE_ERROR => "E_COMPILE_ERROR",
185  E_COMPILE_WARNING => "E_COMPILE_WARNING",
186  E_USER_ERROR => "E_USER_ERROR",
187  E_USER_WARNING => "E_USER_WARNING",
188  E_USER_NOTICE => "E_USER_NOTICE",
189  E_STRICT => "E_STRICT",
190  E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
191  E_DEPRECATED => "E_DEPRECATED",
192  E_USER_DEPRECATED => "E_USER_DEPRECATED",
193  ];
194  if($errno === 0){
195  $type = LogLevel::CRITICAL;
196  }else{
197  $type = ($errno === E_ERROR or $errno === E_USER_ERROR) ? LogLevel::ERROR : (($errno === E_USER_WARNING or $errno === E_WARNING) ? LogLevel::WARNING : LogLevel::NOTICE);
198  }
199  $errno = isset($errorConversion[$errno]) ? $errorConversion[$errno] : $errno;
200  if(($pos = strpos($errstr, "\n")) !== false){
201  $errstr = substr($errstr, 0, $pos);
202  }
203  $errfile = \pocketmine\cleanPath($errfile);
204  $this->log($type, get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline");
205  foreach(@\pocketmine\getTrace(1, $trace) as $i => $line){
206  $this->debug($line);
207  }
208  }

◆ notice()

notice (   $message,
  $name = "NOTICE" 
)
Parameters
string$message
string$name
133  {
134  $this->send(TextFormat::BOLD . $message, \LogLevel::NOTICE, $name, TextFormat::AQUA);
135  }

◆ run()

run ( )
334  {
335  $this->shutdown = false;
336  while($this->shutdown === false){
337  $this->synchronized(function(){
338  while($this->logStream->count() > 0){
339  $chunk = $this->logStream->shift();
340  if($this->write){
341  $this->logResource = file_put_contents($this->logFile, $chunk, FILE_APPEND);
342  }
343  }
344 
345  $this->wait(200000);
346  });
347  }
348 
349  if($this->logStream->count() > 0){
350  while($this->logStream->count() > 0){
351  $chunk = $this->logStream->shift();
352  if($this->write){
353  $this->logResource = file_put_contents($this->logFile, $chunk, FILE_APPEND);
354  }
355  }
356  }
357  }

◆ send()

send (   $message,
  $level,
  $prefix,
  $color 
)
protected
Parameters
$message
$level
$prefix
$color
253  {
254  $now = time();
255 
256  $thread = \Thread::getCurrentThread();
257  if($thread === null){
258  $threadName = "Server thread";
259  }elseif($thread instanceof Thread or $thread instanceof Worker){
260  $threadName = $thread->getThreadName() . " thread";
261  }else{
262  $threadName = (new \ReflectionClass($thread))->getShortName() . " thread";
263  }
264 
265  if($this->shouldRecordMsg){
266  if((time() - $this->lastGet) >= 10) $this->shouldRecordMsg = false; // 10 secs timeout
267  else{
268  if(strlen($this->shouldSendMsg) >= 10000) $this->shouldSendMsg = "";
269  $this->shouldSendMsg .= $color . "|" . $prefix . "|" . trim($message, "\r\n") . "\n";
270  }
271  }
272 
273  $message = TextFormat::toANSI(TextFormat::AQUA . "[" . date("H:i:s", $now) . "] " . TextFormat::RESET . $color . "[" . $threadName . "/" . $prefix . "]:" . " " . $message . TextFormat::RESET);
274  //$message = TextFormat::toANSI(TextFormat::AQUA . "[GenisysPro]->[" . date("H:i:s", $now) . "] " . TextFormat::RESET . $color . "[$prefix]:" . " " . $message . TextFormat::RESET);
275  //$message = TextFormat::toANSI(TextFormat::AQUA . "[" . date("H:i:s") . "] ". TextFormat::RESET . $color ."<".$prefix . ">" . " " . $message . TextFormat::RESET);
276  $cleanMessage = TextFormat::clean($message);
277 
279  echo $cleanMessage . PHP_EOL;
280  }else{
281  echo $message . PHP_EOL;
282  }
283 
284  if(isset($this->consoleCallback)){
285  call_user_func($this->consoleCallback);
286  }
287 
288  if($this->attachment instanceof \ThreadedLoggerAttachment){
289  $this->attachment->call($level, $message);
290  }
291 
292  $this->logStream[] = date("Y-m-d", $now) . " " . $cleanMessage . "\n";
293  if($this->logStream->count() === 1){
294  $this->synchronized(function(){
295  $this->notify();
296  });
297  }
298  }

◆ setConsoleCallback()

setConsoleCallback (   $callback)
Parameters
$callback
369  {
370  $this->consoleCallback = $callback;
371  }

◆ setLogDebug()

setLogDebug (   $logDebug)
Parameters
bool$logDebug
159  {
160  $this->logDebug = (bool) $logDebug;
161  }

◆ setSendMsg()

setSendMsg (   $b)
Parameters
$b
49  {
50  $this->shouldRecordMsg = $b;
51  $this->lastGet = time();
52  }

◆ setWrite()

setWrite (   $write)
Parameters
$write
362  {
363  $this->write = $write;
364  }

◆ shutdown()

shutdown ( )
243  {
244  $this->shutdown = true;
245  }

◆ warning()

warning (   $message,
  $name = "WARNING" 
)
Parameters
string$message
string$name
125  {
126  $this->send($message, \LogLevel::WARNING, $name, TextFormat::YELLOW);
127  }

Field Documentation

◆ $logDebug

$logDebug
protected

◆ $logFile

$logFile
protected

◆ $logger

$logger = null
static

◆ $logStream

$logStream
protected

◆ $shouldRecordMsg

$shouldRecordMsg = false

◆ $shouldSendMsg

$shouldSendMsg = ""

◆ $shutdown

$shutdown
protected

◆ $write

$write = false
protected

Extra Settings


The documentation for this class was generated from the following file: