PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pocketmine/utils/MainLogger.php

https://gitlab.com/wesleyvanneck/ImagicalMine
PHP | 402 lines | 206 code | 54 blank | 142 comment | 17 complexity | 042db9f05ebd359f21075d7bec2a59b4 MD5 | raw file
  1. <?php
  2. /**
  3. * src/pocketmine/utils/MainLogger.php
  4. *
  5. * @package default
  6. */
  7. /*
  8. *
  9. * _ _ _ __ __ _
  10. * (_) (_) | | \/ (_)
  11. * _ _ __ ___ __ _ __ _ _ ___ __ _| | \ / |_ _ __ ___
  12. * | | '_ ` _ \ / _` |/ _` | |/ __/ _` | | |\/| | | '_ \ / _ \
  13. * | | | | | | | (_| | (_| | | (_| (_| | | | | | | | | | __/
  14. * |_|_| |_| |_|\__,_|\__, |_|\___\__,_|_|_| |_|_|_| |_|\___|
  15. * __/ |
  16. * |___/
  17. *
  18. * This program is a third party build by ImagicalMine.
  19. *
  20. * PocketMine is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Lesser General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * @author ImagicalMine Team
  26. * @link http://forums.imagicalcorp.ml/
  27. *
  28. *
  29. */
  30. namespace pocketmine\utils;
  31. use LogLevel;
  32. use pocketmine\Thread;
  33. use pocketmine\Worker;
  34. class MainLogger extends \AttachableThreadedLogger
  35. {
  36. protected $logFile;
  37. protected $logStream;
  38. protected $shutdown;
  39. protected $logDebug;
  40. protected $logEnabled = true;
  41. private $logResource;
  42. private $enabled;
  43. /** @var MainLogger */
  44. public static $logger = null;
  45. /**
  46. *
  47. * @throws \RuntimeException
  48. * @param string $logFile
  49. * @param bool $logDebug (optional)
  50. */
  51. public function __construct($logFile, $logDebug = false)
  52. {
  53. if (static::$logger instanceof MainLogger) {
  54. throw new \RuntimeException("MainLogger has been already created");
  55. }
  56. static::$logger = $this;
  57. $this->enabled=false;
  58. touch($logFile);
  59. $this->logFile = $logFile;
  60. $this->logDebug = (bool) $logDebug;
  61. // $this->logEnabled = (bool) false;
  62. $this->logStream = new \Threaded;
  63. $this->start();
  64. }
  65. /**
  66. *
  67. * @return MainLogger
  68. */
  69. public function Disable()
  70. {
  71. $this->enabled = false;
  72. }
  73. /**
  74. *
  75. */
  76. public function Enable()
  77. {
  78. $this->enabled = true;
  79. }
  80. /**
  81. *
  82. * @return unknown
  83. */
  84. public static function getLogger()
  85. {
  86. return static::$logger;
  87. }
  88. /**
  89. *
  90. * @param unknown $message
  91. */
  92. public function emergency($message)
  93. {
  94. $this->send($message, \LogLevel::EMERGENCY, "emergency", TextFormat::RED);
  95. }
  96. /**
  97. *
  98. * @param unknown $message
  99. */
  100. public function alert($message)
  101. {
  102. $this->send($message, \LogLevel::ALERT, "alert", TextFormat::RED);
  103. }
  104. /**
  105. *
  106. * @param unknown $message
  107. */
  108. public function critical($message)
  109. {
  110. $this->send($message, \LogLevel::CRITICAL, "critical", TextFormat::RED);
  111. }
  112. /**
  113. *
  114. * @param unknown $message
  115. */
  116. public function error($message)
  117. {
  118. $this->send($message, \LogLevel::ERROR, "error", TextFormat::DARK_RED);
  119. }
  120. /**
  121. *
  122. * @param unknown $message
  123. */
  124. public function warning($message)
  125. {
  126. $this->send($message, \LogLevel::WARNING, "warning", TextFormat::YELLOW);
  127. }
  128. /**
  129. *
  130. * @param unknown $message
  131. */
  132. public function notice($message)
  133. {
  134. $this->send($message, \LogLevel::NOTICE, "notice", TextFormat::AQUA);
  135. }
  136. /**
  137. *
  138. * @param unknown $message
  139. */
  140. public function info($message)
  141. {
  142. $this->send($message, \LogLevel::INFO, "system", TextFormat::GOLD);
  143. }
  144. /**
  145. *
  146. * @param unknown $message
  147. */
  148. public function debug($message)
  149. {
  150. if ($this->logDebug === false) {
  151. return;
  152. }
  153. $this->send($message, \LogLevel::DEBUG, "debug", TextFormat::AQUA);
  154. }
  155. /**
  156. *
  157. * @param bool $logDebug
  158. */
  159. public function setLogDebug($logDebug)
  160. {
  161. $this->logDebug = (bool) $logDebug;
  162. }
  163. /**
  164. *
  165. * @param Throwable $e
  166. * @param unknown $trace (optional)
  167. */
  168. public function logException(\Throwable $e, $trace = null)
  169. {
  170. if ($trace === null) {
  171. $trace = $e->getTrace();
  172. }
  173. $errstr = $e->getMessage();
  174. $errfile = $e->getFile();
  175. $errno = $e->getCode();
  176. $errline = $e->getLine();
  177. $errorConversion = [
  178. 0 => "EXCEPTION",
  179. E_ERROR => "E_ERROR",
  180. E_WARNING => "E_WARNING",
  181. E_PARSE => "E_PARSE",
  182. E_NOTICE => "E_NOTICE",
  183. E_CORE_ERROR => "E_CORE_ERROR",
  184. E_CORE_WARNING => "E_CORE_WARNING",
  185. E_COMPILE_ERROR => "E_COMPILE_ERROR",
  186. E_COMPILE_WARNING => "E_COMPILE_WARNING",
  187. E_USER_ERROR => "E_USER_ERROR",
  188. E_USER_WARNING => "E_USER_WARNING",
  189. E_USER_NOTICE => "E_USER_NOTICE",
  190. E_STRICT => "E_STRICT",
  191. E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
  192. E_DEPRECATED => "E_DEPRECATED",
  193. E_USER_DEPRECATED => "E_USER_DEPRECATED",
  194. ];
  195. if ($errno === 0) {
  196. $type = LogLevel::CRITICAL;
  197. } else {
  198. $type = ($errno === E_ERROR or $errno === E_USER_ERROR) ? LogLevel::ERROR : (($errno === E_USER_WARNING or $errno === E_WARNING) ? LogLevel::WARNING : LogLevel::NOTICE);
  199. }
  200. $errno = isset($errorConversion[$errno]) ? $errorConversion[$errno] : $errno;
  201. if (($pos = strpos($errstr, "\n")) !== false) {
  202. $errstr = substr($errstr, 0, $pos);
  203. }
  204. $errfile = \pocketmine\cleanPath($errfile);
  205. $this->log($type, get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline");
  206. foreach (@\pocketmine\getTrace(1, $trace) as $i => $line) {
  207. $this->debug($line);
  208. }
  209. }
  210. /**
  211. *
  212. * @param unknown $level
  213. * @param unknown $message
  214. */
  215. public function log($level, $message)
  216. {
  217. switch ($level) {
  218. case LogLevel::EMERGENCY:
  219. $this->emergency($message);
  220. break;
  221. case LogLevel::ALERT:
  222. $this->alert($message);
  223. break;
  224. case LogLevel::CRITICAL:
  225. $this->critical($message);
  226. break;
  227. case LogLevel::ERROR:
  228. $this->error($message);
  229. break;
  230. case LogLevel::WARNING:
  231. $this->warning($message);
  232. break;
  233. case LogLevel::NOTICE:
  234. $this->notice($message);
  235. break;
  236. case LogLevel::INFO:
  237. $this->info($message);
  238. break;
  239. case LogLevel::DEBUG:
  240. $this->debug($message);
  241. break;
  242. }
  243. }
  244. /**
  245. *
  246. */
  247. public function shutdown()
  248. {
  249. $this->shutdown = true;
  250. }
  251. /**
  252. *
  253. * @param unknown $message
  254. * @param unknown $level
  255. * @param unknown $prefix
  256. * @param unknown $color
  257. */
  258. protected function send($message, $level, $prefix, $color)
  259. {
  260. $now = time();
  261. $thread = \Thread::getCurrentThread();
  262. if ($thread === null) {
  263. $threadName = "Server thread";
  264. } elseif ($thread instanceof Thread or $thread instanceof Worker) {
  265. $threadName = $thread->getThreadName() . " thread";
  266. } else {
  267. $threadName = (new \ReflectionClass($thread))->getShortName() . " thread";
  268. }
  269. $message = TextFormat::toANSI(TextFormat::AQUA . "[" . date("H:i:s", $now) . "] ". TextFormat::RESET . $color . $prefix . "> " . TextFormat::WHITE . $message . TextFormat::RESET);
  270. $cleanMessage = TextFormat::clean($message);
  271. if (!Terminal::hasFormattingCodes()) {
  272. echo $cleanMessage . PHP_EOL;
  273. } else {
  274. echo $message . PHP_EOL;
  275. }
  276. if ($this->attachment instanceof \ThreadedLoggerAttachment) {
  277. $this->attachment->call($level, $message);
  278. }
  279. $this->logStream[] = date("Y-m-d", $now) . " " . $cleanMessage . "\n";
  280. if ($this->logStream->count() === 1) {
  281. $this->synchronized(function () {
  282. $this->notify();
  283. });
  284. }
  285. }
  286. /**
  287. *
  288. * @param boolean $state
  289. */
  290. public function setLoggerState($state)
  291. {
  292. $this->logEnabled = $state;
  293. }
  294. /*public function run(){
  295. $this->shutdown = false;
  296. if($this->logEnabled){// need to be extended. Totally disabled log file now
  297. $this->logResource = fopen($this->logFile, "a+b");
  298. if(!is_resource($this->logResource)){
  299. throw new \RuntimeException("Couldn't open log file");
  300. }
  301. while($this->shutdown === false){
  302. $this->synchronized(function (){
  303. while($this->logStream->count() > 0){
  304. $chunk = $this->logStream->shift();
  305. fwrite($this->logResource, $chunk);
  306. }
  307. $this->wait(25000);
  308. });
  309. }
  310. if($this->logStream->count() > 0){
  311. while($this->logStream->count() > 0){
  312. $chunk = $this->logStream->shift();
  313. fwrite($this->logResource, $chunk);
  314. }
  315. }
  316. fclose($this->logResource);
  317. }
  318. }*/
  319. /**
  320. *
  321. */
  322. public function run()
  323. {
  324. $this->shutdown = false;
  325. while ($this->shutdown === false) {
  326. /**
  327. *
  328. */
  329. $this->synchronized(function () {
  330. while ($this->logStream->count() > 0 and $this->enabled) {
  331. $chunk = $this->logStream->shift();
  332. $this->logResource = file_put_contents($this->logFile, $chunk, FILE_APPEND);
  333. }
  334. $this->wait(25000);
  335. });
  336. }
  337. if ($this->logStream->count() > 0) {
  338. while ($this->logStream->count() > 0 and $this->enabled) {
  339. $chunk = $this->logStream->shift();
  340. $this->logResource = file_put_contents($this->logFile, $chunk, FILE_APPEND);
  341. }
  342. }
  343. }
  344. }