retuve.logs

Logging utilities for the project.

Long term, this should be a decorator.

 1# Copyright 2024 Adam McArthur
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#
 7#     http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16Logging utilities for the project.
17
18Long term, this should be a decorator.
19"""
20
21import logging
22
23MS_CONVERSION_FACTOR = 1000
24
25ulogger = logging.getLogger("uvicorn")
26
27# check if logger exists
28if not ulogger.hasHandlers():
29    ulogger = logging.getLogger()
30
31
32def log_timings(timings, title=None):
33    if title is None:
34        title = "Speed:"
35
36    if not timings:
37        ulogger.info(f"{title} No timings to log.")
38        return
39
40    total_time_ms = sum(timings)
41    average_time_ms = total_time_ms / len(timings)
42    ulogger.info(
43        f"{title} {total_time_ms:.2f} s total, "
44        f"{average_time_ms*MS_CONVERSION_FACTOR :.2f} ms per frame. "
45        f"Max: {max(timings)*MS_CONVERSION_FACTOR:.2f} ms, "
46    )
MS_CONVERSION_FACTOR = 1000
ulogger = <RootLogger root (INFO)>
def log_timings(timings, title=None):
33def log_timings(timings, title=None):
34    if title is None:
35        title = "Speed:"
36
37    if not timings:
38        ulogger.info(f"{title} No timings to log.")
39        return
40
41    total_time_ms = sum(timings)
42    average_time_ms = total_time_ms / len(timings)
43    ulogger.info(
44        f"{title} {total_time_ms:.2f} s total, "
45        f"{average_time_ms*MS_CONVERSION_FACTOR :.2f} ms per frame. "
46        f"Max: {max(timings)*MS_CONVERSION_FACTOR:.2f} ms, "
47    )