commit 5ad35f49556e3e13a0dcd0de34d6e4bcf60f5817 Author: IntegratedQuantum Date: Tue Aug 23 23:06:35 2022 +0200 Init project and overwrite logging, so it logs to a file. diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..610adde6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +logs/ +saves/ +zig-out/ +zig-cache/ +settings.json \ No newline at end of file diff --git a/build.zig b/build.zig new file mode 100644 index 00000000..4d65af6f --- /dev/null +++ b/build.zig @@ -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); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 00000000..560536de --- /dev/null +++ b/src/main.zig @@ -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.", .{}); +}