You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

3 years ago
  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 $message The message to style
  88. * @return string 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($message) {
  92. $timeZone = $this->config->getSystemValue('logtimezone', 'UTC');
  93. $timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null;
  94. $time = new \DateTime('now', $timeZone);
  95. $timestampInfo = $time->format($this->config->getSystemValue('logdateformat', \DateTime::ATOM));
  96. return $timestampInfo . ' ' . $this->formatter->format($message);
  97. }
  98. }