/** * Send print to terminal. */ private static function _log($msgType, $args) { if (!Config::get('debug_mode', 0)) { return; } if (count($args) == 1) { $msg = is_scalar($args[0]) ? $args[0] : self::dump($args[0]); } else { $msg = self::dump($args); } $mt = explode(' ', microtime()); if (self::$DEBUG_TRACE) { $trace = self::getTrace(); } else { $trace = array(); } $msg = ' ' . $msg . " mem: " . Utils::convertSize(memory_get_usage()) . "\n"; Terminal::drawStr('[' . Daemon::$pid . '][' . date('H:i:s', $mt[1]) . '.' . sprintf('%06d', $mt[0] * 1000000) . ']', 'default'); if ($msgType == 'debug') { Terminal::drawStr($msg, 'magenta'); } else { if ($msgType == 'log') { Terminal::drawStr($msg, 'lightgray'); } else { if ($msgType == 'error') { Terminal::drawStr($msg, 'red'); } else { if ($msgType == 'info') { Terminal::drawStr($msg, 'brown'); } else { Terminal::drawStr($msg, 'default'); } } } } //echo "\n"; !empty($trace) && Terminal::drawStr("\t" . implode(" <-- ", $trace) . "\n"); }
public static function init() { Daemon::$eventBase = event_base_new(); $args = $_SERVER['argv']; if (!isset($args[1]) || !isset($args[2])) { Terminal::drawStr('usage: php xxx.php sArea (start|stop|restart|reload|status)' . "\n"); exit(-1); } // todo run multi app in one daemon. $sArea = $args[1]; $command = $args[2]; $appName = Config::get('socket_server_class', null, true); Daemon::$runName = $appName; $runAppInstance = array($appName => $appName); if ($command == 'start') { // fork later self::$pid = posix_getpid(); Debug::log('start'); foreach ($runAppInstance as $appName) { $obj = new $appName(); } while (!Daemon::$breakEventLoop) { event_base_loop(daemon::$eventBase, EVLOOP_ONCE); // 清空本次写状态数组 gc_collect_cycles(); //daemon::debug('<== evet_base_loop() ending'); } } elseif ($command == 'stop') { // cat pid, stop } elseif ($command == 'status') { // cat pid, show status } elseif ($command == 'restart') { //self::stop(); //self::start(); } }