Skip to content

Commit dc95b56

Browse files
committed
game_process: also log stdout/stderr to file
1 parent 3063349 commit dc95b56

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

dweam/game_process.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
1+
import os
2+
from pathlib import Path
3+
import sys
4+
5+
6+
def setup_logging() -> None:
7+
"""Set up logging to both console and file"""
8+
9+
pid = os.getpid()
10+
11+
cache_dir = os.environ.get("CACHE_DIR")
12+
if cache_dir is None:
13+
cache_dir = Path.home() / ".dweam" / "cache"
14+
else:
15+
cache_dir = Path(cache_dir)
16+
17+
log_dir = cache_dir / "worker_logs"
18+
log_dir.mkdir(exist_ok=True)
19+
20+
log_file = log_dir / f"game_process_{pid}.log"
21+
22+
# Create file handle with line buffering
23+
log_handle = open(log_file, 'w', buffering=1)
24+
25+
# Save original stdout/stderr
26+
original_stdout = sys.stdout
27+
original_stderr = sys.stderr
28+
29+
class DualOutput:
30+
def __init__(self, file1, file2):
31+
self.file1 = file1
32+
self.file2 = file2
33+
34+
def write(self, data):
35+
self.file1.write(data)
36+
self.file2.write(data)
37+
self.file1.flush()
38+
self.file2.flush()
39+
40+
def flush(self):
41+
self.file1.flush()
42+
self.file2.flush()
43+
44+
# Replace stdout/stderr with dual-output versions
45+
sys.stdout = DualOutput(original_stdout, log_handle)
46+
sys.stderr = DualOutput(original_stderr, log_handle)
47+
48+
print(f"=== Game Process {pid} Starting ===")
49+
print(f"Logging to {log_file}")
50+
print(f"Command line args: {sys.argv}")
51+
52+
53+
setup_logging()
54+
55+
156
import logging
257
logging.getLogger("aioice.ice").disabled = True
358

459
import asyncio
560
import json
6-
import sys
761
from typing import Any
862
from datetime import datetime, timedelta
963
from dweam.constants import JS_TO_PYGAME_BUTTON_MAP, JS_TO_PYGAME_KEY_MAP
@@ -15,7 +69,6 @@
1569
from aiortc import VideoStreamTrack, RTCPeerConnection, RTCSessionDescription, RTCConfiguration, RTCIceServer, RTCDataChannel
1670
from aiortc.contrib.signaling import object_from_string, object_to_string
1771
import torch
18-
import os
1972
import socket
2073
from dweam.utils.process import patch_subprocess_popen
2174

0 commit comments

Comments
 (0)