add timestamps to HTML debug output

This commit is contained in:
Sarah Hoffmann 2023-05-26 09:05:44 +02:00
parent b48cda7173
commit c7db69a30c

View File

@ -9,6 +9,7 @@ Functions for specialised logging with HTML output.
""" """
from typing import Any, Iterator, Optional, List, Tuple, cast from typing import Any, Iterator, Optional, List, Tuple, cast
from contextvars import ContextVar from contextvars import ContextVar
import datetime as dt
import textwrap import textwrap
import io import io
@ -98,11 +99,16 @@ class HTMLLogger(BaseLogger):
self.buffer = io.StringIO() self.buffer = io.StringIO()
def _timestamp(self) -> None:
self._write(f'<p class="timestamp">[{dt.datetime.now()}]</p>')
def get_buffer(self) -> str: def get_buffer(self) -> str:
return HTML_HEADER + self.buffer.getvalue() + HTML_FOOTER return HTML_HEADER + self.buffer.getvalue() + HTML_FOOTER
def function(self, func: str, **kwargs: Any) -> None: def function(self, func: str, **kwargs: Any) -> None:
self._timestamp()
self._write(f"<h1>Debug output for {func}()</h1>\n<p>Parameters:<dl>") self._write(f"<h1>Debug output for {func}()</h1>\n<p>Parameters:<dl>")
for name, value in kwargs.items(): for name, value in kwargs.items():
self._write(f'<dt>{name}</dt><dd>{self._python_var(value)}</dd>') self._write(f'<dt>{name}</dt><dd>{self._python_var(value)}</dd>')
@ -110,14 +116,17 @@ class HTMLLogger(BaseLogger):
def section(self, heading: str) -> None: def section(self, heading: str) -> None:
self._timestamp()
self._write(f"<h2>{heading}</h2>") self._write(f"<h2>{heading}</h2>")
def comment(self, text: str) -> None: def comment(self, text: str) -> None:
self._timestamp()
self._write(f"<p>{text}</p>") self._write(f"<p>{text}</p>")
def var_dump(self, heading: str, var: Any) -> None: def var_dump(self, heading: str, var: Any) -> None:
self._timestamp()
if callable(var): if callable(var):
var = var() var = var()
@ -125,6 +134,7 @@ class HTMLLogger(BaseLogger):
def table_dump(self, heading: str, rows: Iterator[Optional[List[Any]]]) -> None: def table_dump(self, heading: str, rows: Iterator[Optional[List[Any]]]) -> None:
self._timestamp()
head = next(rows) head = next(rows)
assert head assert head
self._write(f'<table><thead><tr><th colspan="{len(head)}">{heading}</th></tr><tr>') self._write(f'<table><thead><tr><th colspan="{len(head)}">{heading}</th></tr><tr>')
@ -143,6 +153,7 @@ class HTMLLogger(BaseLogger):
def result_dump(self, heading: str, results: Iterator[Tuple[Any, Any]]) -> None: def result_dump(self, heading: str, results: Iterator[Tuple[Any, Any]]) -> None:
""" Print a list of search results generated by the generator function. """ Print a list of search results generated by the generator function.
""" """
self._timestamp()
def format_osm(osm_object: Optional[Tuple[str, int]]) -> str: def format_osm(osm_object: Optional[Tuple[str, int]]) -> str:
if not osm_object: if not osm_object:
return '-' return '-'
@ -173,6 +184,7 @@ class HTMLLogger(BaseLogger):
def sql(self, conn: AsyncConnection, statement: 'sa.Executable') -> None: def sql(self, conn: AsyncConnection, statement: 'sa.Executable') -> None:
self._timestamp()
sqlstr = self.format_sql(conn, statement) sqlstr = self.format_sql(conn, statement)
if CODE_HIGHLIGHT: if CODE_HIGHLIGHT:
sqlstr = highlight(sqlstr, PostgresLexer(), sqlstr = highlight(sqlstr, PostgresLexer(),
@ -348,6 +360,26 @@ HTML_HEADER: str = """<!DOCTYPE html>
padding: 3pt; padding: 3pt;
border: solid lightgrey 0.1pt border: solid lightgrey 0.1pt
} }
table, th, tbody {
border: thin solid;
border-collapse: collapse;
}
td {
border-right: thin solid;
padding-left: 3pt;
padding-right: 3pt;
}
.timestamp {
font-size: 0.8em;
color: darkblue;
width: calc(100% - 5pt);
text-align: right;
position: absolute;
left: 0;
margin-top: -5px;
}
</style> </style>
</head> </head>
<body> <body>