File.php 4.4 KB

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