Changed some print to log

This commit is contained in:
Adrien Bouvais 2024-10-26 18:52:10 +02:00
parent c5f931cf04
commit e5f2f7c5e5
3 changed files with 15 additions and 25 deletions

View File

@ -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;
},
};

View File

@ -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.", .{});

View File

@ -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;
}