StatusValue: Improve logging/debug output with multibyte characters

Use mb_str_split and mb_substr to improve the chances that the fancy
table lines will line up, and to avoid corrupting multibyte characters
at boundaries. Remove max lengths in the sprintf format.

I'm not sure if this is the ideal output format, but I don't have the
heart to replace it.

Change-Id: Iff9ba98a12490938fcfefdeaebd5f1d82cd0ce7b
This commit is contained in:
Bartosz Dziewoński 2023-09-30 04:53:30 +02:00 committed by James D. Forrester
parent a9c67a87d0
commit 12c930aac2
2 changed files with 10 additions and 10 deletions

View file

@ -448,14 +448,14 @@ class StatusValue {
}
$type = $error['type'];
$keyChunks = str_split( $key, 25 );
$paramsChunks = str_split( $this->flattenParams( $params, " | " ), 36 );
$keyChunks = mb_str_split( $key, 25 );
$paramsChunks = mb_str_split( $this->flattenParams( $params, " | " ), 36 );
// array_map(null,...) is like Python's zip()
foreach ( array_map( null, [ $type ], $keyChunks, $paramsChunks )
as [ $typeChunk, $keyChunk, $paramsChunk ]
) {
$out .= sprintf( "| %-8s | %-25.25s | %-36.36s |\n",
$out .= sprintf( "| %-8s | %-25s | %-36s |\n",
$typeChunk,
$keyChunk,
$paramsChunk
@ -485,7 +485,7 @@ class StatusValue {
$r = (string)$p;
}
$ret[] = strlen( $r ) > 100 ? substr( $r, 0, 99 ) . "..." : $r;
$ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) . "..." : $r;
}
return implode( $joiner, $ret );
}

View file

@ -149,18 +149,18 @@ class StatusValueTest extends MediaWikiUnitTestCase {
. "| error | Basic string parsing | Naïve string parsing |\n"
. "| error | Wrapped string | This is a longer input parameter and |\n"
. "| | | thus will wrap |\n"
. "| error | Multi-byte string | Demo: 캐나다∂는 북미에 있 |\n"
. "| | | 는 나라로 면적이 매우 넓 |\n"
. "| | | 습니다. |\n"
. "| error | Multi-byte string | 캐나다∂는 북미에 있는 나라로 면적이 매우 넓습니다. |\n"
. "| error | Multi-byte wrapped string | 캐나다는 태평양에서 대서양까지, 북쪽으로는 북극과 접해 있는 북미 |\n"
. "| | | 의 큰 나라입니다. |\n"
. "+----------+---------------------------+--------------------------------------+\n";
yield [
false, null, [
[ 'message' => 'Basic string parsing', 'params' => 'Naïve string parsing' ],
[ 'message' => 'Wrapped string', 'params' => 'This is a longer input parameter and thus will wrap' ],
// Note: Carefully spaced to avoid breaking multi-byte characters, as it's hard to re-create the bad behaviour.
[ 'message' => 'Multi-byte string', 'params' => 'Demo: 캐나다∂는 북미에 있는 나라로 면적이 매우 넓 습니다.' ]
[ 'message' => 'Multi-byte string', 'params' => '캐나다∂는 북미에 있는 나라로 면적이 매우 넓습니다.' ],
[ 'message' => 'Multi-byte wrapped string', 'params' => '캐나다는 태평양에서 대서양까지, 북쪽으로는 북극과 접해 있는 북미의 큰 나라입니다.' ]
],
'<Error, collected 3 message(s) on the way, no value set>' . $multiErrorReport,
'<Error, collected 4 message(s) on the way, no value set>' . $multiErrorReport,
'Three errors with different kinds of string parameters including long strings that are split when simple'
];
}