diff --git a/lib/private/debug/dummyeventlogger.php b/lib/private/debug/dummyeventlogger.php new file mode 100644 index 0000000000..7aa4c21b67 --- /dev/null +++ b/lib/private/debug/dummyeventlogger.php @@ -0,0 +1,40 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Debug; + +use OCP\Debug\IEventLogger; + +/** + * Dummy event logger that doesn't actually log anything + */ +class DummyEventLogger implements IEventLogger { + /** + * Mark the start of an event + * + * @param $id + * @param $description + */ + public function start($id, $description) { + } + + /** + * Mark the end of an event + * + * @param $id + */ + public function end($id) { + } + + /** + * @return \OCP\Debug\IEvent[] + */ + public function getEvents(){ + return array(); + } +} diff --git a/lib/private/debug/event.php b/lib/private/debug/event.php new file mode 100644 index 0000000000..b03fdeabc1 --- /dev/null +++ b/lib/private/debug/event.php @@ -0,0 +1,86 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Debug; + +use OCP\Debug\IEvent; + +class Event implements IEvent { + /** + * @var string + */ + protected $id; + + /** + * @var float + */ + protected $start; + + /** + * @var float + */ + protected $end; + + /** + * @var string + */ + protected $description; + + /** + * @param string $id + * @param string $description + * @param float $start + */ + public function __construct($id, $description, $start) { + $this->id = $id; + $this->description = $description; + $this->start = $start; + } + + /** + * @param float $time + */ + public function end($time) { + $this->end = $time; + } + + /** + * @return float + */ + public function getStart() { + return $this->start; + } + + /** + * @return string + */ + public function getId() { + return $this->id; + } + + /** + * @return string + */ + public function getDescription() { + return $this->description; + } + + /** + * @return float + */ + public function getEnd() { + return $this->end; + } + + /** + * @return float + */ + public function getDuration() { + return $this->end - $this->start; + } +} diff --git a/lib/private/debug/eventlogger.php b/lib/private/debug/eventlogger.php new file mode 100644 index 0000000000..2127a624ed --- /dev/null +++ b/lib/private/debug/eventlogger.php @@ -0,0 +1,36 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Debug; + +use OCP\Debug\IEventLogger; + +class EventLogger implements IEventLogger { + /** + * @var \OC\Debug\Event[] + */ + private $events = array(); + + public function start($id, $description) { + $this->events[$id] = new Event($id, $description, microtime(true)); + } + + public function end($id) { + if (isset($this->events[$id])) { + $timing = $this->events[$id]; + $timing->end(microtime(true)); + } + } + + /** + * @return \OCP\Debug\IEvent[] + */ + public function getEvents() { + return $this->events; + } +} diff --git a/lib/private/server.php b/lib/private/server.php index d2728d2b6e..263f991902 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -6,12 +6,14 @@ use OC\AppFramework\Http\Request; use OC\AppFramework\Db\Db; use OC\AppFramework\Utility\SimpleContainer; use OC\Cache\UserCache; +use OC\Debug\EventLogger; use OC\Security\CertificateManager; use OC\DB\ConnectionWrapper; use OC\Files\Node\Root; use OC\Files\View; use OC\Security\Crypto; use OC\Security\SecureRandom; +use OC\Debug\DummyEventLogger; use OCP\IServerContainer; use OCP\ISession; use OC\Tagging\TagMapper; @@ -24,7 +26,6 @@ use OC\Tagging\TagMapper; * TODO: hookup all manager classes */ class Server extends SimpleContainer implements IServerContainer { - function __construct() { $this->registerService('ContactsManager', function ($c) { return new ContactsManager(); @@ -59,8 +60,8 @@ class Server extends SimpleContainer implements IServerContainer { 'env' => $_ENV, 'cookies' => $_COOKIE, 'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD'])) - ? $_SERVER['REQUEST_METHOD'] - : null, + ? $_SERVER['REQUEST_METHOD'] + : null, 'urlParams' => $urlParams, 'requesttoken' => $requestToken, ), $stream @@ -208,10 +209,10 @@ class Server extends SimpleContainer implements IServerContainer { $this->registerService('Search', function ($c) { return new Search(); }); - $this->registerService('SecureRandom', function($c) { + $this->registerService('SecureRandom', function ($c) { return new SecureRandom(); }); - $this->registerService('Crypto', function($c) { + $this->registerService('Crypto', function ($c) { return new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom()); }); $this->registerService('Db', function ($c) { @@ -221,6 +222,13 @@ class Server extends SimpleContainer implements IServerContainer { $config = $c->query('AllConfig'); return new HTTPHelper($config); }); + $this->registerService('EventLogger', function ($c) { + if (defined('DEBUG') and DEBUG) { + return new EventLogger(); + } else { + return new DummyEventLogger(); + } + }); } /** @@ -285,7 +293,7 @@ class Server extends SimpleContainer implements IServerContainer { * @return \OCP\Files\Folder */ function getUserFolder($userId = null) { - if($userId === null) { + if ($userId === null) { $user = $this->getUserSession()->getUser(); if (!$user) { return null; @@ -528,6 +536,7 @@ class Server extends SimpleContainer implements IServerContainer { /** * Returns an instance of the HTTP helper class + * * @return \OC\HTTPHelper */ function getHTTPHelper() { @@ -559,4 +568,15 @@ class Server extends SimpleContainer implements IServerContainer { function createEventSource() { return new \OC_EventSource(); } + + /** + * Get the active event logger + * + * The returned logger only logs data when debug mode is enabled + * + * @return \OCP\Debug\IEventLogger + */ + function getEventLogger() { + return $this->query('EventLogger'); + } } diff --git a/lib/public/debug/ievent.php b/lib/public/debug/ievent.php new file mode 100644 index 0000000000..1cebb274e5 --- /dev/null +++ b/lib/public/debug/ievent.php @@ -0,0 +1,36 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\Debug; + +interface IEvent { + /** + * @return string + */ + public function getId(); + + /** + * @return string + */ + public function getDescription(); + + /** + * @return float + */ + public function getStart(); + + /** + * @return float + */ + public function getEnd(); + + /** + * @return float + */ + public function getDuration(); +} diff --git a/lib/public/debug/ieventlogger.php b/lib/public/debug/ieventlogger.php new file mode 100644 index 0000000000..7a7bff521d --- /dev/null +++ b/lib/public/debug/ieventlogger.php @@ -0,0 +1,31 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\Debug; + +interface IEventLogger { + /** + * Mark the start of an event + * + * @param string $id + * @param string $description + */ + public function start($id, $description); + + /** + * Mark the end of an event + * + * @param string $id + */ + public function end($id); + + /** + * @return \OCP\Debug\IEvent[] + */ + public function getEvents(); +} diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index a093ff3a64..57bbf62899 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -248,4 +248,11 @@ interface IServerContainer { * @return \OC\HTTPHelper */ function getHTTPHelper(); + + /** + * Get the active event logger + * + * @return \OCP\Debug\IEventLogger + */ + function getEventLogger(); }