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.

96 lines
2.1 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Piotr Mrówczyński <mrow4a@yahoo.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Diagnostics;
  26. use OC\Cache\CappedMemoryCache;
  27. use OCP\Diagnostics\IQueryLogger;
  28. class QueryLogger implements IQueryLogger {
  29. /**
  30. * @var \OC\Diagnostics\Query
  31. */
  32. protected $activeQuery;
  33. /**
  34. * @var CappedMemoryCache
  35. */
  36. protected $queries;
  37. /**
  38. * QueryLogger constructor.
  39. */
  40. public function __construct() {
  41. $this->queries = new CappedMemoryCache(1024);
  42. }
  43. /**
  44. * @var bool - Module needs to be activated by some app
  45. */
  46. private $activated = false;
  47. /**
  48. * @inheritdoc
  49. */
  50. public function startQuery($sql, array $params = null, array $types = null) {
  51. if ($this->activated) {
  52. $this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack());
  53. }
  54. }
  55. private function getStack() {
  56. $stack = debug_backtrace();
  57. array_shift($stack);
  58. array_shift($stack);
  59. array_shift($stack);
  60. return $stack;
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function stopQuery() {
  66. if ($this->activated && $this->activeQuery) {
  67. $this->activeQuery->end(microtime(true));
  68. $this->queries[] = $this->activeQuery;
  69. $this->activeQuery = null;
  70. }
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function getQueries() {
  76. return $this->queries->getData();
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function activate() {
  82. $this->activated = true;
  83. }
  84. }