Skip to content

Commit

Permalink
added logging levels.
Browse files Browse the repository at this point in the history
  • Loading branch information
ofbeaton committed Jul 26, 2015
1 parent 7ef4eb8 commit 4aea140
Showing 1 changed file with 65 additions and 10 deletions.
75 changes: 65 additions & 10 deletions src/Ami.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ class Ami
*/
const DEFAULT_PHPAGI_CONFIG = '/etc/asterisk/phpagi.conf';

/**
* Log level.
*/
const LOG_FATAL = 0;

/**
* Log level.
*/
const LOG_ERROR = 1;

/**
* Log level.
*/
const LOG_WARN = 2;

/**
* Log level.
*/
const LOG_INFO = 3;

/**
* Log level.
*/
const LOG_DEBUG = 4;

/**
* Log level.
*/
const LOG_TRACE = 5;

/**
* Config variables
*
Expand Down Expand Up @@ -77,6 +107,11 @@ class Ami
*/
private $loggedIn = false;

/**
* @var int Log level.
*/
private $logLevel = self::LOG_ERROR;


/**
* Constructor
Expand Down Expand Up @@ -198,7 +233,7 @@ public function waitResponse($allowTimeout = false)
break;

default:
$this->log('Unhandled response packet from Manager: ' . print_r($parameters, true));
$this->log('Unhandled response packet from Manager: '.print_r($parameters, true), self::LOG_ERROR);
break;
}
} while ($type !== 'response' && $timeout === false);
Expand Down Expand Up @@ -248,7 +283,10 @@ public function connect($server = null, $username = null, $secret = null)
// TODO: Convert this to a custom error handler to silence the error instead.
$this->socket = @fsockopen($this->server, $this->port, $errno, $errstr);
if ($this->socket === false) {
$this->log('Unable to connect to manager '.$this->server.':'.$this->port.' ('.$errno.'): '.$errstr);
$this->log(
'Unable to connect to manager '.$this->server.':'.$this->port.' ('.$errno.'): '.$errstr,
self::LOG_FATAL
);
return false;
}

Expand All @@ -257,15 +295,15 @@ public function connect($server = null, $username = null, $secret = null)
// note: else: don't $this->log($str) until someone looks to see why it mangles the logging
if ($str === false) {
// a problem.
$this->log('Asterisk Manager header not received.');
$this->log('Asterisk Manager header not received.', self::LOG_FATAL);
return false;
}

// login
$res = $this->sendRequest('login', ['Username' => $username, 'Secret' => $secret]);
if ($res['Response'] !== 'Success') {
$this->loggedIn = false;
$this->log('Failed to login.');
$this->log('Failed to login.', self::LOG_FATAL);
$this->disconnect();
return false;
}
Expand Down Expand Up @@ -1001,11 +1039,28 @@ public function zapShowChannels($actionId = null)
*
* @return void
*/
public function log($message, $level = 1)
public function log($message, $level = self::LOG_INFO)
{
error_log(date('r') . ' - ' . $message);
if ($level <= $this->logLevel) {
error_log(date('r') . ' - ' . $message);
}
}//end log()

/**
* @param integer $level Log Level to use.
*
* @return void
* @throws \InvalidArgumentException Invalid Log level.
*/
public function setLogLevel($level)
{
if ($level < self::LOG_FATAL || $level > self::LOG_TRACE) {
throw new \InvalidArgumentException('Invalid Log Level');
}

$this->logLevel = $level;
}//end setLogLevel()


/**
* Add event handler
Expand Down Expand Up @@ -1052,7 +1107,7 @@ public function addEventHandler($event, $callback)
{
$event = strtolower($event);
if (isset($this->eventHandlers[$event]) === true) {
$this->log($event.' handler is already defined, not over-writing.');
$this->log($event.' handler is already defined, not over-writing.', self::LOG_ERROR);
return false;
}

Expand All @@ -1072,7 +1127,7 @@ public function processEvent(array $parameters)
{
$ret = false;
$e = strtolower($parameters['Event']);
$this->log('Got event: '.$e);
$this->log('Got event: '.$e, self::LOG_INFO);

$handler = '';
if (isset($this->eventHandlers[$e]) === true) {
Expand All @@ -1082,10 +1137,10 @@ public function processEvent(array $parameters)
}

if (function_exists($handler) === true) {
$this->log('Execute handler: '.$handler);
$this->log('Execute handler: '.$handler, self::LOG_DEBUG);
$ret = $handler($e, $parameters, $this->server, $this->port);
} else {
$this->log('No event handler for event: '.$e);
$this->log('No event handler for event: '.$e, self::LOG_DEBUG);
}

return $ret;
Expand Down

0 comments on commit 4aea140

Please sign in to comment.