From e5f2f7c5e54b659431bff78e4b05220656ad4021 Mon Sep 17 00:00:00 2001 From: MrBounty Date: Sat, 26 Oct 2024 18:52:10 +0200 Subject: [PATCH] Changed some print to log --- src/fileEngine.zig | 6 ++++-- src/main.zig | 12 ++++++------ src/stuffs/utils.zig | 22 +++++----------------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/fileEngine.zig b/src/fileEngine.zig index 6744624..1ac8510 100644 --- a/src/fileEngine.zig +++ b/src/fileEngine.zig @@ -23,6 +23,8 @@ const BUFFER_SIZE = @import("config.zig").BUFFER_SIZE; const MAX_FILE_SIZE = @import("config.zig").MAX_FILE_SIZE; const CSV_DELIMITER = @import("config.zig").CSV_DELIMITER; +const log = std.log.scoped(.fileEngine); + /// Manage everything that is relate to read or write in files /// Or even get stats, whatever. If it touch files, it's here pub const FileEngine = struct { @@ -288,7 +290,7 @@ pub const FileEngine = struct { file.close(); // Do I need to close ? I think so file = std.fs.cwd().openFile(path_buff, .{}) catch { - std.debug.print("Error trying to open {s}\n", .{path_buff}); + log.err("Error trying to open {s}\n", .{path_buff}); @panic("Can't open file to update a data iterator"); }; @@ -954,7 +956,7 @@ pub const FileEngine = struct { continue; }, // file read till the end else => { - std.debug.print("Error while reading file: {any}\n", .{err}); + log.err("Error while reading file: {any}", .{err}); break; }, }; diff --git a/src/main.zig b/src/main.zig index c636306..85ee8d3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -70,13 +70,13 @@ pub fn myLog( // TODO: If an argument is given when starting the binary, it is the db path pub fn main() !void { - var state: State = .expect_main_command; + errdefer log.warn("Main function ended with an error", .{}); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); defer switch (gpa.deinit()) { .ok => {}, - .leak => std.log.debug("We fucked it up bro...\n", .{}), + .leak => log.debug("We fucked it up bro...\n", .{}), }; var file_engine = try initFileEngine.init(allocator, null); @@ -86,7 +86,7 @@ pub fn main() !void { defer allocator.free(line_buf); while (true) { - std.debug.print("> ", .{}); + std.debug.print("> ", .{}); // TODO: Find something better than just std.debug.print const line = try std.io.getStdIn().reader().readUntilDelimiterOrEof(line_buf, '\n'); if (line) |line_str| { @@ -97,7 +97,7 @@ pub fn main() !void { var toker = cliTokenizer.init(null_term_line_str); var token = toker.next(); - state = .expect_main_command; + var state = State.expect_main_command; while ((state != .end) and (state != .quit)) : (token = toker.next()) switch (state) { .expect_main_command => switch (token.tag) { @@ -333,7 +333,7 @@ const initFileEngine = struct { if (!file_engine.isSchemaFileInDir()) { try initSchema(allocator, &file_engine); } else { - std.debug.print("Database has a schema.\n", .{}); + log.info("Database has a schema.\n", .{}); } return file_engine; @@ -359,7 +359,7 @@ const initFileEngine = struct { if (schema) |s| { log.debug("Found environment variable ZIPPONDB_SCHEMA: {s}.", .{s}); file_engine.initDataFolder(s) catch { - std.debug.print("Couldn't use {s} as schema.\n", .{s}); + log.warn("Couldn't use {s} as schema.\n", .{s}); }; } else { log.debug("No environment variable ZIPPONDB_SCHEMA found.", .{}); diff --git a/src/stuffs/utils.zig b/src/stuffs/utils.zig index 792d746..cfdfe13 100644 --- a/src/stuffs/utils.zig +++ b/src/stuffs/utils.zig @@ -1,6 +1,8 @@ const std = @import("std"); const ZipponError = @import("errors.zig").ZipponError; +const log = std.log.scoped(.utils); + pub fn getEnvVariable(allocator: std.mem.Allocator, variable: []const u8) ?[]const u8 { var env_map = std.process.getEnvMap(allocator) catch return null; defer env_map.deinit(); @@ -31,28 +33,12 @@ pub fn getDirTotalSize(dir: std.fs.Dir) !u64 { return total; } -pub fn getArgsString(allocator: std.mem.Allocator) std.ArrayList(u8) { - const args = try std.process.argsAlloc(allocator); - defer std.process.argsFree(allocator, args); - - var buffer = std.ArrayList(u8).init(allocator); - var writer = buffer.writer(); - - for (args) |arg| { - writer.print("{s} ", .{arg}); - } - - buffer.append(0); - - return buffer; -} - const stdout = std.io.getStdOut().writer(); // Maybe create a struct for that pub fn send(comptime format: []const u8, args: anytype) void { stdout.print(format, args) catch |err| { - std.log.err("Can't send: {any}", .{err}); + log.err("Can't send: {any}", .{err}); stdout.print("\x03\n", .{}) catch {}; }; @@ -91,6 +77,8 @@ pub fn printError(message: []const u8, err: ZipponError, query: ?[]const u8, sta writer.print(" \n", .{}) catch {}; // Align with the message } + log.debug("Parsing error: {s}", .{buffer.items}); + send("{s}", .{buffer.items}); return err; }