zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 1af31baf0ba447b6ea5a1456df5ba2d82dc26e56 (tree)
parent 287f640cc94d7f1cddb30e9ef57a8c921621a5b9
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Tue, 19 Jan 2021 15:49:08 -0700

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.

Diffstat:
Mbuild.zig | 4++--
Msrc/Compilation.zig | 3+++
Msrc/config.zig.in | 2+-
Msrc/main.zig | 19++++++++++++++++---
4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/build.zig b/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); diff --git a/src/Compilation.zig b/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) { diff --git a/src/config.zig.in b/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; diff --git a/src/main.zig b/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")) {