Commit 1af31baf0b

Andrew Kelley <andrew@ziglang.org>
2021-01-19 23:49:08
stage2: -Dlog enables all logging, log scopes can be set at runtime
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.
1 parent 287f640
src/Compilation.zig
@@ -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) {
src/config.zig.in
@@ -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;
src/main.zig
@@ -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;
     }
 
@@ -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")) {
@@ -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
     \\
 ;
 
@@ -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")) {
build.zig
@@ -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: {
@@ -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);