TimestampFormatter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Console;
  23. use OCP\IConfig;
  24. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  25. use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface;
  26. class TimestampFormatter implements OutputFormatterInterface {
  27. /** @var IConfig */
  28. protected $config;
  29. /** @var OutputFormatterInterface */
  30. protected $formatter;
  31. /**
  32. * @param IConfig $config
  33. * @param OutputFormatterInterface $formatter
  34. */
  35. public function __construct(IConfig $config, OutputFormatterInterface $formatter) {
  36. $this->config = $config;
  37. $this->formatter = $formatter;
  38. }
  39. /**
  40. * Sets the decorated flag.
  41. *
  42. * @param bool $decorated Whether to decorate the messages or not
  43. */
  44. public function setDecorated($decorated) {
  45. $this->formatter->setDecorated($decorated);
  46. }
  47. /**
  48. * Gets the decorated flag.
  49. *
  50. * @return bool true if the output will decorate messages, false otherwise
  51. */
  52. public function isDecorated() {
  53. return $this->formatter->isDecorated();
  54. }
  55. /**
  56. * Sets a new style.
  57. *
  58. * @param string $name The style name
  59. * @param OutputFormatterStyleInterface $style The style instance
  60. */
  61. public function setStyle($name, OutputFormatterStyleInterface $style) {
  62. $this->formatter->setStyle($name, $style);
  63. }
  64. /**
  65. * Checks if output formatter has style with specified name.
  66. *
  67. * @param string $name
  68. * @return bool
  69. */
  70. public function hasStyle($name) {
  71. return $this->formatter->hasStyle($name);
  72. }
  73. /**
  74. * Gets style options from style with specified name.
  75. *
  76. * @param string $name
  77. * @return OutputFormatterStyleInterface
  78. * @throws \InvalidArgumentException When style isn't defined
  79. */
  80. public function getStyle($name) {
  81. return $this->formatter->getStyle($name);
  82. }
  83. /**
  84. * Formats a message according to the given styles.
  85. *
  86. * @param string $message The message to style
  87. * @return string The styled message, prepended with a timestamp using the
  88. * log timezone and dateformat, e.g. "2015-06-23T17:24:37+02:00"
  89. */
  90. public function format($message) {
  91. $timeZone = $this->config->getSystemValue('logtimezone', 'UTC');
  92. $timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null;
  93. $time = new \DateTime('now', $timeZone);
  94. $timestampInfo = $time->format($this->config->getSystemValue('logdateformat', \DateTime::ATOM));
  95. return $timestampInfo . ' ' . $this->formatter->format($message);
  96. }
  97. }