Init project and overwrite logging, so it logs to a file.

This commit is contained in:
IntegratedQuantum 2022-08-23 23:06:35 +02:00
commit 5ad35f4955
3 changed files with 75 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
logs/
saves/
zig-out/
zig-cache/
settings.json

34
build.zig Normal file
View File

@ -0,0 +1,34 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("Cubyzig", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}

36
src/main.zig Normal file
View File

@ -0,0 +1,36 @@
const std = @import("std");
var logFile: std.fs.File = undefined;
pub fn log(
comptime level: std.log.Level,
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
if(scope != .default) {
@compileError("Scopes are not supported.");
}
const color = comptime switch (level) {
std.log.Level.err => "\x1b[31m",
std.log.Level.info => "",
std.log.Level.warn => "\x1b[33m",
std.log.Level.debug => "\x1b[37;44m",
};
var buf: [4096]u8 = undefined;
std.debug.getStderrMutex().lock();
defer std.debug.getStderrMutex().unlock();
const fileMessage = std.fmt.bufPrint(&buf, "[" ++ level.asText() ++ "]" ++ ": " ++ format ++ "\n", args) catch return;
logFile.writeAll(fileMessage) catch return;
const terminalMessage = std.fmt.bufPrint(&buf, color ++ format ++ "\x1b[0m\n", args) catch return;
nosuspend std.io.getStdErr().writeAll(terminalMessage) catch return;
}
pub fn main() !void {
logFile = std.fs.cwd().createFile("logs/latest.log", .{}) catch unreachable; // init logging.
std.log.info("Hello zig.", .{});
}