Skip to content

Commit

Permalink
NXDRIVE-1231: Improve and move to public Report.export_logs()
Browse files Browse the repository at this point in the history
Also rework CustomMemoryHandler.get_buffer() to properly handle the `size` argument.
  • Loading branch information
Mickaël Schoentgen authored and BoboTiG committed Jul 30, 2018
1 parent 553003b commit ec493f7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
2 changes: 2 additions & 0 deletions docs/technical_changes.md
Expand Up @@ -8,6 +8,7 @@
- Removed `AbstractOSIntegration.is_windows()`
- Removed `Application.get_htmlpage()`
- Removed `Application.get_cache_folder()`
- Removed `CustomMemoryHandler.flush()`
- Added `Engine.init_remote()`
- Changed `Engine(..., remote_doc_client_factory, remote_fs_client_factory, remote_filtered_fs_client_factory` to `Engine(..., remote_cls, filtered_remote_cls, local_cls)`
- Removed `Engine.get_abspath()`
Expand Down Expand Up @@ -105,6 +106,7 @@
- Added gui/settings.py::`SettingsView`
- Added gui/systray.py::`SystrayView`
- Added gui/view.py
- Moved logging_config.py::`MAX_LOG_DISPLAYED` constants.py
- Moved manager.py::`EngineTypeMissing` exception to exceptions.py
- Moved manager.py::`FolderAlreadyUsed` exception to exceptions.py
- Moved manager.py::`MissingToken` to client/proxy.py
Expand Down
1 change: 1 addition & 0 deletions nxdrive/constants.py
Expand Up @@ -11,6 +11,7 @@
TIMEOUT = 20
TX_TIMEOUT = 300
FILE_BUFFER_SIZE = 1024 ** 2
MAX_LOG_DISPLAYED = 50000

DOWNLOAD_TMP_FILE_PREFIX = "."
DOWNLOAD_TMP_FILE_SUFFIX = ".nxpart"
Expand Down
27 changes: 11 additions & 16 deletions nxdrive/logging_config.py
Expand Up @@ -4,11 +4,13 @@
import logging
import os
from logging.handlers import BufferingHandler, TimedRotatingFileHandler
from typing import List
from zipfile import ZIP_DEFLATED, ZipFile

from . import constants
from .options import Options

__all__ = ("MAX_LOG_DISPLAYED", "configure", "get_handler")
__all__ = ("configure", "get_handler")

FILE_HANDLER = None
TRACE = 5
Expand All @@ -28,7 +30,6 @@
_logging_context = dict()

is_logging_configured = False
MAX_LOG_DISPLAYED = 50000


def add_trace_level() -> None:
Expand All @@ -53,27 +54,21 @@ def trace(self, message, *args, **kws):


class CustomMemoryHandler(BufferingHandler):
def __init__(self, capacity: int = MAX_LOG_DISPLAYED) -> None:
def __init__(self, capacity: int = constants.MAX_LOG_DISPLAYED) -> None:
super().__init__(capacity)
self.old_buffer_ = None

def flush(self) -> None:
def get_buffer(self, size: int) -> List[str]:
"""
If `size` is positive, returns the first `size` lines from the memory buffer.
If `size` is negative, returns the last `size` lines from the memory buffer.
By default, `size` is equal to the buffer length, so the entire buffer is returned.
"""
self.acquire()
try:
self.old_buffer_, self.buffer = self.buffer[:], []
return self.buffer[:size] if size > 0 else self.buffer[size:]
finally:
self.release()

def get_buffer(self, size: int) -> None:
self.acquire()
try:
result = self.buffer[:]
if len(result) < size and self.old_buffer_:
result += self.old_buffer_[size - len(result) - 1 :]
finally:
self.release()
return result


class TimedCompressedRotatingFileHandler(TimedRotatingFileHandler):
"""
Expand Down
22 changes: 11 additions & 11 deletions nxdrive/report.py
Expand Up @@ -2,10 +2,11 @@
import os
from datetime import datetime
from logging import getLogger
from typing import Generator
from typing import Iterator
from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile

from .logging_config import MAX_LOG_DISPLAYED, get_handler
from . import constants
from .logging_config import get_handler

__all__ = ("Report",)

Expand Down Expand Up @@ -91,29 +92,28 @@ def get_path(self) -> str:
return self._zipfile

@staticmethod
def _export_logs() -> Generator[str, None, None]:
def export_logs(lines: int = constants.MAX_LOG_DISPLAYED) -> Iterator[bytes]:
"""
Export all lines from the memory logger.
:return bytes: bytes needed by zipfile.writestr()
"""

handler = get_handler(getLogger(), "memory")
log_buffer = handler.get_buffer(MAX_LOG_DISPLAYED)
log_buffer = handler.get_buffer(lines)

for record in log_buffer:
try:
line = handler.format(record)
except:
try:
log.error("Logging record error: %r", record)
yield "Logging record error: {record!r}"
except:
pass
continue

if not isinstance(line, bytes):
line = line.encode(errors="replace")
yield line
else:
if not isinstance(line, bytes):
line = line.encode(errors="replace")
yield line

def generate(self) -> None:
""" Create the ZIP report with all interesting files. """
Expand All @@ -133,7 +133,7 @@ def generate(self) -> None:

# Memory logger -> debug.log
try:
lines = b"\n".join(list(self._export_logs()))
lines = b"\n".join(self.export_logs())
zip_.writestr("debug.log", lines, compress_type=ZIP_DEFLATED)
except:
log.exception("Impossible to get lines from the memory logger")

0 comments on commit ec493f7

Please sign in to comment.