TimestampFormatter.php 3.3 KB

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