Add an EventLogger interface to allow apps to get a log of the request timeline

This commit is contained in:
Robin Appelman 2014-10-03 01:16:57 +02:00
parent cb3a4d22b1
commit d38050cf52
7 changed files with 262 additions and 6 deletions

View File

@ -0,0 +1,40 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* 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();
}
}

View File

@ -0,0 +1,86 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* 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;
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* 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;
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* 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();
}

View File

@ -0,0 +1,31 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* 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();
}

View File

@ -248,4 +248,11 @@ interface IServerContainer {
* @return \OC\HTTPHelper
*/
function getHTTPHelper();
/**
* Get the active event logger
*
* @return \OCP\Debug\IEventLogger
*/
function getEventLogger();
}