Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate the center of log files #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/config/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
'pre' => [
'\\Filter\\Pre\\Trim',
'\\Filter\\Pre\\Length',
'\\Filter\\Pre\\Lines',
'\\Filter\\Pre\\Length',
'\\Filter\\Pre\\Ip',
'\\Filter\\Pre\\Username',
'\\Filter\\Pre\\AccessToken'
Expand Down
20 changes: 19 additions & 1 deletion core/src/Filter/Pre/Lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ class Lines implements PreFilterInterface {
public static function Filter(string $data): string
{
$config = \Config::Get('storage');
return implode("\n", array_slice(explode("\n", $data), 0, $config["maxLines"]));
$limit = $config["maxLines"];

$lines = explode("\n", $data);
$count = count($lines);

if ($count <= $limit) {
return $data;
}

$removed = $count - $limit + 3;
$message = "Truncated " . $removed . " line" . ($removed > 1 ? "s" : "");

array_splice($lines, $limit / 2, $removed, [
str_repeat("=", strlen($message)),
$message,
str_repeat("=", strlen($message)),
]);

return implode("\n", $lines);
}
}
40 changes: 37 additions & 3 deletions web/public/js/mclogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ document.addEventListener('keydown', event => {
return true;
})

/**
* Limit the number of characters in a string by removing the end
* @param {string} string
* @param {number} limit
* @returns {string}
*/
function limitLength(string, limit = parseInt(pasteArea.dataset.maxLength)) {
return string.substring(0, limit);
}

/**
* Limit the number of lines in a string by truncating the middle
* @param {string} string
* @param {number} limit
* @returns {string}
*/
function limitLines(string, limit = parseInt(pasteArea.dataset.maxLines)) {
let lines = string.split('\n');
if (lines.length <= limit) {
return string;
}

let removed = lines.length - limit + 3;
let message = 'Truncated ' + removed + ' line' + (removed > 1 ? 's' : '');

lines.splice(limit / 2, removed,
"=".repeat(message.length),
message,
"=".repeat(message.length)
);

return lines.join('\n')
}

/**
* Save the log to the API
* @returns {Promise<void>}
Expand All @@ -58,9 +92,9 @@ async function sendLog() {
pasteSaveButtons.forEach(button => button.classList.add("btn-working"));

try {
let log = pasteArea.value
.substring(0, parseInt(pasteArea.dataset.maxLength))
.split('\n').slice(0, parseInt(pasteArea.dataset.maxLines)).join('\n');
let log = pasteArea.value;
log = limitLines(log);
log = limitLength(log);

const response = await fetch(`${location.protocol}//api.${location.host}/1/log`, {
method: "POST",
Expand Down