X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/c314a3092c5b51c7782015f6fa9ac093b46fa174..1f0796778754d8df0dfab9dd01302e26a397f064:/src/nominatim_api/logging.py diff --git a/src/nominatim_api/logging.py b/src/nominatim_api/logging.py index 7df36ec1..1a6aef9b 100644 --- a/src/nominatim_api/logging.py +++ b/src/nominatim_api/logging.py @@ -49,41 +49,35 @@ class BaseLogger: """ Start a new debug chapter for the given function and its parameters. """ - def section(self, heading: str) -> None: """ Start a new section with the given title. """ - def comment(self, text: str) -> None: """ Add a simple comment to the debug output. """ - def var_dump(self, heading: str, var: Any) -> None: """ Print the content of the variable to the debug output prefixed by the given heading. """ - def table_dump(self, heading: str, rows: Iterator[Optional[List[Any]]]) -> None: """ Print the table generated by the generator function. """ - def result_dump(self, heading: str, results: Iterator[Tuple[Any, Any]]) -> None: """ Print a list of search results generated by the generator function. """ - def sql(self, conn: AsyncConnection, statement: 'sa.Executable', params: Union[Mapping[str, Any], Sequence[Mapping[str, Any]], None]) -> None: """ Print the SQL for the given statement. """ def format_sql(self, conn: AsyncConnection, statement: 'sa.Executable', - extra_params: Union[Mapping[str, Any], - Sequence[Mapping[str, Any]], None]) -> str: + extra_params: Union[Mapping[str, Any], Sequence[Mapping[str, Any]], None] + ) -> str: """ Return the compiled version of the statement. """ compiled = cast('sa.ClauseElement', statement).compile(conn.sync_engine) @@ -108,7 +102,7 @@ class BaseLogger: try: sqlstr = re.sub(r'__\[POSTCOMPILE_[^]]*\]', '%s', sqlstr) return sqlstr % tuple((repr(params.get(name, None)) - for name in compiled.positiontup)) # type: ignore + for name in compiled.positiontup)) # type: ignore except TypeError: return sqlstr @@ -121,28 +115,26 @@ class BaseLogger: assert conn.dialect.name == 'sqlite' # params in positional order - pparams = (repr(params.get(name, None)) for name in compiled.positiontup) # type: ignore + pparams = (repr(params.get(name, None)) for name in compiled.positiontup) # type: ignore sqlstr = re.sub(r'__\[POSTCOMPILE_([^]]*)\]', '?', sqlstr) sqlstr = re.sub(r"\?", lambda m: next(pparams), sqlstr) return sqlstr + class HTMLLogger(BaseLogger): """ Logger that formats messages in HTML. """ def __init__(self) -> None: self.buffer = io.StringIO() - def _timestamp(self) -> None: self._write(f'

[{dt.datetime.now()}]

') - def get_buffer(self) -> str: return HTML_HEADER + self.buffer.getvalue() + HTML_FOOTER - def function(self, func: str, **kwargs: Any) -> None: self._timestamp() self._write(f"

Debug output for {func}()

\n

Parameters:

") @@ -150,17 +142,14 @@ class HTMLLogger(BaseLogger): self._write(f'
{name}
{self._python_var(value)}
') self._write('

') - def section(self, heading: str) -> None: self._timestamp() self._write(f"

{heading}

") - def comment(self, text: str) -> None: self._timestamp() self._write(f"

{text}

") - def var_dump(self, heading: str, var: Any) -> None: self._timestamp() if callable(var): @@ -168,7 +157,6 @@ class HTMLLogger(BaseLogger): self._write(f'
{heading}
{self._python_var(var)}') - def table_dump(self, heading: str, rows: Iterator[Optional[List[Any]]]) -> None: self._timestamp() head = next(rows) @@ -185,11 +173,11 @@ class HTMLLogger(BaseLogger): self._write('') self._write('') - def result_dump(self, heading: str, results: Iterator[Tuple[Any, Any]]) -> None: """ Print a list of search results generated by the generator function. """ self._timestamp() + def format_osm(osm_object: Optional[Tuple[str, int]]) -> str: if not osm_object: return '-' @@ -218,7 +206,6 @@ class HTMLLogger(BaseLogger): total += 1 self._write(f'TOTAL: {total}

') - def sql(self, conn: AsyncConnection, statement: 'sa.Executable', params: Union[Mapping[str, Any], Sequence[Mapping[str, Any]], None]) -> None: self._timestamp() @@ -230,7 +217,6 @@ class HTMLLogger(BaseLogger): else: self._write(f'{html.escape(sqlstr)}') - def _python_var(self, var: Any) -> str: if CODE_HIGHLIGHT: fmt = highlight(str(var), PythonLexer(), HtmlFormatter(nowrap=True)) @@ -238,7 +224,6 @@ class HTMLLogger(BaseLogger): return f'{html.escape(str(var))}' - def _write(self, text: str) -> None: """ Add the raw text to the debug output. """ @@ -251,38 +236,31 @@ class TextLogger(BaseLogger): def __init__(self) -> None: self.buffer = io.StringIO() - def _timestamp(self) -> None: self._write(f'[{dt.datetime.now()}]\n') - def get_buffer(self) -> str: return self.buffer.getvalue() - def function(self, func: str, **kwargs: Any) -> None: self._write(f"#### Debug output for {func}()\n\nParameters:\n") for name, value in kwargs.items(): self._write(f' {name}: {self._python_var(value)}\n') self._write('\n') - def section(self, heading: str) -> None: self._timestamp() self._write(f"\n# {heading}\n\n") - def comment(self, text: str) -> None: self._write(f"{text}\n") - def var_dump(self, heading: str, var: Any) -> None: if callable(var): var = var() self._write(f'{heading}:\n {self._python_var(var)}\n\n') - def table_dump(self, heading: str, rows: Iterator[Optional[List[Any]]]) -> None: self._write(f'{heading}:\n') data = [list(map(self._python_var, row)) if row else None for row in rows] @@ -291,7 +269,7 @@ class TextLogger(BaseLogger): maxlens = [max(len(d[i]) for d in data if d) for i in range(num_cols)] tablewidth = sum(maxlens) + 3 * num_cols + 1 - row_format = '| ' +' | '.join(f'{{:<{l}}}' for l in maxlens) + ' |\n' + row_format = '| ' + ' | '.join(f'{{:<{ln}}}' for ln in maxlens) + ' |\n' self._write('-'*tablewidth + '\n') self._write(row_format.format(*data[0])) self._write('-'*tablewidth + '\n') @@ -303,7 +281,6 @@ class TextLogger(BaseLogger): if data[-1]: self._write('-'*tablewidth + '\n') - def result_dump(self, heading: str, results: Iterator[Tuple[Any, Any]]) -> None: self._timestamp() self._write(f'{heading}:\n') @@ -318,18 +295,15 @@ class TextLogger(BaseLogger): total += 1 self._write(f'TOTAL: {total}\n\n') - def sql(self, conn: AsyncConnection, statement: 'sa.Executable', params: Union[Mapping[str, Any], Sequence[Mapping[str, Any]], None]) -> None: self._timestamp() sqlstr = '\n| '.join(textwrap.wrap(self.format_sql(conn, statement, params), width=78)) self._write(f"| {sqlstr}\n\n") - def _python_var(self, var: Any) -> str: return str(var) - def _write(self, text: str) -> None: self.buffer.write(text) @@ -368,8 +342,8 @@ HTML_HEADER: str = """ Nominatim - Debug