Skip to content

Commit

Permalink
Refactor get max_lines logic
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Nov 16, 2022
1 parent 830e78a commit 2e6811e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/tribler/core/components/restapi/rest/debug_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import psutil
from aiohttp import web
from aiohttp.abc import BaseRequest
from aiohttp_apispec import docs
from ipv8.REST.schema import schema
from ipv8.messaging.anonymization.community import TunnelCommunity
Expand All @@ -18,6 +19,8 @@
from tribler.core.utilities.osutils import get_root_state_directory
from tribler.core.utilities.path_util import Path, tail

DEFAULT_MAX_LINES = 100

HAS_MELIAE = True
try:
from meliae import scanner
Expand Down Expand Up @@ -255,8 +258,9 @@ async def get_memory_dump(self, request):
}
}
)
async def get_log(self, request):
async def get_log(self, request: BaseRequest):
# First, flush all the logs to make sure it is written to file
self.logger.info(f'Get log: {request.query}')
for handler in logging.getLogger().handlers:
handler.flush()

Expand All @@ -279,9 +283,10 @@ async def get_log(self, request):
# If the log file exists and return last requested 'max_lines' of log
try:
max_lines = int(request.query['max_lines'])
except ValueError as e:
self.logger.exception(e)
max_lines = 100
except ValueError:
max_lines = DEFAULT_MAX_LINES
self.logger.warning(f'Wrong max_lines value. Use the default {max_lines}.')
max_lines = max(1, max_lines)

response['content'] = tail(log_file_name, max_lines)
response['max_lines'] = max_lines
Expand Down
3 changes: 1 addition & 2 deletions src/tribler/core/utilities/path_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class WindowsPath(Path, pathlib.PureWindowsPath):


def tail(file_name: Union[str, Path], count: int = 1) -> str:
"""Tail a file and get X lines from the end"""

"""Tail a file and get `count` lines from the end"""
with FileReadBackwards(file_name) as f:
lines = list(islice(f, count))
return '\n'.join(reversed(lines))
12 changes: 5 additions & 7 deletions src/tribler/gui/debug_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
from time import localtime, strftime, time
from typing import Dict

import libtorrent
import psutil
from PyQt5 import QtGui, uic
from PyQt5.QtCore import QTimer, Qt, pyqtSignal
from PyQt5.QtGui import QBrush, QColor, QTextCursor
from PyQt5.QtWidgets import QDesktopWidget, QFileDialog, QHeaderView, QMainWindow, QMessageBox, QTreeWidgetItem

import libtorrent

import psutil

from tribler.core.utilities.utilities import has_bep33_support
from tribler.gui.defs import DEBUG_PANE_REFRESH_TIMEOUT, GB, MB
from tribler.gui.dialogs.confirmationdialog import ConfirmationDialog
Expand Down Expand Up @@ -847,8 +845,8 @@ def load_logs_tab(self):
tab_index = self.window().log_tab_widget.currentIndex()
tab_name = "core" if tab_index == 0 else "gui"

request_query = f"process={tab_name}&max_lines={max_log_lines}"
TriblerNetworkRequest(f"debug/log?{request_query}", self.display_logs)
params = {'process': tab_name, 'max_lines': max_log_lines}
TriblerNetworkRequest(f"debug/log", url_params=params, reply_callback=self.display_logs)

def display_logs(self, data):
if not data:
Expand Down Expand Up @@ -955,7 +953,7 @@ def on_channels_peers(self, data):
channel_item.setText(0, str(c["channel_name"]))
channel_item.setText(1, str(c["channel_pk"]))
channel_item.setText(2, str(c["channel_id"]))
channel_item.setData(3, Qt.DisplayRole, len(c["peers"])) # Peers count
channel_item.setData(3, Qt.DisplayRole, len(c["peers"])) # Peers count
for p in c["peers"]:
peer_item = QTreeWidgetItem()
peer_item.setText(1, str(p[0])) # Peer mid
Expand Down

0 comments on commit 2e6811e

Please sign in to comment.