OwnCloud.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  6. * @author Thomas Pulzer <t.pulzer@kniel.de>
  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\Core\Command\Log;
  24. use \OCP\IConfig;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Input\InputArgument;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. class OwnCloud extends Command {
  31. /** @var IConfig */
  32. protected $config;
  33. public function __construct(IConfig $config) {
  34. $this->config = $config;
  35. parent::__construct();
  36. }
  37. protected function configure() {
  38. $this
  39. ->setName('log:owncloud')
  40. ->setDescription('manipulate ownCloud logging backend')
  41. ->addOption(
  42. 'enable',
  43. null,
  44. InputOption::VALUE_NONE,
  45. 'enable this logging backend'
  46. )
  47. ->addOption(
  48. 'file',
  49. null,
  50. InputOption::VALUE_REQUIRED,
  51. 'set the log file path'
  52. )
  53. ->addOption(
  54. 'rotate-size',
  55. null,
  56. InputOption::VALUE_REQUIRED,
  57. 'set the file size for log rotation, 0 = disabled'
  58. )
  59. ;
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output) {
  62. $toBeSet = [];
  63. if ($input->getOption('enable')) {
  64. $toBeSet['log_type'] = 'owncloud';
  65. }
  66. if ($file = $input->getOption('file')) {
  67. $toBeSet['logfile'] = $file;
  68. }
  69. if (($rotateSize = $input->getOption('rotate-size')) !== null) {
  70. $rotateSize = \OCP\Util::computerFileSize($rotateSize);
  71. $this->validateRotateSize($rotateSize);
  72. $toBeSet['log_rotate_size'] = $rotateSize;
  73. }
  74. // set config
  75. foreach ($toBeSet as $option => $value) {
  76. $this->config->setSystemValue($option, $value);
  77. }
  78. // display config
  79. if ($this->config->getSystemValue('log_type', 'owncloud') === 'owncloud') {
  80. $enabledText = 'enabled';
  81. } else {
  82. $enabledText = 'disabled';
  83. }
  84. $output->writeln('Log backend ownCloud: '.$enabledText);
  85. $dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data');
  86. $defaultLogFile = rtrim($dataDir, '/').'/nextcloud.log';
  87. $output->writeln('Log file: '.$this->config->getSystemValue('logfile', $defaultLogFile));
  88. $rotateSize = $this->config->getSystemValue('log_rotate_size', 0);
  89. if ($rotateSize) {
  90. $rotateString = \OCP\Util::humanFileSize($rotateSize);
  91. } else {
  92. $rotateString = 'disabled';
  93. }
  94. $output->writeln('Rotate at: '.$rotateString);
  95. }
  96. /**
  97. * @param mixed $rotateSize
  98. * @throws \InvalidArgumentException
  99. */
  100. protected function validateRotateSize(&$rotateSize) {
  101. if ($rotateSize === false) {
  102. throw new \InvalidArgumentException('Error parsing log rotation file size');
  103. }
  104. $rotateSize = (int) $rotateSize;
  105. if ($rotateSize < 0) {
  106. throw new \InvalidArgumentException('Log rotation file size must be non-negative');
  107. }
  108. }
  109. }