Skip to content

Commit 866f2bb

Browse files
Improve new checks logs visibility (#215)
* Improve new checks logs * Change implementation to display all new checks warnings separately in the end
1 parent b594fea commit 866f2bb

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

src/extensions/score_metamodel/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,14 @@ def is_check_enabled(check: local_check_function | graph_check_function):
141141
check(app, needs_all_needs, log)
142142

143143
if log.has_warnings:
144-
log.warning("Some needs have issues. See the log for more information.")
144+
logger.warning("Some needs have issues. See the log for more information.")
145145

146146
if log.has_infos:
147-
log.info(
148-
"Some needs have issues related to the new checks. "
149-
"See the log for more information."
147+
log.flush_new_checks()
148+
logger.info(
149+
"\n\nThese next warnings are displayed as info statements for now. "
150+
"They will become real warnings in the future. "
151+
"Please fix them as soon as possible.\n"
150152
)
151153
# TODO: exit code
152154

src/extensions/score_metamodel/log.py

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414
from typing import Any
1515

1616
from docutils.nodes import Node
17+
from sphinx_needs import logging
1718
from sphinx_needs.data import NeedsInfoType
1819
from sphinx_needs.logging import SphinxLoggerAdapter
1920

21+
Location = str | tuple[str | None, int | None] | Node | None
22+
NewCheck = tuple[str, Location]
23+
logger = logging.get_logger(__name__)
24+
2025

2126
class CheckLogger:
2227
def __init__(self, log: SphinxLoggerAdapter, prefix: str):
2328
self._log = log
2429
self._info_count = 0
2530
self._warning_count = 0
2631
self._prefix = prefix
32+
self._new_checks: list[NewCheck] = []
2733

2834
@staticmethod
2935
def _location(need: NeedsInfoType, prefix: str):
@@ -43,47 +49,45 @@ def get(key: str) -> Any:
4349
return None
4450

4551
def warning_for_option(
46-
self, need: NeedsInfoType, option: str, msg: str, new_check: bool = False
52+
self, need: NeedsInfoType, option: str, msg: str, is_new_check: bool = False
4753
):
4854
full_msg = f"{need['id']}.{option} ({need.get(option, None)}): {msg}"
4955
location = CheckLogger._location(need, self._prefix)
50-
self._log_message(full_msg, location, new_check)
56+
self._log_message(full_msg, location, is_new_check)
5157

52-
def warning_for_need(self, need: NeedsInfoType, msg: str, new_check: bool = False):
58+
def warning_for_need(
59+
self, need: NeedsInfoType, msg: str, is_new_check: bool = False
60+
):
5361
full_msg = f"{need['id']}: {msg}"
5462
location = CheckLogger._location(need, self._prefix)
55-
self._log_message(full_msg, location, new_check)
63+
self._log_message(full_msg, location, is_new_check)
5664

5765
def _log_message(
5866
self,
5967
msg: str,
60-
location: None | str | tuple[str | None, int | None] | Node = None,
61-
is_info: bool = False,
68+
location: Location,
69+
is_new_check: bool = False,
6270
):
63-
if is_info:
64-
msg += (
65-
"\nPlease fix this warning related to the new check "
66-
"before the release of the next version of Docs-As-Code."
67-
)
68-
self.info(msg, location)
71+
if is_new_check:
72+
self._new_checks.append((msg, location))
73+
self._info_count += 1
6974
else:
7075
self.warning(msg, location)
76+
self._warning_count += 1
7177

7278
def info(
7379
self,
7480
msg: str,
75-
location: None | str | tuple[str | None, int | None] | Node = None,
81+
location: Location,
7682
):
7783
self._log.info(msg, type="score_metamodel", location=location)
78-
self._info_count += 1
7984

8085
def warning(
8186
self,
8287
msg: str,
83-
location: None | str | tuple[str | None, int | None] | Node = None,
88+
location: Location,
8489
):
8590
self._log.warning(msg, type="score_metamodel", location=location)
86-
self._warning_count += 1
8791

8892
@property
8993
def has_warnings(self):
@@ -92,3 +96,27 @@ def has_warnings(self):
9296
@property
9397
def has_infos(self):
9498
return self._info_count > 0
99+
100+
def flush_new_checks(self):
101+
"""Log all new-check messages together at once."""
102+
103+
def make_header_line(text: str, width: int = 80) -> str:
104+
"""Center a header inside '=' padding so line length stays fixed."""
105+
text = f" {text} "
106+
return text.center(width, "=")
107+
108+
if not self._new_checks:
109+
return
110+
111+
info_header = make_header_line("[INFO MESSAGE]")
112+
separator = "=" * 80
113+
warning_header = make_header_line(
114+
f"[New Checks] has {len(self._new_checks)} warnings"
115+
)
116+
117+
logger.info(info_header)
118+
logger.info(separator)
119+
logger.info(warning_header)
120+
121+
for msg, location in self._new_checks:
122+
self.info(msg, location)

0 commit comments

Comments
 (0)