Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
08582e2
Improved logging, enumerations and more
kleisauke Oct 20, 2016
593c24a
Merge pull request #15 from kleisauke/logging-enumerations
jcupitt Oct 22, 2016
ef0d885
pass PSR2, README updates
jcupitt Oct 22, 2016
a1e2e6e
remove Enum from enum names, version bump
jcupitt Oct 22, 2016
d1727aa
Override __toString() + remove enums import
kleisauke Oct 22, 2016
cc2a01d
add notes on scope to the README
jcupitt Oct 25, 2016
22d2277
add formatted docs to repro
jcupitt Oct 25, 2016
d582081
make tests run in less RAM
jcupitt Oct 25, 2016
02d3c39
better write to file exception test
jcupitt Oct 26, 2016
d91b499
add sig.php example
jcupitt Oct 26, 2016
ff4e79f
move logger
jcupitt Oct 26, 2016
95888ac
all seems to work
jcupitt Oct 26, 2016
e9bee8f
fix sig.php
jcupitt Oct 26, 2016
efd538f
fix some phpcs warnings
jcupitt Oct 26, 2016
706072b
make a separate Logger class
jcupitt Oct 27, 2016
1b44116
move debug/error logging convenience into Main
jcupitt Oct 27, 2016
d0efc5f
fix Logger image->string conversion
jcupitt Oct 27, 2016
a1ad5e3
Merge branch 'add-default-logger' of github.com:jcupitt/php-vips into…
jcupitt Oct 27, 2016
cb42841
remove @version tags
jcupitt Oct 27, 2016
eeb8483
update docs
jcupitt Oct 27, 2016
f4d4fb5
avoid AND/OR member names
jcupitt Oct 27, 2016
6e99cdd
regen docs, again
jcupitt Oct 27, 2016
44c805e
incorporate review comments
jcupitt Oct 27, 2016
e6bd0e5
Merge branch 'dev' into add-default-logger
jcupitt Oct 28, 2016
2e5cc1b
ready to merge to dev
jcupitt Oct 28, 2016
c3ac43d
rename error() and debug()
jcupitt Nov 2, 2016
d63c9da
remove :void return
jcupitt Nov 2, 2016
420b9f4
regen docs
jcupitt Nov 2, 2016
e79da93
update docs without cache files
jcupitt Nov 2, 2016
c00f6af
remove left-over debugLog() etc.
jcupitt Nov 2, 2016
2d7fb28
update docs
jcupitt Nov 2, 2016
d80e827
remove formatted docs, bump to 1.0.0
jcupitt Nov 3, 2016
e0c521b
fixes for review comments
jcupitt Nov 4, 2016
7fd21be
revise README for 1.0
jcupitt Nov 4, 2016
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make a separate Logger class
and move the logging interface into Main
  • Loading branch information
jcupitt committed Oct 27, 2016
commit 706072b580b19b1b4e84bc8e2b6d0c3e31fcb093
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ libvips properties as properties of the PHP `Vips\Image` class.

```
$ phpcs --standard=PSR2 src
$ php ~/package/php/composer.phar install
$ php ~/packages/php/composer.phar install
$ vendor/bin/phpunit
$ vendor/bin/phpdoc
```
Expand Down
2 changes: 1 addition & 1 deletion examples/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Jcupitt\Vips;

public static function setLogger(LoggerInterface $logger)
Vips\Main::setLogger(new Vips\Logger);

$image = Vips\Image::newFromFile($argv[1]);

Expand Down
132 changes: 44 additions & 88 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@

use Psr\Log\LoggerInterface;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoggerInterface is no longer used in this class, so this can be removed.


const LOG_FORMAT = "[%datetime%] %level_name%: %message% %context%\n";
const DATE_FORMAT = "Y-m-d\TH:i:sP";

/**
* This class represents a Vips image object.
*
Expand Down Expand Up @@ -416,99 +413,71 @@ class Image extends ImageAutodoc implements \ArrayAccess
{

/**
* The logger instance.
* The resource for the underlying VipsImage.
*
* @var LoggerInterface
* @internal
*/
private static $logger;
private $image;

/**
* A basic logger, handy for debugging.
* Wrap a Image around an underlying vips resource.
*
* @var LoggerInterface
*/
public static $debugLogger = new class implements Psr\Log\LoggerInterface {
// Use the LoggerTrait so that we only have to implement the generic
// log method.
use Psr\Log\LoggerTrait;

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = [])
{
// `Vips\Image` to string convert
array_walk_recursive($context, function (&$value) {
if ($value instanceof Vips\Image) {
$value = (string) $value;
}
});

$strParams = [
'%datetime%' => date(DATE_FORMAT),
'%level_name%' => $level,
'%message%' => $message,
'%context%' => json_encode(
$context,
JSON_UNESCAPED_SLASHES |
JSON_UNESCAPED_UNICODE |
JSON_PRESERVE_ZERO_FRACTION
),
];

echo strtr(LOG_FORMAT, $strParams);
}
});

/**
* Sets a logger.
* Don't call this yourself, users should stick to (for example)
* Image::newFromFile().
*
* @param LoggerInterface $logger
* @param resource $image The underlying vips image resource that this
* class should wrap.
*
* @return void
* @internal
*/
public static function setLogger(LoggerInterface $logger)
public function __construct($image)
{
self::$logger = $logger;
$this->image = $image;
}

/**
* Gets a logger.
* Log a debug message.
*
* @return LoggerInterface $logger|null
*/
public static function getLogger()
{
return self::$logger;
}

/**
* The resource for the underlying VipsImage.
* @param string $name The method creating the messages.
* @param Image|null $instance Optionally, the instance that created the
* message.
* @param array $arguments The method arguments.
*
* @return void
*
* @internal
*/
private $image;
private static function debug(
string $name,
$instance,
array $arguments
): void {
$logger = Main::getLogger();
if ($logger) {
if ($instance) {
$arguments = array_merge(['instance' => $instance], $arguments);
}
$arguments = array_merge(['name' => $name], $arguments);
$logger->debug('Image', $arguments);
}
}

/**
* Wrap a Image around an underlying vips resource.
* Log an error message.
*
* Don't call this yourself, users should stick to (for example)
* Image::newFromFile().
* @param string $message The error message.
* @param \Exception $exception The exception.
*
* @param resource $image The underlying vips image resource that this
* class should wrap.
* @return void
*
* @internal
*/
public function __construct($image)
private static function error(string $message, \Exception $exception): void
{
$this->image = $image;
$logger = Main::getLogger();
if ($logger) {
$logger->error($message, ['exception' => $exception]);
}
}

/**
Expand Down Expand Up @@ -697,9 +666,7 @@ private static function errorVips()
{
$message = vips_error_buffer();
$exception = new Exception($message);
if (self::$logger) {
self::$logger->error($message, ['exception' => $exception]);
}
self::error($message, $exception);
throw $exception;
}

Expand Down Expand Up @@ -958,13 +925,7 @@ public static function callBase(
$instance,
array $arguments
) {
if (self::$logger) {
self::$logger->debug('Called \'callBase\'', [
'name' => $name,
'instance' => $instance,
'arguments' => $arguments
]);
}
self::debug($name, $instance, $arguments);

$arguments = array_merge([$name, $instance], $arguments);

Expand All @@ -978,12 +939,7 @@ public static function callBase(
self::errorIsArray($result);
$result = self::wrapResult($result);

if (self::$logger) {
self::$logger->debug('Result \'callBase\'', [
'name' => $name,
'result' => $result,
]);
}
self::debug($name, $instance, ['result' => $result]);

return $result;
}
Expand Down
105 changes: 105 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* Vips is a php binding for the vips image processing library
*
* PHP version 7
*
* LICENSE:
*
* Copyright (c) 2016 John Cupitt
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @category Images
* @package Jcupitt\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @version GIT:ad44dfdd31056a41cbf217244ce62e7a702e0282
* @link https://github.com/jcupitt/php-vips
*/

namespace Jcupitt\Vips;

use \Psr\Log\LoggerInterface;

const LOG_FORMAT = "[%datetime%] %level_name%: %message% %context%\n";
const DATE_FORMAT = "Y-m-d\TH:i:sP";

/**
* A simple logger, handy for debugging. See Main::setLogger().
*
* @category Images
* @package Jcupitt\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @version Release:0.9.0
* @link https://github.com/jcupitt/php-vips
*/
class Logger implements LoggerInterface
{
// Use the LoggerTrait so that we only have to implement the generic
// log method.
use \Psr\Log\LoggerTrait;

/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = [])
{
// `Vips\Image` to string convert
array_walk_recursive($context, function (&$value) {
if ($value instanceof Vips\Image) {
$value = (string) $value;
}
});

$strParams = [
'%datetime%' => date(DATE_FORMAT),
'%level_name%' => $level,
'%message%' => $message,
'%context%' => json_encode(
$context,
JSON_UNESCAPED_SLASHES |
JSON_UNESCAPED_UNICODE |
JSON_PRESERVE_ZERO_FRACTION
),
];

echo strtr(LOG_FORMAT, $strParams);
}
}

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: expandtab sw=4 ts=4 fdm=marker
* vim<600: expandtab sw=4 ts=4
*/
32 changes: 32 additions & 0 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

namespace Jcupitt\Vips;

use \Psr\Log\LoggerInterface;

/**
* This class contains the top-level libvips control methods.
*
Expand All @@ -52,6 +54,36 @@
*/
class Main
{

/**
* The logger instance.
*
* @var LoggerInterface
*/
private static $logger;

/**
* Sets a logger.
*
* @param LoggerInterface $logger
*
* @return void
*/
public static function setLogger(LoggerInterface $logger)
{
self::$logger = $logger;
}

/**
* Gets a logger.
*
* @return LoggerInterface $logger|null
*/
public static function getLogger()
{
return self::$logger;
}

/**
* Set the maximum number of operations to hold in the libvips operation
* cache.
Expand Down
6 changes: 3 additions & 3 deletions tests/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public function testGetLoggerCall()
{
// Asserts that getLogger without setting it should
// return a null value.
$logger = Vips\Image::getLogger();
$logger = Vips\Main::getLogger();

$this->assertNull($logger);
}

public function testSetLoggerCall()
{
Vips\Image::setLogger(new class implements Psr\Log\LoggerInterface {
Vips\Main::setLogger(new class implements Psr\Log\LoggerInterface {
use Psr\Log\LoggerTrait;

public function log($level, $message, array $context = array())
Expand All @@ -24,7 +24,7 @@ public function log($level, $message, array $context = array())
}
});

$logger = Vips\Image::getLogger();
$logger = Vips\Main::getLogger();

// Asserts that getLogger should return an instance of Psr\Log\LoggerInterface
$this->assertInstanceOf(Psr\Log\LoggerInterface::class, $logger);
Expand Down