Skip to content

Commit 9d9aeca

Browse files
committed
update: add debugf for print logs
1 parent 81a36bf commit 9d9aeca

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

src/AbstractApplication.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,19 @@ public function logf(int $level, string $format, ...$args): void
429429
Console::logf($level, $format, ...$args);
430430
}
431431

432+
/**
433+
* @param string $format
434+
* @param mixed ...$args
435+
*/
436+
public function debugf(string $format, ...$args): void
437+
{
438+
if ($this->getVerbLevel() < Console::VERB_DEBUG) {
439+
return;
440+
}
441+
442+
Console::logf(Console::VERB_DEBUG, $format, ...$args);
443+
}
444+
432445
/**********************************************************
433446
* getter/setter methods
434447
**********************************************************/

src/Application.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function command(string $name, $handler = null, $option = null)
139139
];
140140
}
141141

142+
$this->logf(Console::VERB_CRAZY, 'load application command: %s', $name);
142143
$this->router->addCommand($name, $handler, (array)$option);
143144

144145
return $this;
@@ -265,7 +266,7 @@ public function dispatch(string $name, bool $detachedRun = false)
265266
}
266267

267268
$cmdId = $name;
268-
$this->logf(Console::VERB_DEBUG, 'begin dispatch the input command: %s', $name);
269+
$this->debugf( 'begin dispatch the input command: %s', $name);
269270

270271
// format is: `group action`
271272
if (strpos($name, ' ') > 0) {
@@ -277,8 +278,9 @@ public function dispatch(string $name, bool $detachedRun = false)
277278

278279
// command not found
279280
if (!$info) {
280-
if (true === $this->fire(ConsoleEvent::ON_NOT_FOUND, $cmdId, $this)) {
281-
$this->logf(Console::VERB_DEBUG, 'not found handle by user, command: %s', $name);
281+
$evtName = ConsoleEvent::ON_NOT_FOUND;
282+
if (true === $this->fire($evtName, $cmdId, $this)) {
283+
$this->debugf('user custom handle the not found command: %s, event: %s', $name, $evtName);
282284
return 0;
283285
}
284286

src/Concern/AttachApplicationTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ public function getVerbLevel(): int
101101
return (int)$this->input->getLongOpt('debug', Console::VERB_ERROR);
102102
}
103103

104+
/**
105+
* @param string $format
106+
* @param mixed ...$args
107+
*/
108+
public function debugf(string $format, ...$args): void
109+
{
110+
if ($this->getVerbLevel() < Console::VERB_DEBUG) {
111+
return;
112+
}
113+
114+
Console::logf(Console::VERB_DEBUG, $format, ...$args);
115+
}
116+
104117
/**
105118
* @param int $level
106119
* @param string $format

src/Controller.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,15 @@ public function run(string $command = '')
217217

218218
// if not input sub-command, render group help.
219219
if (!$command) {
220-
$this->logf(Console::VERB_DEBUG, 'sub-command is empty, display help for the group: %s', self::getName());
220+
$this->debugf('sub-command is empty, display help for the group: %s', self::getName());
221221
return $this->showHelp();
222222
}
223223

224224
$command = $this->getRealCommandName($command);
225225

226226
// convert 'boo-foo' to 'booFoo'
227227
$this->action = Str::camelCase($command);
228-
$this->logf(Console::VERB_DEBUG, 'will run the group action: %s, sub-command: %s', $this->action, $command);
228+
$this->debugf('will run the group action: %s, sub-command: %s', $this->action, $command);
229229

230230
// do running
231231
return parent::run($command);
@@ -277,7 +277,7 @@ final public function execute($input, $output)
277277
$group = static::getName();
278278

279279
if ($this->isDisabled($action)) {
280-
$this->logf(Console::VERB_DEBUG, 'command %s is disabled on the group %s', $action, $group);
280+
$this->debugf('command %s is disabled on the group %s', $action, $group);
281281
$output->error(sprintf("Sorry, The command '%s' is invalid in the group '%s'!", $action, $group));
282282
return -1;
283283
}
@@ -291,7 +291,7 @@ final public function execute($input, $output)
291291
if (method_exists($this, $beforeFunc = 'before' . ucfirst($action))) {
292292
$beforeOk = $this->$beforeFunc($input, $output);
293293
if ($beforeOk === false) {
294-
$this->logf(Console::VERB_DEBUG, '%s() returns FALSE, interrupt processing continues', $beforeFunc);
294+
$this->debugf('%s() returns FALSE, interrupt processing continues', $beforeFunc);
295295
return 0;
296296
}
297297
}
@@ -309,11 +309,11 @@ final public function execute($input, $output)
309309

310310
// if user custom handle not found logic.
311311
if ($this->onNotFound($action)) {
312-
$this->logf(Console::VERB_DEBUG, 'user custom handle the action:%s not found logic', $action);
312+
$this->debugf('user custom handle the action:%s not found logic', $action);
313313
return 0;
314314
}
315315

316-
$this->logf(Console::VERB_DEBUG, 'action:%s not found on the group controller', $action);
316+
$this->debugf('action:%s not found on the group controller', $action);
317317

318318
// if you defined the method '$this->notFoundCallback' , will call it
319319
// if (($notFoundCallback = $this->notFoundCallback) && method_exists($this, $notFoundCallback)) {

0 commit comments

Comments
 (0)