TimestampFormatter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /**
  30. * @param IConfig $config
  31. * @param OutputFormatterInterface $formatter
  32. */
  33. public function __construct(IConfig $config, OutputFormatterInterface $formatter) {
  34. $this->config = $config;
  35. $this->formatter = $formatter;
  36. }
  37. /**
  38. * Sets the decorated flag.
  39. *
  40. * @param bool $decorated Whether to decorate the messages or not
  41. */
  42. public function setDecorated($decorated) {
  43. $this->formatter->setDecorated($decorated);
  44. }
  45. /**
  46. * Gets the decorated flag.
  47. *
  48. * @return bool true if the output will decorate messages, false otherwise
  49. */
  50. public function isDecorated() {
  51. return $this->formatter->isDecorated();
  52. }
  53. /**
  54. * Sets a new style.
  55. *
  56. * @param string $name The style name
  57. * @param OutputFormatterStyleInterface $style The style instance
  58. */
  59. public function setStyle($name, OutputFormatterStyleInterface $style) {
  60. $this->formatter->setStyle($name, $style);
  61. }
  62. /**
  63. * Checks if output formatter has style with specified name.
  64. *
  65. * @param string $name
  66. * @return bool
  67. */
  68. public function hasStyle($name) {
  69. $this->formatter->hasStyle($name);
  70. }
  71. /**
  72. * Gets style options from style with specified name.
  73. *
  74. * @param string $name
  75. * @return OutputFormatterStyleInterface
  76. */
  77. public function getStyle($name) {
  78. return $this->formatter->getStyle($name);
  79. }
  80. /**
  81. * Formats a message according to the given styles.
  82. *
  83. * @param string $message The message to style
  84. * @return string The styled message, prepended with a timestamp using the
  85. * log timezone and dateformat, e.g. "2015-06-23T17:24:37+02:00"
  86. */
  87. public function format($message) {
  88. $timeZone = $this->config->getSystemValue('logtimezone', 'UTC');
  89. $timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null;
  90. $time = new \DateTime('now', $timeZone);
  91. $timestampInfo = $time->format($this->config->getSystemValue('logdateformat', \DateTime::ATOM));
  92. return $timestampInfo . ' ' . $this->formatter->format($message);
  93. }
  94. }