Have Zig's std.log log to a file instead of to stderr
at main 98 lines 3.4 kB view raw
1// Copyright 2024 - 2025, Kristófer Reykjalín and the log_to_file contributors. 2// SPDX-License-Identifier: BSD-3-Clause 3 4const std = @import("std"); 5const builtin = @import("builtin"); 6 7const minimum_zig_version = std.SemanticVersion.parse("0.14.0") catch unreachable; 8 9const Example = enum { 10 defaults, 11 custom_log_file, 12 custom_storage_path, 13 custom_storage_path_and_log_file, 14}; 15 16pub fn build(b: *std.Build) void { 17 // Enforce minimum zig version. 18 if (comptime (builtin.zig_version.order(minimum_zig_version) == .lt)) { 19 @compileError(std.fmt.comptimePrint( 20 \\Your Zig version does not meet the minimum build requirement: 21 \\ required Zig version: {[minimum_zig_version]} 22 \\ actual Zig version: {[current_version]} 23 \\ 24 , .{ 25 .current_version = builtin.zig_version, 26 .minimum_zig_version = minimum_zig_version, 27 })); 28 } 29 30 const target = b.standardTargetOptions(.{}); 31 const optimize = b.standardOptimizeOption(.{}); 32 const root_source_file = b.path("src/log_to_file.zig"); 33 34 // TODO: figure out why stripping debug symbols causes an issue. 35 // const strip = b.option(bool, "strip", "Strip debug symbols") orelse 36 // if (optimize == .Debug) false else true; 37 38 const ltf = b.addModule("log_to_file", .{ 39 .root_source_file = root_source_file, 40 .target = target, 41 .optimize = optimize, 42 // .strip = strip, 43 }); 44 45 // Add a check for ZLS to build-on-save. 46 const ltf_check = b.addExecutable(.{ 47 .name = "log_to_file", 48 .root_module = b.createModule(.{ 49 .root_source_file = b.path("examples/defaults.zig"), 50 .optimize = optimize, 51 .target = target, 52 }), 53 }); 54 ltf_check.root_module.addImport("log_to_file", ltf); 55 const check = b.step("check", "Check if log_to_file compiles"); 56 check.dependOn(&ltf_check.step); 57 58 // Build docs. 59 const docs_obj = b.addLibrary(.{ 60 .name = "log_to_file", 61 .root_module = ltf, 62 .linkage = .static, 63 }); 64 const install_docs = b.addInstallDirectory(.{ 65 .source_dir = docs_obj.getEmittedDocs(), 66 .install_dir = .prefix, 67 .install_subdir = "docs", 68 }); 69 const docs = b.step("docs", "Build log_to_file library docs"); 70 docs.dependOn(&install_docs.step); 71 72 const maybe_example = b.option(Example, "example", "Run an example"); 73 if (maybe_example) |example| { 74 const source_file = switch (example) { 75 .defaults => b.path("examples/defaults.zig"), 76 .custom_log_file => b.path("examples/custom_log_file.zig"), 77 .custom_storage_path => b.path("examples/custom_storage_path.zig"), 78 .custom_storage_path_and_log_file => b.path("examples/custom_storage_path_and_log_file.zig"), 79 }; 80 81 const exe = b.addExecutable(.{ 82 .name = "example", 83 .root_module = b.createModule(.{ 84 .root_source_file = source_file, 85 .optimize = optimize, 86 .target = target, 87 }), 88 }); 89 90 exe.root_module.addImport("log_to_file", ltf); 91 92 b.installArtifact(exe); 93 94 const run_exe = b.addRunArtifact(exe); 95 const run_step = b.step("run", "Run an example"); 96 run_step.dependOn(&run_exe.step); 97 } 98}