Writing a Plugin¶
This guide walks through creating a sawmill plugin from scratch using the plugin generator.
Prerequisites¶
Python 3.10+
sawmill installed (
pip install sawmill)
Generate a scaffold¶
The plugin generator is a local tool in the sawmill repository. Install it first:
$ cd tools/sawmill-plugin-generator
$ pip install -e .
Then generate a plugin project:
$ sawmill-new-plugin --name quartus
This creates a sawmill-plugin-quartus/ directory with:
sawmill-plugin-quartus/
├── pyproject.toml # Package metadata and entry point
├── README.md
├── Makefile # install / test / lint shortcuts
├── src/
│ └── sawmill_plugin_quartus/
│ ├── __init__.py # Plugin class export
│ ├── plugin.py # Hook implementations (fill in TODOs)
│ └── patterns.py # Regex patterns for log parsing
├── tests/
│ ├── conftest.py # Shared fixtures
│ ├── test_detection.py # can_handle() tests
│ ├── test_parsing.py # load_and_parse() tests
│ ├── test_severity.py # Severity level contract tests
│ └── test_plugin_contract.py # Hook interface compliance
└── examples/
└── sample.log # Example log file for development
The hookspec contract¶
Plugins inherit from SawmillPlugin and decorate methods with
@hookimpl:
from sawmill.plugin import SawmillPlugin, hookimpl
class QuartusPlugin(SawmillPlugin):
name = "quartus"
version = "0.1.0"
description = "Sawmill plugin for Intel Quartus logs"
Six hooks are available. One is required; the rest are optional.
can_handle(path) -> float (optional)¶
Return a confidence score from 0.0 to 1.0. Examine the filename, extension, or first few lines of the file:
@hookimpl
def can_handle(self, path):
if path.suffix == ".log" and "quartus" in path.stem.lower():
return 0.9
# Read first lines for tool signature
try:
with open(path) as f:
header = f.read(1024)
if "Quartus Prime" in header:
return 0.8
except OSError:
pass
return 0.0
load_and_parse(path) -> Iterator[Message] (optional)¶
Open the file, parse it line by line, group multi-line messages, and
yield each Message as soon as it is complete. Since plugin API 2
this hook is a generator; returning a list still works but forces the
whole log into memory. See Plugin API 2.
from sawmill.models.message import Message, FileRef
@hookimpl
def load_and_parse(self, path):
f = open(path, errors="replace") # open eagerly: errors surface here
return self._iter_messages(f)
def _iter_messages(self, f):
try:
for line_num, line in enumerate(f, start=1):
severity, msg_id, content = self._parse_line(line)
if severity:
yield Message(
start_line=line_num,
end_line=line_num,
raw_text=line.rstrip(),
content=content,
severity=severity,
message_id=msg_id,
)
finally:
f.close()
get_severity_levels() -> list[dict] (required)¶
Define the tool’s severity levels. Levels must be consecutive integers starting at 0 (lowest severity):
@hookimpl
def get_severity_levels(self):
return [
{"id": "error", "name": "Error", "level": 2, "style": "red bold"},
{"id": "warning", "name": "Warning", "level": 1, "style": "yellow"},
{"id": "info", "name": "Info", "level": 0, "style": "cyan"},
]
get_filters() -> list[dict] (optional)¶
Provide pre-defined filter categories:
from sawmill.models.filter_def import FilterDefinition
@hookimpl
def get_filters(self):
return [
FilterDefinition(
id="timing",
name="Timing",
pattern=r"Timing Analyzer",
source="plugin:quartus",
description="Timing analysis messages",
),
]
extract_file_reference(content) -> FileRef | None (optional)¶
Extract source file references from message content:
import re
from sawmill.models.message import FileRef
@hookimpl
def extract_file_reference(self, content):
m = re.search(r'File: (.+?) Line: (\d+)', content)
if m:
return FileRef(path=m.group(1), line=int(m.group(2)))
return None
get_grouping_fields() -> list[dict] (optional)¶
Declare custom grouping dimensions. The base class provides defaults (severity, id, file, category). Override to add tool-specific fields:
@hookimpl
def get_grouping_fields(self):
return [
{"id": "severity", "name": "Severity", "field_type": "builtin",
"description": "Group by message severity level"},
{"id": "id", "name": "Message ID", "field_type": "builtin",
"description": "Group by tool-specific message ID"},
{"id": "file", "name": "Source File", "field_type": "file_ref",
"description": "Group by source file path"},
{"id": "entity", "name": "Design Entity", "field_type": "metadata",
"description": "Group by design entity name"},
]
Metadata fields use Message.metadata[field_id] — populate this
dict in load_and_parse().
The Message model¶
Each Message has these fields:
start_line/end_lineLine numbers in the source log (1-indexed). For multi-line messages,
end_line > start_line.raw_textThe complete original text (all lines concatenated).
contentCleaned/extracted message content (without severity prefix, etc.).
severitySeverity level ID (lowercase string matching one of your
get_severity_levels()IDs).message_idTool-specific ID (e.g.
"Synth 8-6157"). Used for rule matching (the ruleidkey), session hiding and grouping.categoryOptional category for grouping (e.g.
"timing").file_refOptional
FileRef(path, line)extracted from the message.metadatadict[str, str]for plugin-specific data used by custom grouping fields.
Filling in hooks¶
The typical workflow is:
Study your tool’s log format. Identify the severity prefix pattern, message ID format, and multi-line grouping rules.
Write regex patterns in
patterns.py.Implement
can_handle()to detect the tool’s log files.Implement
load_and_parse()as a generator that yields messages.Implement
get_severity_levels()with the correct level mapping.Optionally add
get_filters()for common categories.
Plugin options: the configure hook¶
A manifest target can pass options to its plugin with a
[target.plugin_options] table:
[[target]]
name = "pnr"
plugin = "quartus"
logs = ["build/pnr/*.log"]
[target.plugin_options]
mode = "fit"
Implement the optional configure hook to receive them:
_MODES = {"synthesis", "fit"}
class QuartusPlugin(SawmillPlugin):
mode = "synthesis"
@hookimpl
def configure(self, options: dict) -> None:
unknown = set(options) - {"mode"}
if unknown:
raise ValueError(f"unknown option(s): {', '.join(sorted(unknown))}")
mode = options.get("mode", "synthesis")
if mode not in _MODES:
raise ValueError(f"mode must be one of {sorted(_MODES)}, got {mode!r}")
self.mode = mode
Called once per target, before any of the target’s logs is parsed.
optionsis the target’splugin_optionstable, or{}when the target has none; reset any per-target state here.Raise
ValueErrorfor unknown keys or bad values. Sawmill reports it as a configuration error and exits with status 2.The
SawmillPluginbase class provides a no-opconfigure. If a target givesplugin_optionsand the plugin does not override it, sawmill printswarning: ...on stderr and ignores the options.
Performance checklist¶
Make
load_and_parse()a generator; read withfor line in f, neverreadlines().Open the file eagerly so missing-file errors are raised at the call.
Use one combined regex per line; avoid a dict per match.
Intern repeated strings and share or omit
metadatadicts.
Details and measurements: Plugin API 2.
Testing¶
The generated scaffold includes test files with fixtures and basic structure. Run tests with:
$ make test
The test_plugin_contract.py file tests that your plugin conforms to
the hookspec interface (severity levels are consecutive ints from 0,
required hooks are implemented, etc.).
Installing¶
Install the plugin in development mode:
$ make install # runs: pip install -e .
Verify it appears in sawmill:
$ sawmill plugins list
$ sawmill plugins info quartus
Entry point registration¶
The generated pyproject.toml includes the entry point declaration:
[project.entry-points."sawmill.plugins"]
quartus = "sawmill_plugin_quartus:QuartusPlugin"
This is how sawmill discovers the plugin at runtime. After changing
entry points, reinstall the package (pip install -e .).