Skip to content

Commit

Permalink
enqueue log messages to make logging multiprocess-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
angelala3252 committed Jun 9, 2023
1 parent 3da5b64 commit eb3deb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
45 changes: 25 additions & 20 deletions openadapt/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

Event = namedtuple("Event", ("timestamp", "type", "data"))

utils.configure_logging(logger, LOG_LEVEL)


def args_to_str(*args):
return ", ".join(map(str, args))
Expand All @@ -52,24 +54,27 @@ def kwargs_to_str(**kwargs):
return ret[:-1]


def logging(func):
@functools.wraps(func)
def wrapper_logging(*args, **kwargs):
func_name = func.__qualname__
func_args = args_to_str(*args)
func_kwargs = kwargs_to_str(**kwargs)
def logging(logger):
def decorator(func):
@functools.wraps(func)
def wrapper_logging(*args, **kwargs):
func_name = func.__qualname__
func_args = args_to_str(*args)
func_kwargs = kwargs_to_str(**kwargs)

if func_kwargs != "":
logger.info(f" -> Enter: {func_name}({func_args}, {func_kwargs})")
else:
logger.info(f" -> Enter: {func_name}({func_args})")
if func_kwargs != "":
logger.info(f" -> Enter: {func_name}({func_args}, {func_kwargs})")
else:
logger.info(f" -> Enter: {func_name}({func_args})")

result = func(*args, **kwargs)

result = func(*args, **kwargs)
logger.info(f" <- Leave: {func_name}({result})")
return result

logger.info(f" <- Leave: {func_name}({result})")
return result
return wrapper_logging

return wrapper_logging
return decorator


def process_event(event, write_q, write_fn, recording_timestamp, perf_q):
Expand All @@ -79,7 +84,7 @@ def process_event(event, write_q, write_fn, recording_timestamp, perf_q):
write_fn(recording_timestamp, event, perf_q)


@logging
@logging(logger)
def process_events(
event_q: queue.Queue,
screen_write_q: multiprocessing.Queue,
Expand Down Expand Up @@ -221,7 +226,7 @@ def write_window_event(
perf_q.put((event.type, event.timestamp, utils.get_timestamp()))


@logging
@logging(logger)
def write_events(
event_type: str,
write_fn: Callable,
Expand Down Expand Up @@ -392,7 +397,7 @@ def read_screen_events(
logger.info("done")


@logging
@logging(logger)
def read_window_events(
event_q: queue.Queue,
terminate_event: multiprocessing.Event,
Expand Down Expand Up @@ -439,7 +444,7 @@ def read_window_events(
prev_window_data = window_data


@logging
@logging(logger)
def performance_stats_writer (
perf_q: multiprocessing.Queue,
recording_timestamp: float,
Expand Down Expand Up @@ -471,7 +476,7 @@ def performance_stats_writer (
logger.info("performance stats writer done")


@logging
@logging(logger)
def create_recording(
task_description: str,
) -> Dict[str, Any]:
Expand Down Expand Up @@ -552,7 +557,7 @@ def read_mouse_events(
mouse_listener.stop()


@logging
@logging(logger)
def record(
task_description: str,
):
Expand Down
8 changes: 7 additions & 1 deletion openadapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import mss
import mss.base
import numpy as np
from logging import StreamHandler

from openadapt import common

Expand All @@ -28,7 +29,12 @@ def configure_logging(logger, log_level):
log_level_override = os.getenv("LOG_LEVEL")
log_level = log_level_override or log_level
logger.remove()
logger.add(sys.stderr, level=log_level)
logger_format = ("<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
"<level>{level: <8}</level> | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> "
"- <level>{message}</level>")
logger.add(StreamHandler(sys.stderr), colorize=True, level=log_level, enqueue=True,
format=logger_format)
logger.debug(f"{log_level=}")


Expand Down

0 comments on commit eb3deb0

Please sign in to comment.