From ad8496ca68a99baaa4775783801f81ab0c7e47be Mon Sep 17 00:00:00 2001 From: FantasyPvP <80643031+FantasyPvP@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:59:02 +0000 Subject: [PATCH] started working on logging --- .gitignore | 4 +++- final/dynstr.h | 1 + final/editor.c | 4 ---- final/log.c | 14 ++++++++++++++ final/log.h | 12 ++++++++++++ final/main.c | 16 ++++++++++++++++ prototype/src/main.rs | 3 +++ 7 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 final/log.c create mode 100644 final/log.h diff --git a/.gitignore b/.gitignore index 7fc4f74..82bce40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ */target -*.out \ No newline at end of file +*.out +*somefile* +*binary* \ No newline at end of file diff --git a/final/dynstr.h b/final/dynstr.h index ccd4e41..4439426 100644 --- a/final/dynstr.h +++ b/final/dynstr.h @@ -1,6 +1,7 @@ #ifndef DYNSTR_H #define DYNSTR_H #include +#include /** * Custom String type that stores an array of characters diff --git a/final/editor.c b/final/editor.c index 4e02d74..24d9fee 100644 --- a/final/editor.c +++ b/final/editor.c @@ -342,7 +342,3 @@ void shutdown_editor(Editor* self) { str_dealloc(&self->filename); free(self->buffer); } - -String_t* to_string(Editor* self) { - -} diff --git a/final/log.c b/final/log.c new file mode 100644 index 0000000..7f15bfa --- /dev/null +++ b/final/log.c @@ -0,0 +1,14 @@ +#include "dynstr.h" + + +int write_log(String_t* string, int lines) { + + FILE* file = fopen("log.txt", "a"); + + for (int i = 0; i < lines; i++) { + str_to_file(&string[i], file); + } + + return 0; +} + diff --git a/final/log.h b/final/log.h new file mode 100644 index 0000000..a5ff0a3 --- /dev/null +++ b/final/log.h @@ -0,0 +1,12 @@ +#include "dynstr.h" + +/** + * Writes the string to a file, appending a newline character + * This function automatically appends a newline character to the end of the string + * + * @param string the string to write + * @return 0 on success, -1 on error + * @note This function frees the string passed to it! + */ +int write_log(String_t* string, int lines); + diff --git a/final/main.c b/final/main.c index e3760f5..aba9f04 100644 --- a/final/main.c +++ b/final/main.c @@ -3,6 +3,7 @@ #include #include #include "editor.h" +#include "log.h" void help() { printf("Usage:\n"); @@ -198,6 +199,7 @@ CommandType str_to_cmd(const char* cmd) { } int main(int argc, char* argv[]) { + char log_fmt_string[128]; if (argc < 2) { help(); @@ -209,12 +211,15 @@ int main(int argc, char* argv[]) { if (argc == 3) { switch (cmd) { case CMD_OPEN: + sprintf(log_fmt_string, "Edited File [%s]", argv[2]); open_editor(argv[2]); break; case CMD_RM: remove(argv[2]); + sprintf(log_fmt_string, "Deleted File [%s]", argv[2]); break; case CMD_NEW: + sprintf(log_fmt_string, "Created File [%s]", argv[2]); open_editor(argv[2]); break; case CMD_LENC: @@ -233,11 +238,16 @@ int main(int argc, char* argv[]) { case CMD_MV: if (rename(argv[2], argv[3]) != 0) { fprintf(stderr, "Error: Failed to move file %s to %s\n", argv[2], argv[3]); + } else { + sprintf(log_fmt_string, "Moved file [%s] To [%s]", argv[2], argv[3]); } break; case CMD_CP: if (copy_file(argv[2], argv[3]) != 0) { fprintf(stderr, "Error: Failed to copy file %s to %s\n", argv[2], argv[3]); + sprintf(log_fmt_string, "Failed to copy file [%s] To [%s]", argv[2], argv[3]); + } else { + sprintf(log_fmt_string, "Copied file [%s] To [%s]", argv[2], argv[3]); } break; default: @@ -250,5 +260,11 @@ int main(int argc, char* argv[]) { help(); } + // log output of command + if (log_fmt_string[0] != '\0') { + String_t str = str_from_chars(log_fmt_string); + write_log(&str, 1); + } + return 0; } diff --git a/prototype/src/main.rs b/prototype/src/main.rs index 299f890..58bd009 100644 --- a/prototype/src/main.rs +++ b/prototype/src/main.rs @@ -15,6 +15,9 @@ fn somefunc() { let c2 = String::from("hello world"); } +// some comment + + struct EditorData { buffer: Vec>, // outer vec is the line, inner is col; file_line: i32,