Skip to content

Commit cef85c4

Browse files
committed
feat: impl XBS_LOGPATH to redirect debug log
1 parent 9425d7b commit cef85c4

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ def update_check_time(path):
239239

240240
def trigger_parse(self, xcpath):
241241
# FIXME: ensure index_store_path from buildServer.json consistent with parsed .compile file..
242+
import xclog_parser
243+
xclog_parser.hooks_echo_to_log = True
244+
242245
from xclog_parser import parse, OutputLockedError
243246

244247
try:

xclog_parser.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
import logging
45
import os
56
import re
67
import shlex
78
import sys
89
from typing import Iterator, List, Optional
910

11+
hooks_echo_to_log = False
1012

11-
def echo(s):
12-
print(s, file=sys.stderr)
13+
def echo(s: str):
14+
if hooks_echo_to_log:
15+
logging.debug(s)
16+
else:
17+
print(s, file=sys.stderr)
1318

1419

1520
cmd_split_pattern = re.compile(

xcode-build-server

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ def show_help():
88
print(
99
f"""usage:
1010
{sys.argv[0]}: start a build server
11+
{sys.argv[0]} serve: start a build server
1112
{sys.argv[0]} config: bind xcworkspace and generate a buildServer.json to current dir
1213
{sys.argv[0]} parse: xcode log subcommand. call parse -h to see more help
1314
{sys.argv[0]} postaction: dump a xcode post build bash script to sync flags to .compile(usage: `{sys.argv[0]} postaction | bash &` in xcode post build bash script)
1415
{sys.argv[0]} [-h|--help]: show help
16+
17+
SPECIAL ENVIRONMENT VARIABLE:
18+
SOURCEKIT_LOGGING=3: enable detail debug log
19+
XBS_LOGPATH: set log path. default is :stderr. use :null to disable log
1520
"""
1621
)
1722
exit(0)
@@ -24,13 +29,35 @@ def show_debug_help():
2429
)
2530
exit(0)
2631

27-
def main():
32+
def serve():
33+
import server
34+
server.serve()
35+
36+
def setup_root_logger():
2837
level = os.environ.get("SOURCEKIT_LOGGING")
29-
logger = logging.getLogger()
30-
if (level is not None and int(level) >= 1) or os.environ.get("XBSDEBUG") == "1":
31-
logger.setLevel(logging.DEBUG)
38+
xbs_debug = ((level is not None and int(level) >= 1) or os.environ.get("XBS_DEBUG") == "1"
39+
or os.environ.get("XBSDEBUG") == "1") # compatible, remove in future
40+
level = logging.DEBUG if xbs_debug else logging.INFO
41+
42+
xbs_logpath = os.environ.get("XBS_LOGPATH")
43+
44+
if xbs_logpath:
45+
if xbs_logpath == ":null":
46+
logging.basicConfig(handlers=[logging.NullHandler()], level=level)
47+
elif xbs_logpath == ":stderr":
48+
logging.basicConfig(stream=sys.stderr, level=level)
49+
else:
50+
logging.basicConfig(filename=xbs_logpath, level=level)
51+
else:
52+
logging.basicConfig(stream=sys.stderr, level=level)
53+
54+
if xbs_debug:
3255
logging.info(f"# xcode builde server with python {sys.version}]")
3356

57+
58+
def main():
59+
setup_root_logger()
60+
3461
if len(sys.argv) > 1:
3562

3663
if "-h" == sys.argv[1] or "--help" == sys.argv[1]:
@@ -56,6 +83,8 @@ def main():
5683
bash "{os.path.abspath(os.path.join(os.path.realpath(__file__), "..", "post_action.bash"))}"
5784
"""
5885
)
86+
elif sys.argv[1] == "serve":
87+
serve()
5988
elif sys.argv[1] == "debug":
6089

6190
if sys.argv[2] == "print-xclog":
@@ -70,13 +99,10 @@ def main():
7099
show_help()
71100
else:
72101
# else serve as build server, and wait json reqest
73-
import server
74-
if logger.level > logging.INFO:
75-
logger.setLevel(logging.INFO)
76-
77-
server.serve()
102+
serve()
78103

79104

80105
if __name__ == "__main__":
106+
# print(sys.path)
81107
# sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
82108
main()

0 commit comments

Comments
 (0)