Skip to content

Commit 820cce3

Browse files
committed
Convert most of prints to logs (WIP)
1 parent fc76e4b commit 820cce3

File tree

1 file changed

+56
-36
lines changed

1 file changed

+56
-36
lines changed

UDKTests/run_udk_tests.py

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,31 @@
4444
import unicodedata
4545
import watchdog.events
4646
import watchdog.observers
47+
from loguru import logger
4748
from udk_configparser import UDKConfigParser
4849

4950
import defaults
5051

5152
# TODO: leverage pytest?
52-
# TODO: use the logging module.
5353
# TODO: reduce log spam? More verbose logging to a file,
5454
# only general info straight to console?
5555

56+
57+
_log_format = "[{time:YYYY-MM-DD HH:mm:ss.SSSZZ}] [{level}] [{function}] {message}"
58+
59+
logger.remove()
60+
logger.add(
61+
sys.stdout,
62+
format=_log_format,
63+
level="DEBUG",
64+
)
65+
logger.add(
66+
"run_udk_tests.log",
67+
format=_log_format,
68+
rotation="50 MB",
69+
level="DEBUG",
70+
)
71+
5672
SCRIPT_DIR = Path(__file__).parent
5773
REPO_DIR = SCRIPT_DIR.parent
5874
CACHE_DIR = SCRIPT_DIR / ".cache/"
@@ -107,13 +123,13 @@ def state(self) -> State:
107123

108124
@state.setter
109125
def state(self, state: State):
110-
print(f"setting state: {state}")
126+
logger.info("setting state: {}", state)
111127
self._state = state
112128
self._pos = 0
113129

114130
def on_any_event(self, event: watchdog.events.FileSystemEvent):
115131
if Path(event.src_path).name == self._log_filename:
116-
print(f"fs event: {event.event_type}, {event.src_path}")
132+
logger.info("fs event: {} {}", event.event_type, event.src_path)
117133
self._fh = open(self._log_file, errors="replace", encoding="utf-8")
118134

119135
# TODO: better state handling, this is a mess ATM.
@@ -125,7 +141,7 @@ def on_modified(self, event: watchdog.events.FileSystemEvent):
125141

126142
size = self._log_file.stat().st_size
127143
if size == 0:
128-
print("log file cleared")
144+
logger.info("log file cleared")
129145
self._pos = 0
130146
return
131147

@@ -152,7 +168,7 @@ def on_modified(self, event: watchdog.events.FileSystemEvent):
152168

153169
if os.getenv("GITHUB_ACTIONS"):
154170
line = unicodedata.normalize("NFKD", line)
155-
print(line.strip())
171+
logger.info(line.strip())
156172

157173
if self._state == State.BUILDING:
158174
if "Log file closed" in line:
@@ -162,7 +178,7 @@ def on_modified(self, event: watchdog.events.FileSystemEvent):
162178
log_end = True
163179

164180
if log_end:
165-
print("setting stop event")
181+
logger.info("setting stop event")
166182
if self._state == State.BUILDING:
167183
self._building_event.set()
168184
elif self._state == State.TESTING:
@@ -191,7 +207,7 @@ def resolve_script_path(path: str) -> Path:
191207

192208

193209
def write_cache(file: Path, cache: Cache):
194-
print(f"writing cache file: '{file}'")
210+
logger.info("writing cache file: '{}'", file)
195211
file.write_text(json.dumps(asdict(cache), indent=2))
196212

197213

@@ -202,7 +218,7 @@ def load_cache(path: Path) -> Cache:
202218

203219

204220
def download_file(url: str, out_file: Path, progress_bar: bool = True):
205-
print(f"downloading '{url}'")
221+
logger.info("downloading: '{}'", url)
206222
with out_file.open("wb") as f:
207223
with httpx.stream(
208224
"GET",
@@ -226,18 +242,18 @@ def download_file(url: str, out_file: Path, progress_bar: bool = True):
226242
for data in resp.iter_bytes(chunk_size=4096):
227243
f.write(data)
228244

229-
print("download finished")
245+
logger.info("download finished")
230246

231247

232248
def remove_old_extracted(cache: Cache):
233-
print("removing old extracted files, if any")
249+
logger.info("removing old extracted files, if any")
234250

235251
dirs: list[Path] = []
236252

237253
for file in cache.pkg_archive_extracted_files:
238254
p = Path(file).resolve()
239255
if p.exists() and p.is_file():
240-
print(f"removing '{p}'")
256+
logger.info("removing '{}'", p)
241257
p.unlink(missing_ok=True)
242258
elif p.is_dir():
243259
dirs.append(p)
@@ -247,9 +263,9 @@ def remove_old_extracted(cache: Cache):
247263
if is_empty:
248264
shutil.rmtree(d)
249265
else:
250-
print(f"not removing non-empty directory: '{d}'")
266+
logger.info("not removing non-empty directory: '{}'", d)
251267

252-
print("remove_old_extracted done")
268+
logger.info("remove_old_extracted done")
253269

254270

255271
def already_extracted(archive_file: str, out_dir: Path, cache: Cache) -> bool:
@@ -272,12 +288,17 @@ def poke_file(file: Path, event: threading.Event):
272288
time.sleep(1)
273289

274290

291+
def move_file(src: Path, dst: Path):
292+
logger.info("{} -> {}", src, dst)
293+
shutil.move(src, dst)
294+
295+
275296
async def run_udk_build(
276297
watcher: LogWatcher,
277298
udk_lite_root: Path,
278299
building_event: threading.Event,
279300
) -> int:
280-
print("starting UDK build phase")
301+
logger.info("starting UDK build phase")
281302

282303
udk_exe = (udk_lite_root / "Binaries/Win64/UDK.com").resolve()
283304

@@ -295,7 +316,7 @@ async def run_udk_build(
295316
],
296317
)
297318

298-
print(f"proc: {proc}")
319+
logger.info("proc: {}", proc)
299320

300321
ok = building_event.wait(timeout=UDK_TEST_TIMEOUT)
301322

@@ -306,10 +327,10 @@ async def run_udk_build(
306327
if not ok:
307328
raise RuntimeError("timed out waiting for UDK.exe (building_event) stop event")
308329

309-
print("UDK.exe finished")
330+
logger.info("UDK.exe finished")
310331

311332
ec = await proc.wait()
312-
print(f"UDK.exe exited with code: {ec}")
333+
logger.info("UDK.exe exited with code: {}", ec)
313334

314335
if ec != 0:
315336
raise RuntimeError(f"UDK.exe error: {ec}")
@@ -323,7 +344,7 @@ async def run_udk_server(
323344
testing_event: threading.Event,
324345
udk_args: str,
325346
) -> int:
326-
print("starting UDK testing phase")
347+
logger.info("starting UDK testing phase")
327348

328349
udk_exe_norunaway = (udk_lite_root / "Binaries/Win64/UDK_norunaway.exe").resolve()
329350
udk_exe = (udk_lite_root / "Binaries/Win64/UDK.exe").resolve()
@@ -334,14 +355,12 @@ async def run_udk_server(
334355
dst = udk_exe.with_name("UDK.exe.backup")
335356

336357
if dst.exists():
337-
print(f"{dst} exists, assuming runaway loop detection executable already in use")
358+
logger.info("{} exists, assuming runaway loop detection executable already in use", dst)
338359
else:
339-
print("moving original UDK.exe to backup "
340-
"and replacing it with runaway loop detection patched variant")
341-
print(f"{udk_exe} -> {dst}")
342-
shutil.move(udk_exe, dst)
343-
print(f"{udk_exe_norunaway} -> {udk_exe}")
344-
shutil.move(udk_exe_norunaway, udk_exe)
360+
logger.info("moving original UDK.exe to backup "
361+
"and replacing it with runaway loop detection patched variant")
362+
move_file(udk_exe, dst)
363+
move_file(udk_exe_norunaway, udk_exe)
345364

346365
watcher.state = State.TESTING
347366
test_proc = await asyncio.create_subprocess_exec(
@@ -355,7 +374,7 @@ async def run_udk_server(
355374
],
356375
)
357376

358-
print(f"proc: {test_proc}")
377+
logger.info("proc: {}", test_proc)
359378

360379
ok = testing_event.wait(timeout=UDK_TEST_TIMEOUT)
361380

@@ -367,7 +386,7 @@ async def run_udk_server(
367386
raise RuntimeError("timed out waiting for UDK.exe (testing_event) stop event")
368387

369388
test_ec = await test_proc.wait()
370-
print(f"UDK.exe FCrypto test run exited with code: {test_ec}")
389+
logger.info("UDK.exe FCrypto test run exited with code: {}", test_ec)
371390

372391
return test_ec
373392

@@ -377,7 +396,7 @@ def print_udk_processes(event: threading.Event):
377396
sleep_time = 0.01
378397
for proc in psutil.process_iter():
379398
if "udk" in proc.name().lower():
380-
print(f"\t{'#' * 4} {proc}")
399+
logger.info("{} {}", '#' * 4, proc)
381400
sleep_time = 5.0
382401

383402
time.sleep(sleep_time)
@@ -391,14 +410,14 @@ async def start_gmp_server(echo_server_path: Path):
391410
str(echo_server_path)
392411
)
393412

394-
print(f"{FCRYPTO_GMP_SERVER_PROC=}")
413+
logger.info("FCRYPTO_GMP_SERVER_PROC={}", FCRYPTO_GMP_SERVER_PROC)
395414

396415

397416
async def stop_gmp_server():
398417
if FCRYPTO_GMP_SERVER_PROC:
399418
FCRYPTO_GMP_SERVER_PROC.terminate()
400419
ec = await FCRYPTO_GMP_SERVER_PROC.wait()
401-
print(f"FCRYPTO_GMP_SERVER_PROC exited with code: {ec}")
420+
logger.info("FCRYPTO_GMP_SERVER_PROC exited with code: {}", ec)
402421

403422

404423
async def main():
@@ -443,16 +462,17 @@ async def main():
443462
try:
444463
cache = load_cache(cache_file)
445464
except Exception as e:
446-
print(f"error loading cache: {type(e).__name__}: {e}")
447-
print(f"delete the cache file '{cache_file}' "
448-
f"or cache directory '{CACHE_DIR}' to force a hard reset")
465+
logger.error("error loading cache: {} {}", type(e).__name__, e)
466+
logger.info("delete the cache file '{}' "
467+
"or cache directory '{}' to force a hard reset",
468+
cache_file, CACHE_DIR)
449469
raise
450470
else:
451471
hard_reset = True
452472

453473
if hard_reset:
454-
print("hard reset requested")
455-
print(f"removing '{CACHE_DIR}'")
474+
logger.info("hard reset requested")
475+
logger.info("removing '{}'", CACHE_DIR)
456476
shutil.rmtree(CACHE_DIR, ignore_errors=True)
457477
CACHE_DIR.mkdir(parents=True, exist_ok=True)
458478
write_cache(cache_file, cache)
@@ -667,7 +687,7 @@ async def main():
667687
try:
668688
asyncio.run(main())
669689
except Exception as _e:
670-
print(f"error running main: {_e}")
690+
logger.error("error running man: {}", _e)
671691

672692
if BUILDING_EVENT:
673693
BUILDING_EVENT.set()

0 commit comments

Comments
 (0)