Skip to content

Commit 826a96b

Browse files
committed
use python logging
1 parent 294f496 commit 826a96b

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

qbpm/icons.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from PIL import Image
1313

1414
from . import Profile, __version__
15-
from .utils import debug, error
15+
from .utils import error, info
1616

1717
PREFERRED_ICONS = [
1818
re.compile(p)
@@ -34,7 +34,7 @@ def choose_icon(icons: list[Icon]) -> Optional[Icon]:
3434
for pattern in PREFERRED_ICONS:
3535
for icon in icons:
3636
if pattern.search(icon.url):
37-
debug(f"chose {icon.url}")
37+
info(f"chose {icon.url}")
3838
return icon
3939
return None
4040

@@ -48,15 +48,15 @@ def download_icon(profile: Profile, home_page: str, overwrite: bool) -> Optional
4848
try:
4949
icons = favicon.get(home_page, headers=headers, timeout=10)
5050
except Exception as e:
51-
debug(str(e))
51+
info(str(e))
5252
error(f"failed to fetch favicon from {home_page}")
5353
return None
5454
if not icons:
5555
error(f"no favicon found on {home_page}")
5656
return None
5757
icon = choose_icon(icons)
5858
if not icon:
59-
print(f"no favicons found matching one of {PREFERRED_ICONS}")
59+
info(f"no favicons found matching one of {PREFERRED_ICONS}")
6060
return None
6161

6262
tmp_dir = TemporaryDirectory()
@@ -90,7 +90,7 @@ def install_icon_file(
9090
image = Image.open(src)
9191
image.save(dest, icon_format="png")
9292
except Exception as e:
93-
debug(str(e))
93+
info(str(e))
9494
error(f"failed to convert {origin or src} to png")
9595
return None
9696
else:

qbpm/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import sys
23
from pathlib import Path
34
from typing import Any, Callable, NoReturn, Optional
@@ -40,6 +41,12 @@ def creator_options(f: Callable[..., Any]) -> Callable[..., Any]:
4041
return f
4142

4243

44+
class LowerCaseFormatter(logging.Formatter):
45+
def format(self, record: logging.LogRecord) -> str:
46+
record.levelname = record.levelname.lower()
47+
return super().format(record)
48+
49+
4350
@click.group(context_settings=CONTEXT_SETTINGS)
4451
@click.version_option()
4552
@click.option(
@@ -51,9 +58,20 @@ def creator_options(f: Callable[..., Any]) -> Callable[..., Any]:
5158
default=default_profile_dir,
5259
help="Defaults to $XDG_DATA_HOME/qutebrowser-profiles.",
5360
)
61+
@click.option(
62+
"-l",
63+
"--log-level",
64+
default="error",
65+
type=click.Choice(["debug", "info", "error"], case_sensitive=False),
66+
)
5467
@click.pass_context
55-
def main(ctx: click.Context, profile_dir: Path) -> None:
68+
def main(ctx: click.Context, profile_dir: Path, log_level: str) -> None:
5669
ctx.obj = profile_dir
70+
root_logger = logging.getLogger()
71+
root_logger.setLevel(log_level.upper())
72+
handler = logging.StreamHandler()
73+
handler.setFormatter(LowerCaseFormatter("{levelname:>6}: {message}", style="{"))
74+
root_logger.addHandler(handler)
5775

5876

5977
@main.command()

qbpm/utils.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import platform
23
from collections.abc import Iterator
34
from os import environ
@@ -12,18 +13,13 @@
1213
AUTO_MENUS = WAYLAND_MENUS + X11_MENUS
1314
SUPPORTED_MENUS = [*AUTO_MENUS, "fzf", "applescript"]
1415

15-
global debugging
16-
debugging: bool = True
1716

18-
19-
# TODO replace with actual logger
20-
def debug(msg: str) -> None:
21-
if debugging:
22-
print(f"debug: {msg}", file=stderr)
17+
def info(msg: str) -> None:
18+
logging.info(msg)
2319

2420

2521
def error(msg: str) -> None:
26-
print(f"error: {msg}", file=stderr)
22+
logging.error(msg)
2723

2824

2925
def default_profile_dir() -> Path:
@@ -64,5 +60,5 @@ def installed_menus() -> Iterator[str]:
6460
yield "fzf-tmux"
6561
# if there's no display and fzf is installed we're probably(?) in a term
6662
if which("fzf") is not None:
67-
print("no graphical launchers found, trying fzf", file=stderr)
63+
info("no graphical launchers found, trying fzf")
6864
yield "fzf"

0 commit comments

Comments
 (0)