예제 #1
0
 /**
  * Create a mock appender which simply stores all messages passed to 
  * its append() method.
  *
  * @return  util.log.Appender
  */
 private function mockAppender()
 {
     $appender = newinstance(Appender::class, [], ['messages' => [], 'append' => function (LoggingEvent $event) {
         $this->messages[] = [strtolower(LogLevel::nameOf($event->getLevel())), $this->layout->format($event)];
     }]);
     return $appender->withLayout(new PatternLayout('%m'));
 }
예제 #2
0
 /**
  * Formats a logging event according to this layout
  *
  * @param   util.log.LoggingEvent event
  * @return  string
  */
 public function format(\util\log\LoggingEvent $event)
 {
     $out = '';
     foreach ($this->format as $token) {
         switch ($token) {
             case '%m':
                 $out .= implode(' ', array_map([$this, 'stringOf'], $event->getArguments()));
                 break;
             case '%t':
                 $out .= gmdate('H:i:s', $event->getTimestamp());
                 break;
             case '%c':
                 $out .= $event->getCategory()->identifier;
                 break;
             case '%l':
                 $out .= strtolower(\util\log\LogLevel::nameOf($event->getLevel()));
                 break;
             case '%L':
                 $out .= strtoupper(\util\log\LogLevel::nameOf($event->getLevel()));
                 break;
             case '%p':
                 $out .= $event->getProcessId();
                 break;
             case '%x':
                 $out .= null == ($context = $event->getContext()) ? '' : $context->format();
                 break;
             default:
                 $out .= $token;
         }
     }
     return $out;
 }
 public function unknown()
 {
     LogLevel::named('@UNKNOWN@');
 }
예제 #4
0
 /**
  * Entry point method
  *
  * @param   string[] args
  */
 public static function main(array $args)
 {
     if (empty($args)) {
         return self::usage();
     }
     foreach (ClassLoader::getLoaders() as $loader) {
         if ($loader instanceof JitClassLoader) {
             ClassLoader::removeLoader($loader);
         }
     }
     // Set up compiler
     $compiler = new Compiler();
     $manager = new FileManager();
     $manager->setSourcePaths(\xp::$classpath);
     // Handle arguments
     $profiles = ['default'];
     $emitter = 'php5.5';
     $result = function ($success) {
         return $success ? 0 : 1;
     };
     $files = [];
     $listener = new DefaultDiagnosticListener(Console::$out);
     for ($i = 0, $s = sizeof($args); $i < $s; $i++) {
         if ('-?' === $args[$i] || '--help' === $args[$i]) {
             return self::usage();
         } else {
             if ('-cp' === $args[$i]) {
                 \lang\ClassLoader::registerPath($args[++$i]);
             } else {
                 if ('-sp' === $args[$i]) {
                     $manager->addSourcePath($args[++$i]);
                 } else {
                     if ('-v' === $args[$i]) {
                         $listener = new VerboseDiagnosticListener(Console::$out);
                     } else {
                         if ('-q' === $args[$i]) {
                             $listener = new QuietDiagnosticListener(Console::$out);
                         } else {
                             if ('-t' === $args[$i]) {
                                 $levels = LogLevel::NONE;
                                 foreach (explode(',', $args[++$i]) as $level) {
                                     $levels |= LogLevel::named($level);
                                 }
                                 $compiler->setTrace(create(new LogCategory('xcc'))->withAppender(new ConsoleAppender(), $levels));
                             } else {
                                 if ('-E' === $args[$i]) {
                                     $emitter = $args[++$i];
                                 } else {
                                     if ('-p' === $args[$i]) {
                                         $profiles = explode(',', $args[++$i]);
                                     } else {
                                         if ('-o' === $args[$i]) {
                                             $output = $args[++$i];
                                             $folder = new Folder($output);
                                             $folder->exists() || $folder->create();
                                             $manager->setOutput($folder);
                                         } else {
                                             if ('-N' === $args[$i]) {
                                                 $dir = $args[++$i];
                                                 $manager->addSourcePath($dir);
                                                 $files = array_merge($files, self::fromFolder($dir, false));
                                             } else {
                                                 if (is_dir($args[$i])) {
                                                     $dir = $args[$i];
                                                     $manager->addSourcePath($dir);
                                                     $files = array_merge($files, self::fromFolder($dir, true));
                                                 } else {
                                                     $files[] = new FileSource(new File($args[$i]));
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // Check
     if (empty($files)) {
         Console::$err->writeLine('*** No files given (-? will show usage)');
         return 2;
     }
     // Setup emitter
     sscanf($emitter, '%[^0-9]%d.%d', $language, $major, $minor);
     try {
         $emit = \lang\XPClass::forName('xp.compiler.emit.Emitter')->cast(Package::forName('xp.compiler.emit')->getPackage($language)->loadClass(($major ? 'V' . $major . $minor : '') . 'Emitter')->newInstance());
     } catch (\lang\ClassCastException $e) {
         Console::$err->writeLine('*** Not an emitter implementation: ', $e->compoundMessage());
         return 4;
     } catch (\lang\IllegalAccessException $e) {
         Console::$err->writeLine('*** Cannot use emitter named "', $emitter, '": ', $e->compoundMessage());
         return 4;
     } catch (\lang\Throwable $e) {
         Console::$err->writeLine('*** No emitter named "', $emitter, '": ', $e->compoundMessage());
         return 4;
     }
     // Load compiler profile configurations
     try {
         $reader = new CompilationProfileReader();
         foreach ($profiles as $configuration) {
             $reader->addSource(new Properties('res://xp/compiler/' . $configuration . '.xcp.ini'));
         }
         $emit->setProfile($reader->getProfile());
     } catch (\lang\Throwable $e) {
         Console::$err->writeLine('*** Cannot load profile configuration(s) ' . implode(',', $profiles) . ': ' . $e->getMessage());
         return 3;
     }
     // Compile files and pass return value to result handler
     return $result($compiler->compile($files, $listener, $manager, $emit), array_slice($args, $i + 1));
 }
 /**
  * Sets a logger to use. Accepts either a closure, a util.log.LogCategory
  * instance or NULL (to unset).
  *
  * @param  var $logger
  * @return self this
  * @throws lang.IllegalArgumentException on argument mismatch
  */
 public function withLogger($logger)
 {
     if ($logger instanceof \Closure) {
         $this->setBuiltin('log', function ($items, $context, $options) use($logger) {
             $logger($options);
             return '';
         });
     } else {
         if ($logger instanceof LogCategory) {
             $this->setBuiltin('log', function ($items, $context, $options) use($logger) {
                 if (sizeof($options) > 1) {
                     $logger->log(LogLevel::named(array_shift($options)), $options);
                 } else {
                     $logger->log(LogLevel::DEBUG, $options);
                 }
                 return '';
             });
         } else {
             if (null === $logger) {
                 $this->setBuiltin('log', null);
             } else {
                 throw new IllegalArgumentException('Expect either a closure, a util.log.LogCategory or NULL, ' . \xp::typeOf($logger) . ' given');
             }
         }
     }
     return $this;
 }
예제 #6
0
 /**
  * Configure this logger
  *
  * @param   util.Properties prop instance of a Properties object
  */
 public function configure($prop)
 {
     // Read all other properties
     $section = $prop->getFirstSection();
     do {
         $this->category[$section] = \lang\XPClass::forName($prop->readString($section, 'category', 'util.log.LogCategory'))->newInstance($section, $prop->readInteger($section, 'flags', LogLevel::ALL));
         // Configure appenders
         $appenders = $prop->readArray($section, 'appenders', []);
         // Go through all of the appenders, loading classes as necessary
         foreach ($appenders as $appender) {
             // Read levels (alternatively, for BC, read "flags" setting)
             $levels = $prop->readArray($section, 'appender.' . $appender . '.levels');
             if (!empty($levels)) {
                 $flags = 0;
                 foreach ($levels as $name) {
                     $flags |= LogLevel::named($name);
                 }
             } else {
                 $flags = $prop->readArray($section, 'appender.' . $appender . '.flags', LogLevel::ALL);
                 if (!is_int($flags)) {
                     $arrflags = $flags;
                     $flags = 0;
                     foreach ($arrflags as $f) {
                         try {
                             $flags |= LogLevel::named(substr($f, 12));
                             // 12 = strlen('LOGGER_FLAG_')
                         } catch (\lang\IllegalArgumentException $ignore) {
                             // ...
                         }
                     }
                 }
             }
             $a = $this->category[$section]->addAppender(\lang\XPClass::forName($appender)->newInstance(), $flags);
             // Params
             $params = $prop->readArray($section, 'appender.' . $appender . '.params', []);
             foreach ($params as $param) {
                 $a->{$param} = $prop->readString($section, 'appender.' . $appender . '.param.' . $param, '');
             }
             // Layout
             if ($layout = $prop->readArray($section, 'appender.' . $appender . '.layout')) {
                 $class = \lang\XPClass::forName(array_shift($layout));
                 if ($class->hasConstructor()) {
                     $a->setLayout($class->getConstructor()->newInstance($layout));
                 } else {
                     $a->setLayout($class->newInstance());
                 }
             }
             // Set context
             if ('' !== ($contextFQN = $prop->readString($section, 'context', ''))) {
                 $this->category[$section]->setContext(\lang\XPClass::forName($contextFQN)->newInstance());
             }
         }
     } while ($section = $prop->getNextSection());
 }
예제 #7
0
 public function nameOf_illegal_loglevel()
 {
     LogLevel::nameOf(-1);
 }