Skip to content

Commit

Permalink
Fix incorrect logging directory for perception (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xierumeng authored Jul 14, 2024
1 parent 55e622a commit 325a831
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
4 changes: 3 additions & 1 deletion documentation/main_multiprocess_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ def main() -> int:
print("ERROR: Failed to load configuration file")
return -1

# Get Pylance to stop complaining
assert config is not None

# Setup main logger
result, main_logger = logger_setup_main.setup_main_logger(config)
result, main_logger, _ = logger_setup_main.setup_main_logger(config)
if not result:
print("ERROR: Failed to create main logger")
return -1

# Get Pylance to stop complaining
assert main_logger is not None

# Main is managing all worker processes and is responsible
Expand Down
18 changes: 6 additions & 12 deletions main_2024.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import argparse
import datetime
import inspect
import multiprocessing as mp
import pathlib
Expand Down Expand Up @@ -52,41 +51,36 @@ def main() -> int:
print("ERROR: Failed to load configuration file")
return -1

# Get Pylance to stop complaining
assert config is not None

# Setup main logger
result, main_logger = logger_setup_main.setup_main_logger(config)
result, main_logger, logging_path = logger_setup_main.setup_main_logger(config)
if not result:
print("ERROR: Failed to create main logger")
return -1

# Get Pylance to stop complaining
assert main_logger is not None
assert logging_path is not None

# Get settings
try:
# Local constants
# pylint: disable=invalid-name
QUEUE_MAX_SIZE = config["queue_max_size"]

log_directory_path = config["logger"]["directory_path"]
log_path_format = config["logger"]["file_datetime_format"]
start_time = datetime.datetime.now().strftime(log_path_format)

VIDEO_INPUT_CAMERA_NAME = config["video_input"]["camera_name"]
VIDEO_INPUT_WORKER_PERIOD = config["video_input"]["worker_period"]
VIDEO_INPUT_SAVE_NAME_PREFIX = config["video_input"]["save_prefix"]
VIDEO_INPUT_SAVE_PREFIX = (
f"{log_directory_path}/{start_time}/{VIDEO_INPUT_SAVE_NAME_PREFIX}"
)
VIDEO_INPUT_SAVE_PREFIX = str(pathlib.Path(logging_path, VIDEO_INPUT_SAVE_NAME_PREFIX))

DETECT_TARGET_WORKER_COUNT = config["detect_target"]["worker_count"]
DETECT_TARGET_DEVICE = "cpu" if args.cpu else config["detect_target"]["device"]
DETECT_TARGET_MODEL_PATH = config["detect_target"]["model_path"]
DETECT_TARGET_OVERRIDE_FULL_PRECISION = args.full
DETECT_TARGET_SAVE_NAME_PREFIX = config["detect_target"]["save_prefix"]
DETECT_TARGET_SAVE_PREFIX = (
f"{log_directory_path}/{start_time}/{DETECT_TARGET_SAVE_NAME_PREFIX}"
)
DETECT_TARGET_SAVE_PREFIX = str(pathlib.Path(logging_path, DETECT_TARGET_SAVE_NAME_PREFIX))
DETECT_TARGET_SHOW_ANNOTATED = args.show_annotated

FLIGHT_INTERFACE_ADDRESS = config["flight_interface"]["address"]
Expand Down
2 changes: 2 additions & 0 deletions modules/detect_target/detect_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ def run(
confidence = float(boxes.conf[i])
result, detection = detections_and_time.Detection.create(bounds, label, confidence)
if result:
# Get Pylance to stop complaining
assert detection is not None

detections.append(detection)

# Logging
Expand Down
2 changes: 2 additions & 0 deletions modules/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def create(cls, name: str, enable_log_to_file: bool) -> "tuple[bool, Logger | No
print("ERROR: Failed to load configuration file")
return False, None

# Get Pylance to stop complaining
assert config is not None

try:
Expand Down Expand Up @@ -106,6 +107,7 @@ def message_and_metadata(message: str, frame: "types.FrameType | None") -> str:
if frame is None:
return message

# Get Pylance to stop complaining
assert frame is not None

function_name = frame.f_code.co_name
Expand Down
16 changes: 10 additions & 6 deletions modules/logger/logger_setup_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
MAIN_LOGGER_NAME = "main"


def setup_main_logger(config: "dict") -> "tuple[bool, logger.Logger | None]":
def setup_main_logger(config: "dict") -> "tuple[bool, logger.Logger | None, pathlib.Path | None]":
"""
Setup prerequisites for logging in `main()` .
config: The configuration.
Returns: Success, logger.
TODO: Remove logger path from return
Returns: Success, logger, logger path.
"""
# Get settings
try:
Expand All @@ -27,21 +28,24 @@ def setup_main_logger(config: "dict") -> "tuple[bool, logger.Logger | None]":
start_time = datetime.datetime.now().strftime(log_path_format)
except KeyError as exception:
print(f"ERROR: Config key(s) not found: {exception}")
return False, None
return False, None, None

logging_path = pathlib.Path(log_directory_path, start_time)

# Create logging directory
pathlib.Path(log_directory_path).mkdir(exist_ok=True)
pathlib.Path(log_directory_path, start_time).mkdir()
logging_path.mkdir()

# Setup logger
result, main_logger = logger.Logger.create(MAIN_LOGGER_NAME, True)
if not result:
print("ERROR: Failed to create main logger")
return False, None
return False, None, None

# Get Pylance to stop complaining
assert main_logger is not None

frame = inspect.currentframe()
main_logger.info(f"{MAIN_LOGGER_NAME} logger initialized", frame)

return True, main_logger
return True, main_logger, logging_path

0 comments on commit 325a831

Please sign in to comment.