diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig index 9c81776..89c72f0 100644 --- a/src/main.zig +++ b/src/main.zig | |||
| @@ -2,14 +2,54 @@ const std = @import("std"); | |||
| 2 | 2 | ||
| 3 | const Allocator = std.mem.Allocator; | 3 | const Allocator = std.mem.Allocator; |
| 4 | const Editor = @import("Editor.zig"); | 4 | const Editor = @import("Editor.zig"); |
| 5 | const File = std.fs.File; | ||
| 5 | const GPA = std.heap.GeneralPurposeAllocator(.{}); | 6 | const GPA = std.heap.GeneralPurposeAllocator(.{}); |
| 6 | const RawMode = @import("RawMode.zig"); | 7 | const RawMode = @import("RawMode.zig"); |
| 7 | 8 | ||
| 9 | var log_file: ?File = null; | ||
| 10 | |||
| 11 | pub fn log( | ||
| 12 | comptime level: std.log.Level, | ||
| 13 | comptime scope: @TypeOf(.EnumLiteral), | ||
| 14 | comptime format: []const u8, | ||
| 15 | args: anytype, | ||
| 16 | ) void { | ||
| 17 | const file = if (log_file) |f| f else std.io.getStdErr(); | ||
| 18 | |||
| 19 | const writer = file.writer(); | ||
| 20 | |||
| 21 | const prefix = switch (scope) { | ||
| 22 | .default => "[" ++ level.asText() ++ "] ", | ||
| 23 | else => "[" ++ @tagName(scope) ++ ":" ++ level.asText() ++ "] ", | ||
| 24 | }; | ||
| 25 | |||
| 26 | nosuspend writer.print(prefix ++ format ++ "\n", args) catch return; | ||
| 27 | } | ||
| 28 | |||
| 8 | pub fn main() !void { | 29 | pub fn main() !void { |
| 9 | var gpa = GPA{}; | 30 | var gpa = GPA{}; |
| 10 | defer _ = gpa.deinit(); | 31 | defer _ = gpa.deinit(); |
| 11 | const allocator = gpa.allocator(); | 32 | const allocator = gpa.allocator(); |
| 12 | 33 | ||
| 34 | const log_file_name = try std.fmt.allocPrint( | ||
| 35 | allocator, | ||
| 36 | "/tmp/es.{}.log", | ||
| 37 | .{std.os.linux.getpid()}, | ||
| 38 | ); | ||
| 39 | defer { | ||
| 40 | std.log.info("Logs in {s}", .{log_file_name}); | ||
| 41 | allocator.free(log_file_name); | ||
| 42 | } | ||
| 43 | |||
| 44 | log_file = try std.fs.createFileAbsolute( | ||
| 45 | log_file_name, | ||
| 46 | .{ .exclusive = true, .truncate = true }, | ||
| 47 | ); | ||
| 48 | defer { | ||
| 49 | log_file.?.close(); | ||
| 50 | log_file = null; | ||
| 51 | } | ||
| 52 | |||
| 13 | const raw_mode = try RawMode.init(); | 53 | const raw_mode = try RawMode.init(); |
| 14 | defer raw_mode.deinit(); | 54 | defer raw_mode.deinit(); |
| 15 | 55 | ||