File.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Pulzer <t.pulzer@kniel.de>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Command\Log;
  27. use OCP\IConfig;
  28. use Stecman\Component\Symfony\Console\BashCompletion\Completion;
  29. use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion;
  30. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class File extends Command implements Completion\CompletionAwareInterface {
  36. protected IConfig $config;
  37. public function __construct(IConfig $config) {
  38. $this->config = $config;
  39. parent::__construct();
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('log:file')
  44. ->setDescription('manipulate logging backend')
  45. ->addOption(
  46. 'enable',
  47. null,
  48. InputOption::VALUE_NONE,
  49. 'enable this logging backend'
  50. )
  51. ->addOption(
  52. 'file',
  53. null,
  54. InputOption::VALUE_REQUIRED,
  55. 'set the log file path'
  56. )
  57. ->addOption(
  58. 'rotate-size',
  59. null,
  60. InputOption::VALUE_REQUIRED,
  61. 'set the file size for log rotation, 0 = disabled'
  62. )
  63. ;
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $toBeSet = [];
  67. if ($input->getOption('enable')) {
  68. $toBeSet['log_type'] = 'file';
  69. }
  70. if ($file = $input->getOption('file')) {
  71. $toBeSet['logfile'] = $file;
  72. }
  73. if (($rotateSize = $input->getOption('rotate-size')) !== null) {
  74. $rotateSize = \OCP\Util::computerFileSize($rotateSize);
  75. $this->validateRotateSize($rotateSize);
  76. $toBeSet['log_rotate_size'] = $rotateSize;
  77. }
  78. // set config
  79. foreach ($toBeSet as $option => $value) {
  80. $this->config->setSystemValue($option, $value);
  81. }
  82. // display config
  83. // TODO: Drop backwards compatibility for config in the future
  84. $logType = $this->config->getSystemValue('log_type', 'file');
  85. if ($logType === 'file' || $logType === 'owncloud') {
  86. $enabledText = 'enabled';
  87. } else {
  88. $enabledText = 'disabled';
  89. }
  90. $output->writeln('Log backend file: '.$enabledText);
  91. $dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data');
  92. $defaultLogFile = rtrim($dataDir, '/').'/nextcloud.log';
  93. $output->writeln('Log file: '.$this->config->getSystemValue('logfile', $defaultLogFile));
  94. $rotateSize = $this->config->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
  95. if ($rotateSize) {
  96. $rotateString = \OCP\Util::humanFileSize($rotateSize);
  97. } else {
  98. $rotateString = 'disabled';
  99. }
  100. $output->writeln('Rotate at: '.$rotateString);
  101. return 0;
  102. }
  103. /**
  104. * @param mixed $rotateSize
  105. * @throws \InvalidArgumentException
  106. */
  107. protected function validateRotateSize(&$rotateSize) {
  108. if ($rotateSize === false) {
  109. throw new \InvalidArgumentException('Error parsing log rotation file size');
  110. }
  111. $rotateSize = (int) $rotateSize;
  112. if ($rotateSize < 0) {
  113. throw new \InvalidArgumentException('Log rotation file size must be non-negative');
  114. }
  115. }
  116. /**
  117. * @param string $optionName
  118. * @param CompletionContext $context
  119. * @return string[]
  120. */
  121. public function completeOptionValues($optionName, CompletionContext $context) {
  122. if ($optionName === 'file') {
  123. $helper = new ShellPathCompletion(
  124. $this->getName(),
  125. 'file',
  126. Completion::TYPE_OPTION
  127. );
  128. return $helper->run();
  129. } elseif ($optionName === 'rotate-size') {
  130. return [0];
  131. }
  132. return [];
  133. }
  134. /**
  135. * @param string $argumentName
  136. * @param CompletionContext $context
  137. * @return string[]
  138. */
  139. public function completeArgumentValues($argumentName, CompletionContext $context) {
  140. return [];
  141. }
  142. }