Skip to content

Commit 8edb7cf

Browse files
committed
add finally
1 parent abfb735 commit 8edb7cf

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

src/ErrorHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ trait ErrorHandler
99
{
1010
private $catchCallable = null;
1111

12+
protected $finallyCallback = null;
13+
1214
private ?string $_filename = null;
1315

1416
/**
@@ -28,6 +30,8 @@ protected function setFilename(string $filename): void
2830

2931
protected function exception(Throwable $exception): self
3032
{
33+
$this->finished($this->_filename, $exception->getMessage());
34+
3135
if ($this->catchCallable) {
3236
call_user_func($this->catchCallable, $this->convertException($exception));
3337
} else {
@@ -37,6 +41,15 @@ protected function exception(Throwable $exception): self
3741
return $this;
3842
}
3943

44+
protected function finished(string $filenameSource, string $message): void
45+
{
46+
if (is_null($this->finallyCallback)) {
47+
return;
48+
}
49+
50+
call_user_func($this->finallyCallback, $message, $filenameSource);
51+
}
52+
4053
private function convertException(Throwable $exception): RotationFailed
4154
{
4255
return new RotationFailed(

src/Rotation.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct(array $options = [])
3232
'files',
3333
'then',
3434
'catch',
35+
'finally',
3536
]);
3637

3738
$this->options($options);
@@ -99,6 +100,16 @@ public function then(callable $callable): self
99100
return $this;
100101
}
101102

103+
/**
104+
* Function that will be executed when the process was finished.
105+
*/
106+
public function finally(callable $callable): self
107+
{
108+
$this->finallyCallback = $callable;
109+
110+
return $this;
111+
}
112+
102113
/**
103114
* Rotate file.
104115
*
@@ -174,6 +185,8 @@ private function sucessfull(string $filenameSource, ?string $filenameRotated): v
174185
}
175186

176187
call_user_func($this->thenCallback, $filenameRotated, $filenameSource);
188+
189+
$this->finished($filenameRotated, 'sucessfull');
177190
}
178191

179192
/**
@@ -182,6 +195,7 @@ private function sucessfull(string $filenameSource, ?string $filenameRotated): v
182195
private function canRotate(string $filename): bool
183196
{
184197
if (!file_exists($filename)) {
198+
$this->finished($filename, sprintf('the file %s not exists.', $filename));
185199
return false;
186200
}
187201

tests/OptionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function testPassOptions()
1515
'truncate' => false,
1616
'then' => function ($filename) {},
1717
'catch' => function ($error) {},
18+
'finally' => function ($message) {},
1819
]);
1920

2021
$this->assertNotNull($rotation);

tests/test_load/run.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use Cesargb\Log\Rotation;
4+
5+
function app_path(string $path = '/'): string
6+
{
7+
return dirname(__FILE__).$path;
8+
}
9+
10+
function clean()
11+
{
12+
$files = glob(app_path('/file.log*'));
13+
14+
foreach ($files as $file) {
15+
if (is_file($file)) {
16+
unlink($file);
17+
}
18+
}
19+
}
20+
21+
function writeLogInBackground(): string
22+
{
23+
$pid = exec('php '.app_path('/writelog.php').' > /dev/null 2>&1 & echo $!');
24+
25+
sleep(1);
26+
27+
return $pid;
28+
}
29+
30+
include app_path('/../../vendor/autoload.php');
31+
32+
clean();
33+
34+
$pid = writeLogInBackground();
35+
posix_kill($pid, SIGKILL);
36+
37+
$pid = writeLogInBackground();
38+
39+
$rotation = new Rotation();
40+
41+
if (!$rotation->compress()->rotate(app_path('/file.log'))) {
42+
echo "Failed rotation\n";
43+
}
44+
45+
sleep(1);
46+
47+
posix_kill($pid, SIGKILL);
48+
49+
$lastSave = exec('tail -n 1 '.app_path('/file.log.1'));
50+
$firstNew = exec('head -n 1 '.app_path('/file.log'));
51+
52+
clean();
53+
54+
$dataLost = $firstNew - ($lastSave + 1);
55+
56+
if ($dataLost === 0) {
57+
echo "OK, no data lossed\n";
58+
59+
exit(0);
60+
} else {
61+
echo "Warning, $dataLost data lossed\n";
62+
exit($dataLost);
63+
}

tests/test_load/writelog.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
function app_path(string $path = '/'): string
4+
{
5+
return dirname(__FILE__).$path;
6+
}
7+
8+
$n = 0;
9+
10+
while (true) {
11+
++$n;
12+
file_put_contents(app_path('/file.log'), $n."\n", FILE_APPEND);
13+
}

0 commit comments

Comments
 (0)