mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
Provide formattable LogFormat object for VersionChecked
- Avoid internalising the logging function and instead expose a `std.fmt` api.
This commit is contained in:
@ -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 atLeast = AdwaitaVersion.atLeast;
|
||||||
pub const until = AdwaitaVersion.until;
|
pub const until = AdwaitaVersion.until;
|
||||||
pub const runtimeAtLeast = AdwaitaVersion.runtimeAtLeast;
|
pub const runtimeAtLeast = AdwaitaVersion.runtimeAtLeast;
|
||||||
pub const runtimeUntil = AdwaitaVersion.runtimeUntil;
|
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+)
|
// Whether AdwDialog, AdwAlertDialog, etc. are supported (1.5+)
|
||||||
pub inline fn supportsDialogs() bool {
|
pub inline fn supportsDialogs() bool {
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const gtk4_layer_shell = @import("gtk4-layer-shell");
|
const gtk4_layer_shell = @import("gtk4-layer-shell");
|
||||||
const VersionChecked = @import("version.zig").VersionChecked;
|
const VersionChecked = @import("version.zig").VersionChecked;
|
||||||
|
const log = std.log.scoped(.gtk);
|
||||||
|
|
||||||
pub const getRuntimeVersion = gtk4_layer_shell.getRuntimeVersion;
|
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 atLeast = LayerShellVersion.atLeast;
|
||||||
|
pub const until = LayerShellVersion.until;
|
||||||
pub const runtimeAtLeast = LayerShellVersion.runtimeAtLeast;
|
pub const runtimeAtLeast = LayerShellVersion.runtimeAtLeast;
|
||||||
pub const logVersion = LayerShellVersion.logVersion;
|
pub const runtimeUntil = LayerShellVersion.until;
|
||||||
|
|
||||||
|
pub fn logVersion() void {
|
||||||
|
log.info("{s}", .{LayerShellVersion.logFormat()});
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ const c = @cImport({
|
|||||||
|
|
||||||
const gtk = @import("gtk");
|
const gtk = @import("gtk");
|
||||||
const VersionChecked = @import("version.zig").VersionChecked;
|
const VersionChecked = @import("version.zig").VersionChecked;
|
||||||
|
const log = std.log.scoped(.gtk);
|
||||||
|
|
||||||
pub const comptime_version: std.SemanticVersion = .{
|
pub const comptime_version: std.SemanticVersion = .{
|
||||||
.major = c.GTK_MAJOR_VERSION,
|
.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 atLeast = GTKVersion.atLeast;
|
||||||
pub const until = GTKVersion.until;
|
pub const until = GTKVersion.until;
|
||||||
pub const runtimeAtLeast = GTKVersion.runtimeAtLeast;
|
pub const runtimeAtLeast = GTKVersion.runtimeAtLeast;
|
||||||
pub const runtimeUntil = GTKVersion.runtimeUntil;
|
pub const runtimeUntil = GTKVersion.runtimeUntil;
|
||||||
pub const logVersion = GTKVersion.logVersion;
|
|
||||||
|
pub fn logVersion() void {
|
||||||
|
log.info("{s}", .{GTKVersion.logFormat()});
|
||||||
|
}
|
||||||
|
@ -1,20 +1,47 @@
|
|||||||
const std = @import("std");
|
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
|
/// The comptime_version is optional
|
||||||
|
///
|
||||||
|
/// Whenever the runtimeVersion is required the provided function is called again, as opposed to being cached
|
||||||
pub fn VersionChecked(
|
pub fn VersionChecked(
|
||||||
comptime dependency_name: []const u8,
|
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,
|
comptime comptime_version_: ?std.SemanticVersion,
|
||||||
) type {
|
) type {
|
||||||
return struct {
|
return struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
const name = dependency_name;
|
const name = dependency_name;
|
||||||
const log = log_scope;
|
|
||||||
const comptime_version = comptime_version_;
|
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
|
/// Verifies that the running dependency version is at least the given
|
||||||
/// version.
|
/// version.
|
||||||
@ -84,7 +111,7 @@ pub fn VersionChecked(
|
|||||||
) bool {
|
) bool {
|
||||||
// We use the functions instead of the constants such as c.GTK_MINOR_VERSION
|
// We use the functions instead of the constants such as c.GTK_MINOR_VERSION
|
||||||
// because the function gets the actual runtime version.
|
// because the function gets the actual runtime version.
|
||||||
const runtime_version = getRuntimeVersion();
|
const runtime_version = getRuntimeVersion_();
|
||||||
return runtime_version.order(.{
|
return runtime_version.order(.{
|
||||||
.major = major,
|
.major = major,
|
||||||
.minor = minor,
|
.minor = minor,
|
||||||
@ -103,7 +130,7 @@ pub fn VersionChecked(
|
|||||||
comptime minor: u16,
|
comptime minor: u16,
|
||||||
comptime micro: u16,
|
comptime micro: u16,
|
||||||
) bool {
|
) bool {
|
||||||
const runtime_version = getRuntimeVersion();
|
const runtime_version = getRuntimeVersion_();
|
||||||
return runtime_version.order(.{
|
return runtime_version.order(.{
|
||||||
.major = major,
|
.major = major,
|
||||||
.minor = minor,
|
.minor = minor,
|
||||||
@ -111,21 +138,6 @@ pub fn VersionChecked(
|
|||||||
}) == .lt;
|
}) == .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" {
|
test "atLeast" {
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
const version = Self.comptime_version orelse std.SemanticVersion{ 1, 1, 1 };
|
const version = Self.comptime_version orelse std.SemanticVersion{ 1, 1, 1 };
|
||||||
|
Reference in New Issue
Block a user