<?php

/**
 * Saves requests and performance information to log file
 *
 * @version DR1.7
 */
class LoggingFilter implements InterceptingFilter {



	private $request;
	private $utime;



	public function preProcess(RequestContext $request, ResponseContext $response) {
		$this->request = $request;
		$this->utime = microtime(true);
	}



	/**
	 * Saves information to log file
	 *
	 * Saves log info to file var/log/requests.log. File format is:
	 * Client IP address, execution time, action, [ arguments ]
	 */
	public function postProcess(RequestContext $request, ResponseContext $response) {
		$utime = microtime(true);
		$entry = $_SERVER['REMOTE_ADDR'] . "\t";
		$entry .= ($utime - $this->utime) . "\t";
		$entry .= $this->request->getAction();
		if ($args = $this->request->getArgs()) {
			$out = array();
			foreach ($args as $arg) {
				if ($arg instanceof FormContext) {
					$out[] = '{ ' . $arg->__toString() . ' }';
				} else {
					$out[] = $arg;
				}
			}
			$entry .= ' [ ' . implode(' ', $out) . ' ]';
		}
		$entry .= "\n";
		file_put_contents('var/log/requests.log', $entry, FILE_APPEND);
	}



} // class LoggingFilter

?>