Skip to content

Commit

Permalink
added timestamp output class for console
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Preusner committed Nov 10, 2013
1 parent d15a143 commit 59af858
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
86 changes: 86 additions & 0 deletions Console/Output/ConsoleTimestampOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace EightPoints\Bundle\AssistBundle\Console\Output;

use Symfony\Component\Console\Output\ConsoleOutput,
Symfony\Component\Console\Formatter\OutputFormatterInterface,
Symfony\Component\Console\Output\ConsoleOutputInterface;

/**
* ConsoleTimestampOutput
* Adds timestamp to each written line in console
*
* @package EightPoints\Bundle\AssistBundle\Console\Output
*
* @copyright 8points IT
* @author Florian Preusner
*
* @version 1.0
* @since 2013-11
*/
class ConsoleTimestampOutput extends ConsoleOutput implements ConsoleOutputInterface {

/**
* @var float $lastTime
*/
protected $lastTime = 0;

/**
* Construct
*
* @author Florian Preusner
* @version 1.0
* @since 2013-11
*
* @param integer $verbosity the verbosity level
* @param boolean $decorated whether to decorate messages or not (null for auto-guessing)
* @param OutputFormatterInterface $formatter output formatter instance
*/
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null) {

$this->lastTime = microtime(true);

parent::__construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, $formatter = null);
}

/**
* Write message
*
* @author Florian Preusner
* @version 1.0
* @since 2013-11
*
* @param string $message
* @param @todo $newline
*
* @return void
*/
protected function doWrite($message, $newline) {

$message = $this->addTimeStamp($message);

parent::doWrite($message, $newline);
}

/**
* Add timestamp/duration to given string
*
* @author Florian Preusner
* @version 1.0
* @since 2013-11
*
* @param string $message
* @return string $message
*/
protected function addTimeStamp($message) {

$now = microtime(true);
$diff = number_format($now - $this->lastTime, 3);
$prefix = sprintf('[%s :: %f]', date('H:i:s'), $diff);
$message = str_pad($prefix, 25) . $message;

$this->lastTime = microtime(true);

return $message;
}
}
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ Helpers

CommandTimestampOutput
----------------------
Description is following...
To use a timestamp for each printed line on your command just inject the CommandTimestampOutput into your console application.
app/console
``` php
use EightPoints\Bundle\AssistBundle\Console\Output\ConsoleTimestampOutput;

$output = new ConsoleTimestampOutput();
...
$application->run($input, $output);
```

Example output:
```
[20:09:30 :: 0.002] Starting import...
[20:09:31 :: 8.058] Finished import
```

Authors
-------
Expand Down

0 comments on commit 59af858

Please sign in to comment.