Skip to content

Commit

Permalink
stage2: -Dlog enables all logging, log scopes can be set at runtime
Browse files Browse the repository at this point in the history
Previously you had to recompile if you wanted to change the log scopes
that get printed. Now, log scopes can be set at runtime, and -Dlog
controls whether all logging is available at runtime.

Purpose here is a nicer development experience. Most likely stage2
developers will always want -Dlog enabled and then pass --debug-log
scopes when debugging particular issues.
  • Loading branch information
andrewrk committed Jan 19, 2021
1 parent 287f640 commit 1af31ba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn build(b: *Builder) !void {
test_stage2.linkLibC();
}

const log_scopes = b.option([]const []const u8, "log", "Which log scopes to enable") orelse &[0][]const u8{};
const enable_logging = b.option(bool, "log", "Whether to enable logging") orelse false;

const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git.");
const version = if (opt_version_string) |version| version else v: {
Expand Down Expand Up @@ -190,7 +190,7 @@ pub fn build(b: *Builder) !void {
const semver = try std.SemanticVersion.parse(version);
exe.addBuildOption(std.SemanticVersion, "semver", semver);

exe.addBuildOption([]const []const u8, "log_scopes", log_scopes);
exe.addBuildOption(bool, "enable_logging", enable_logging);
exe.addBuildOption(bool, "enable_tracy", tracy != null);
exe.addBuildOption(bool, "is_stage1", is_stage1);
exe.addBuildOption(bool, "omit_stage2", false);
Expand Down
3 changes: 3 additions & 0 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,9 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
}
}

log.debug("calling updateDecl on '{s}', type={}", .{
decl.name, decl.typed_value.most_recent.typed_value.ty,
});
assert(decl.typed_value.most_recent.typed_value.ty.hasCodeGenBits());

self.bin_file.updateDecl(module, decl) catch |err| switch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/config.zig.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub const have_llvm = true;
pub const version: [:0]const u8 = "@ZIG_VERSION@";
pub const semver = try @import("std").SemanticVersion.parse(version);
pub const log_scopes: []const []const u8 = &[_][]const u8{};
pub const enable_logging: bool = false;
pub const enable_tracy = false;
pub const is_stage1 = true;
pub const skip_non_native = false;
Expand Down
19 changes: 16 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,26 @@ pub const log_level: std.log.Level = switch (std.builtin.mode) {
.ReleaseSmall => .crit,
};

var log_scopes: std.ArrayListUnmanaged([]const u8) = .{};

pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
// Hide debug messages unless added with `-Dlog=foo`.
// Hide debug messages unless:
// * logging enabled with `-Dlog`.
// * the --debug-log arg for the scope has been provided
if (@enumToInt(level) > @enumToInt(std.log.level) or
@enumToInt(level) > @enumToInt(std.log.Level.info))
{
if (!build_options.enable_logging) return;

const scope_name = @tagName(scope);
const ok = comptime for (build_options.log_scopes) |log_scope| {
for (log_scopes.items) |log_scope| {
if (mem.eql(u8, log_scope, scope_name))
break true;
break;
} else return;
}

Expand Down Expand Up @@ -156,6 +162,8 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
}
}

defer log_scopes.deinit(gpa);

const cmd = args[1];
const cmd_args = args[2..];
if (mem.eql(u8, cmd, "build-exe")) {
Expand Down Expand Up @@ -358,6 +366,7 @@ const usage_build_generic =
\\ --verbose-llvm-ir Enable compiler debug output for LLVM IR
\\ --verbose-cimport Enable compiler debug output for C imports
\\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
\\ --debug-log [scope] Enable printing debug/info log messages for scope
\\
;

Expand Down Expand Up @@ -811,6 +820,10 @@ fn buildOutputType(
if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
i += 1;
override_lib_dir = args[i];
} else if (mem.eql(u8, arg, "--debug-log")) {
if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
i += 1;
try log_scopes.append(gpa, args[i]);
} else if (mem.eql(u8, arg, "-fcompiler-rt")) {
want_compiler_rt = true;
} else if (mem.eql(u8, arg, "-fno-compiler-rt")) {
Expand Down

0 comments on commit 1af31ba

Please sign in to comment.