@compileLog

@compileLog

@compileLog(args: ...)

This function prints the arguments passed to it at compile-time.

To prevent accidentally leaving compile log statements in a codebase, a compilation error is added to the build, pointing to the compile log statement. This error prevents code from being generated, but does not otherwise interfere with analysis.

This function can be used to do “printf debugging” on compile-time executing code.

test.zig

const print = @import("std").debug.print;

const num1 = blk: {
    var val1: i32 = 99;
    @compileLog("comptime val1 = ", val1);
    val1 = val1 + 1;
    break :blk val1;
};

test "main" {
    @compileLog("comptime in main");

    print("Runtime in main, num1 = {}.\n", .{num1});
}
``` ```
$ zig test test.zig
| *"comptime in main"
| *"comptime val1 = ", 99
./docgen_tmp/test.zig:11:5: error: found compile log statement
    @compileLog("comptime in main");
    ^
./docgen_tmp/test.zig:1:35: note: referenced here
const print = @import("std").debug.print;
                                  ^
./docgen_tmp/test.zig:13:5: note: referenced here
    print("Runtime in main, num1 = {}.\n", .{num1});
    ^
./docgen_tmp/test.zig:5:5: error: found compile log statement
    @compileLog("comptime val1 = ", val1);
    ^
./docgen_tmp/test.zig:13:46: note: referenced here
    print("Runtime in main, num1 = {}.\n", .{num1});
                                             ^

will ouput:

If all @compileLog calls are removed or not encountered by analysis, the program compiles successfully and the generated executable prints:

test.zig

const print = @import("std").debug.print;

const num1 = blk: {
    var val1: i32 = 99;
    val1 = val1 + 1;
    break :blk val1;
};

test "main" {
    print("Runtime in main, num1 = {}.\n", .{num1});
}
``` ```
$ zig test test.zig
Test [1/1] test "main"...
Runtime in main, num1 = 100.

All 1 tests passed.