Skip to content

std.Build.Step.Options: handle std.log.Level and std.log.ScopeLevel #24196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dweiller
Copy link
Contributor

This change adds support for std.log.Level and std.log.ScopeLevel to std.Build.Step.Options, similar to the existing support for std.SemanticVersion. Makes it more straightforward to provide build options for configuring logging by removing the need to use @enumFromInt and @intFromEnum (or some other serialisation scheme).

With this change it becomes possible to do this:

// build.zig
pub fn build(b: *std.Build) void {
    const options = b.addOptions();
    const log_level: std.log.Level = b.option(std.log.Level, "log-level", "Most verbose log level") orelse .info;
    options.addOption(std.log.Level, "log_level", log_level);

    const root_module = b.createModule(.{
        .root_source_file = b.path("src/main.zig"),
    };

    root_module.addOptions("build_options", options);

    const exe = b.addExecutable(.{
        .name = "prog",
        .root_module = root_module,
    });
}

// src/main.zig

const std_options: std.Options = .{
    .log_level = @import("build_options").log_level,
};

Without this change, you would need to do:

     const log_level: std.log.Level = b.option(std.log.Level, "log-level", "Most verbose log level") orelse .info;
-    options.addOption(std.log.Level, "log_level", log_level);
+    options.addOption(u2, "log_level", @intFromEnum(log_level));
 const std_options: std.Options = .{
-    .log_level = @import("build_options").log_level,
+    .log_level = @enumFromInt(@import("build_options").log_level),
 };

which isn't too bad, but it gets more annoying when wanting to use std.log.ScopeLevels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant