started working on logging

This commit is contained in:
FantasyPvP
2024-12-04 18:59:02 +00:00
parent ad96bcc0c8
commit ad8496ca68
7 changed files with 49 additions and 5 deletions
+2
View File
@@ -1,2 +1,4 @@
*/target
*.out
*somefile*
*binary*
+1
View File
@@ -1,6 +1,7 @@
#ifndef DYNSTR_H
#define DYNSTR_H
#include <stdio.h>
#include <stdint.h>
/**
* Custom String type that stores an array of characters
-4
View File
@@ -342,7 +342,3 @@ void shutdown_editor(Editor* self) {
str_dealloc(&self->filename);
free(self->buffer);
}
String_t* to_string(Editor* self) {
}
+14
View File
@@ -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;
}
+12
View File
@@ -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);
+16
View File
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#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;
}
+3
View File
@@ -15,6 +15,9 @@ fn somefunc() {
let c2 = String::from("hello world");
}
// some comment
struct EditorData {
buffer: Vec<Vec<char>>, // outer vec is the line, inner is col;
file_line: i32,