Skip to content

Commit

Permalink
update logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-black-tea committed Jan 14, 2024
1 parent 42a8a8e commit 372a26f
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/linktools/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def _load(self, env: BaseEnviron, key: Any, cache: Any):

default = cache
if default is not __missing__ and not self.always_ask:
if not env.get_config("RELOAD_CONFIG", type=bool):
if not env.get_config("RELOAD_CONFIG", type=bool, default=False):
return default

if default is __missing__:
Expand Down Expand Up @@ -417,7 +417,7 @@ def _load(self, env: BaseEnviron, key: Any, cache: Any):

default = cache
if default is not __missing__ and not self.always_ask:
if not env.get_config("RELOAD_CONFIG", type=bool):
if not env.get_config("RELOAD_CONFIG", type=bool, default=False):
return default

if default is __missing__:
Expand Down
4 changes: 0 additions & 4 deletions src/linktools/_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ def _internal_config(self):
config.update(
DEBUG=False,
STORAGE_PATH=str(pathlib.Path.home() / f".{metadata.__name__}"),
ENVVAR_PREFIX=None,
RELOAD_CONFIG=False,
SHOW_LOG_TIME=False,
SHOW_LOG_LEVEL=True,
)

yaml_path = os.path.join(root_path, "template", "tools.yml")
Expand Down
49 changes: 30 additions & 19 deletions src/linktools/_rich.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class LogHandler(RichHandler):
def __init__(self, environ: BaseEnviron):
super().__init__(
show_path=False,
show_level=environ.get_config("SHOW_LOG_LEVEL"),
show_time=environ.get_config("SHOW_LOG_TIME"),
show_level=environ.get_config("SHOW_LOG_LEVEL", type=bool, default=True),
show_time=environ.get_config("SHOW_LOG_TIME", type=bool, default=False),
omit_repeated_times=False,
log_time_format=self.make_time_text
# markup=True,
Expand Down Expand Up @@ -125,10 +125,13 @@ def make_time_text(self, time: Union[float, datetime, None] = None, format: str
time = datetime.now()
elif isinstance(time, (int, float)):
time = datetime.fromtimestamp(time)
if not format:
format = "[%x %X]"
if not style:
style = "log.time"
if not format:
if self.formatter:
format = self.formatter.datefmt
if not format:
format = "[%x %X]"
return Text(time.strftime(format), style=style)

def make_level_text(self, level_no: int, level_name: str = None, style: str = None) -> Text:
Expand Down Expand Up @@ -182,33 +185,41 @@ def __init__(self):
super().__init__(table_column=Column(no_wrap=True))

def render(self, task: Task = None) -> Union[str, Text]:
handler = LogHandler.get_instance()
if not handler:
return ""
result = Text()
if handler.show_time:
date_format = None
if handler.formatter:
date_format = handler.formatter.datefmt
result.append(handler.make_time_text(format=date_format))
result.append(" ")
if handler.show_level:
result.append(handler.make_level_text(logging.INFO, "⇲"))

handler = LogHandler.get_instance()
if handler:
if handler.show_time:
if len(result) > 0:
result.append(" ")
result.append(handler.make_time_text())

if handler.show_level:
if len(result) > 0:
result.append(" ")
result.append(handler.make_level_text(logging.INFO))

return result


def create_progress():
return Progress(
LogColumn(),
columns = []

handler = LogHandler.get_instance()
if handler and (handler.show_time or handler.show_level):
columns.append(LogColumn())

columns.extend([
TextColumn("[progress.description]{task.description}"),
BarColumn(),
DownloadColumn(),
TransferSpeedColumn(),
TaskProgressColumn(),
TextColumn("eta"),
TimeRemainingColumn(),
refresh_per_second=.1,
)
])

return Progress(*columns, refresh_per_second=.1)


PromptType = TypeVar("PromptType", bound=PromptBase)
Expand Down
5 changes: 5 additions & 0 deletions src/linktools/cli/commands/android/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Command(AndroidCommand):
"detach",
]

def main(self, *args, **kwargs) -> None:
self.environ.config.set_default("SHOW_LOG_LEVEL", False)
self.environ.config.set_default("SHOW_LOG_TIME", False)
return super().main(*args, **kwargs)

def init_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument("adb_args", nargs="...", metavar="args", help="adb args")

Expand Down
5 changes: 5 additions & 0 deletions src/linktools/cli/commands/android/frida.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Command(AndroidCommand):
Easy to use frida (require Android device rooted)
"""

def main(self, *args, **kwargs) -> None:
self.environ.config.set_default("SHOW_LOG_LEVEL", True)
self.environ.config.set_default("SHOW_LOG_TIME", True)
return super().main(*args, **kwargs)

@property
def known_errors(self) -> List[Type[BaseException]]:
return super().known_errors + [DownloadError]
Expand Down
5 changes: 5 additions & 0 deletions src/linktools/cli/commands/common/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Command(BaseCommand):
Shell with environment variables already initialized
"""

def main(self, *args, **kwargs) -> None:
self.environ.config.set_default("SHOW_LOG_LEVEL", False)
self.environ.config.set_default("SHOW_LOG_TIME", False)
return super().main(*args, **kwargs)

def __init__(self):
self._shell_path = None
if environ.system in ["darwin", "linux"]:
Expand Down
5 changes: 5 additions & 0 deletions src/linktools/cli/commands/common/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Command(BaseCommand):
Download and use tools
"""

def main(self, *args, **kwargs) -> None:
self.environ.config.set_default("SHOW_LOG_LEVEL", False)
self.environ.config.set_default("SHOW_LOG_TIME", False)
return super().main(*args, **kwargs)

@property
def known_errors(self) -> List[Type[BaseException]]:
return super().known_errors + [ToolError, DownloadError]
Expand Down
5 changes: 5 additions & 0 deletions src/linktools/cli/commands/ios/frida.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Command(IOSCommand):
Easy to use frida (require iOS device jailbreak)
"""

def main(self, *args, **kwargs) -> None:
self.environ.config.set_default("SHOW_LOG_LEVEL", True)
self.environ.config.set_default("SHOW_LOG_TIME", True)
return super().main(*args, **kwargs)

@property
def known_errors(self) -> List[Type[BaseException]]:
return super().known_errors + [DownloadError]
Expand Down
5 changes: 5 additions & 0 deletions src/linktools/cli/commands/ios/sib.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Command(IOSCommand):
"remote",
]

def main(self, *args, **kwargs) -> None:
self.environ.config.set_default("SHOW_LOG_LEVEL", False)
self.environ.config.set_default("SHOW_LOG_TIME", False)
return super().main(*args, **kwargs)

def init_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument('sib_args', nargs='...', metavar="args", help="sib args")

Expand Down
5 changes: 5 additions & 0 deletions src/linktools/cli/commands/ios/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Command(IOSCommand):
OpenSSH remote login client (require iOS device jailbreak)
"""

def main(self, *args, **kwargs) -> None:
self.environ.config.set_default("SHOW_LOG_LEVEL", False)
self.environ.config.set_default("SHOW_LOG_TIME", False)
return super().main(*args, **kwargs)

@property
def known_errors(self) -> List[Type[BaseException]]:
return super().known_errors + [SSHException]
Expand Down

0 comments on commit 372a26f

Please sign in to comment.