Provide formattable LogFormat object for VersionChecked

- Avoid internalising the logging function and instead expose a `std.fmt` api.
This commit is contained in:
azhn
2025-04-23 16:10:24 +10:00
parent 654bfdb03b
commit 0217998533
4 changed files with 53 additions and 28 deletions

View File

@ -27,13 +27,16 @@ pub fn getRuntimeVersion() std.SemanticVersion {
};
}
const AdwaitaVersion = VersionChecked("libadwaita", std.log.scoped(.gtk), getRuntimeVersion, comptime_version);
const AdwaitaVersion = VersionChecked("libadwaita", getRuntimeVersion, comptime_version);
pub const atLeast = AdwaitaVersion.atLeast;
pub const until = AdwaitaVersion.until;
pub const runtimeAtLeast = AdwaitaVersion.runtimeAtLeast;
pub const runtimeUntil = AdwaitaVersion.runtimeUntil;
pub const logVersion = AdwaitaVersion.logVersion;
pub fn logVersion() void {
log.info("{s}", .{AdwaitaVersion.logFormat()});
}
// Whether AdwDialog, AdwAlertDialog, etc. are supported (1.5+)
pub inline fn supportsDialogs() bool {

View File

@ -1,10 +1,16 @@
const std = @import("std");
const gtk4_layer_shell = @import("gtk4-layer-shell");
const VersionChecked = @import("version.zig").VersionChecked;
const log = std.log.scoped(.gtk);
pub const getRuntimeVersion = gtk4_layer_shell.getRuntimeVersion;
const LayerShellVersion = VersionChecked("gtk4-layer-shell", std.log.scoped(.gtk), getRuntimeVersion, null);
const LayerShellVersion = VersionChecked("gtk4-layer-shell", getRuntimeVersion, null);
pub const atLeast = LayerShellVersion.atLeast;
pub const until = LayerShellVersion.until;
pub const runtimeAtLeast = LayerShellVersion.runtimeAtLeast;
pub const logVersion = LayerShellVersion.logVersion;
pub const runtimeUntil = LayerShellVersion.until;
pub fn logVersion() void {
log.info("{s}", .{LayerShellVersion.logFormat()});
}

View File

@ -9,6 +9,7 @@ const c = @cImport({
const gtk = @import("gtk");
const VersionChecked = @import("version.zig").VersionChecked;
const log = std.log.scoped(.gtk);
pub const comptime_version: std.SemanticVersion = .{
.major = c.GTK_MAJOR_VERSION,
@ -24,10 +25,13 @@ pub fn getRuntimeVersion() std.SemanticVersion {
};
}
const GTKVersion = VersionChecked("GTK", std.log.scoped(.gtk), getRuntimeVersion, comptime_version);
const GTKVersion = VersionChecked("GTK", getRuntimeVersion, comptime_version);
pub const atLeast = GTKVersion.atLeast;
pub const until = GTKVersion.until;
pub const runtimeAtLeast = GTKVersion.runtimeAtLeast;
pub const runtimeUntil = GTKVersion.runtimeUntil;
pub const logVersion = GTKVersion.logVersion;
pub fn logVersion() void {
log.info("{s}", .{GTKVersion.logFormat()});
}

View File

@ -1,20 +1,47 @@
const std = @import("std");
/// A generic way to dispatch version checks of a runtime dependency.
/// A generic construct to dispatch version comparisons of a runtime dependency.
///
/// The runtimeVersion function is expected to be created from the library we link against.
/// The runtimeVersion function is to be created from the library we link against to detect a dynamically linked version.
/// The comptime_version is optional
///
/// Whenever the runtimeVersion is required the provided function is called again, as opposed to being cached
pub fn VersionChecked(
comptime dependency_name: []const u8,
comptime log_scope: @TypeOf(std.log.scoped(.enum_literal)),
comptime getRuntimeVersion: fn () std.SemanticVersion,
comptime getRuntimeVersion_: fn () std.SemanticVersion,
comptime comptime_version_: ?std.SemanticVersion,
) type {
return struct {
const Self = @This();
const name = dependency_name;
const log = log_scope;
const comptime_version = comptime_version_;
const getRuntimeVersion = getRuntimeVersion_;
pub const LogFormat = struct {
pub fn format(
_: LogFormat,
comptime _: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) !void {
if (Self.comptime_version) |comptime_version__| {
try writer.print("{s} version build={} runtime={}", .{
Self.name,
comptime_version__,
Self.getRuntimeVersion(),
});
} else {
try writer.print("{s} version runtime={}", .{
Self.name,
Self.getRuntimeVersion(),
});
}
}
};
/// Provides an object that implements the std.fmt API intended for logging.
pub fn logFormat() LogFormat {
return .{};
}
/// Verifies that the running dependency version is at least the given
/// version.
@ -84,7 +111,7 @@ pub fn VersionChecked(
) bool {
// We use the functions instead of the constants such as c.GTK_MINOR_VERSION
// because the function gets the actual runtime version.
const runtime_version = getRuntimeVersion();
const runtime_version = getRuntimeVersion_();
return runtime_version.order(.{
.major = major,
.minor = minor,
@ -103,7 +130,7 @@ pub fn VersionChecked(
comptime minor: u16,
comptime micro: u16,
) bool {
const runtime_version = getRuntimeVersion();
const runtime_version = getRuntimeVersion_();
return runtime_version.order(.{
.major = major,
.minor = minor,
@ -111,21 +138,6 @@ pub fn VersionChecked(
}) == .lt;
}
pub fn logVersion() void {
if (Self.comptime_version) |comptime_version__| {
Self.log.info("{s} version build={} runtime={}", .{
Self.name,
comptime_version__,
getRuntimeVersion(),
});
} else {
Self.log.info("{s} version runtime={}", .{
Self.name,
getRuntimeVersion(),
});
}
}
test "atLeast" {
const testing = std.testing;
const version = Self.comptime_version orelse std.SemanticVersion{ 1, 1, 1 };