|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Tack Tracker - A library for tracking long-running task progress |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT |
|
7
|
|
|
* @link https://github.com/caseyamcl/tasktracker |
|
8
|
|
|
* @version 2.0 |
|
9
|
|
|
* @package caseyamcl/tasktracker |
|
10
|
|
|
* @author Casey McLaughlin <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* ------------------------------------------------------------------ |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace TaskTracker\Subscriber; |
|
19
|
|
|
|
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
22
|
|
|
use TaskTracker\Events; |
|
23
|
|
|
use TaskTracker\Helper\BytesToHumanTrait; |
|
24
|
|
|
use TaskTracker\Helper\TimeFormatterTrait; |
|
25
|
|
|
use TaskTracker\Tick; |
|
26
|
|
|
use TaskTracker\Tracker; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Symfony Console Log Task Tracker Subscriber |
|
30
|
|
|
* |
|
31
|
|
|
* @author Casey McLaughlin <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class SymfonyConsoleLog implements EventSubscriberInterface |
|
34
|
|
|
{ |
|
35
|
|
|
// Use traits to aid in output |
|
36
|
|
|
use BytesToHumanTrait; |
|
37
|
|
|
use TimeFormatterTrait; |
|
38
|
|
|
|
|
39
|
|
|
// -------------------------------------------------------------- |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var OutputInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $output; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $linePrefixMap = [ |
|
50
|
|
|
Tick::SKIP => 'SKIP»', |
|
51
|
|
|
TICK::SUCCESS => 'SUCC»', |
|
52
|
|
|
TICK::FAIL => 'FAIL»' |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
// -------------------------------------------------------------- |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Constructor |
|
59
|
|
|
* |
|
60
|
|
|
* @param OutputInterface $output |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(OutputInterface $output) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->output = $output; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// -------------------------------------------------------------- |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getSubscribedEvents() |
|
73
|
|
|
{ |
|
74
|
|
|
return [ |
|
75
|
|
|
Events::TRACKER_START => 'writeStartLine', |
|
76
|
|
|
Events::TRACKER_TICK => 'writeLogLine', |
|
77
|
|
|
Events::TRACKER_FINISH => 'writeFinishLine', |
|
78
|
|
|
Events::TRACKER_ABORT => 'writeAbortLine' |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
// --------------------------------------------------------------- |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set line prefix map |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $prefixMap [status => 'prefix'] |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setLinePrefixMap(array $prefixMap) |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($prefixMap as $status => $prefix) { |
|
93
|
|
|
$this->setLinePrefix($status, $prefix); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// --------------------------------------------------------------- |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set prefix for a given status |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $status Tick::SUCCESS, Tick::FAIL, or Tick::SUCCESS |
|
103
|
|
|
* @param string $prefix |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setLinePrefix($status, $prefix) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->linePrefixMap[$status] = $prefix; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// --------------------------------------------------------------- |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Write Log Line |
|
114
|
|
|
* |
|
115
|
|
|
* @param Tick $tick |
|
116
|
|
|
*/ |
|
117
|
|
|
public function writeLogLine(Tick $tick) |
|
118
|
|
|
{ |
|
119
|
|
|
// Line segments |
|
120
|
|
|
$lineSegs = array(); |
|
121
|
|
|
|
|
122
|
|
|
// 1st Segment is a star |
|
123
|
|
|
switch ($tick->getStatus()) { |
|
124
|
|
|
case Tick::SUCCESS: |
|
125
|
|
|
$lineSegs[] = sprintf("<fg=green>%s</fg=green>", $this->linePrefixMap[Tick::SUCCESS]); |
|
126
|
|
|
break; |
|
127
|
|
|
case Tick::FAIL: |
|
128
|
|
|
$lineSegs[] = sprintf("<fg=red>%s</fg=red>", $this->linePrefixMap[Tick::FAIL]); |
|
129
|
|
|
break; |
|
130
|
|
|
case Tick::SKIP: |
|
131
|
|
|
default: |
|
132
|
|
|
$lineSegs[] = $this->linePrefixMap[Tick::SKIP]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Item Progress |
|
136
|
|
|
$lineSegs[] = sprintf( |
|
137
|
|
|
"[%s%s]", |
|
138
|
|
|
$tick->getReport()->getNumItemsProcessed(), |
|
139
|
|
|
$tick->getReport()->getTotalItemCount() != Tracker::UNKNOWN ? "/" . $tick->getReport()->getTotalItemCount() : '' |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
// If verbose, add walltime and item counts |
|
143
|
|
|
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
|
144
|
|
|
$lineSegs[] = $this->formatSeconds($tick->getReport()->getTimeElapsed()); |
|
145
|
|
|
|
|
146
|
|
|
$lineSegs[] = sprintf( |
|
147
|
|
|
'(<fg=green>%s</fg=green>/%s/<fg=red>%s</fg=red>)', |
|
148
|
|
|
$tick->getReport()->getNumItemsSuccess(), |
|
149
|
|
|
$tick->getReport()->getNumItemsSkip(), |
|
150
|
|
|
$tick->getReport()->getNumItemsFail() |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// If very verbose, add memory usage |
|
155
|
|
|
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { |
|
156
|
|
|
$lineSegs[] = sprintf("{%s/%s}", |
|
157
|
|
|
$this->bytesToHuman($tick->getReport()->getMemUsage()), |
|
158
|
|
|
$this->bytesToHuman($tick->getReport()->getMemPeakUsage()) |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Add message |
|
163
|
|
|
$lineSegs[] = $tick->getMessage() ?: sprintf( |
|
164
|
|
|
"Processing item %s", |
|
165
|
|
|
number_format($tick->getReport()->getNumItemsProcessed(), 0) |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
// Output it! |
|
169
|
|
|
$this->output->writeln(implode(' ', $lineSegs)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// --------------------------------------------------------------- |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param Tick $tick |
|
176
|
|
|
*/ |
|
177
|
|
|
public function writeStartLine(Tick $tick) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->output->writeln($tick->getMessage() ?: "Starting . . ."); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param Tick $tick |
|
184
|
|
|
*/ |
|
185
|
|
|
public function writeFinishLine(Tick $tick) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->output->writeln($tick->getMessage() ?: ". . . Finished"); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param Tick $tick |
|
192
|
|
|
*/ |
|
193
|
|
|
public function writeAbortLine(Tick $tick) |
|
194
|
|
|
{ |
|
195
|
|
|
$this->output->writeln('<error>' . ($tick->getMessage() ?: 'Aborted!') . '</error>'); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|