diff --git a/README.md b/README.md index 1beb826..f6bcc61 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The C Things Framework aims to address these challenges by providing an abstract - [x] Stack Data structure - [x] [Vector Library](docs/VECTORS.md) - [ ] HashMap Data structure -- [ ] Log System +- [x] [Log System](docs/LOGGER.md) - [ ] Date and Time - [x] [Test Library](docs/TESTLIB.md) diff --git a/docs/LOGGER.md b/docs/LOGGER.md new file mode 100644 index 0000000..d0bb00c --- /dev/null +++ b/docs/LOGGER.md @@ -0,0 +1 @@ +# Docs comming soon diff --git a/docs/README.md b/docs/USAGE_FROM_RELEASE.md similarity index 100% rename from docs/README.md rename to docs/USAGE_FROM_RELEASE.md diff --git a/examples/Logger.c b/examples/Logger.c new file mode 100644 index 0000000..fc5679b --- /dev/null +++ b/examples/Logger.c @@ -0,0 +1,17 @@ +#include + +#include "../include/Logger.h" + +int main(void) { + // Log to stdout + Log(stdout, INFO, "User %s is online", "Edilson"); + Log(stdout, WARN, "Remaining %s of memory", "49762KB"); + Log(stdout, ERROR, "Couldn't connect to the database"); + + // Log to a specific file + FILE *file = fopen("LOGS", "a"); + Log(file, INFO, "User %s is online", "Edilson"); + Log(file, WARN, "Remaining %s of memory", "49762KB"); + Log(file, ERROR, "Couldn't connect to the database"); + return 0; +} diff --git a/headers-joiner.py b/headers-joiner.py index f55656e..66662b8 100644 --- a/headers-joiner.py +++ b/headers-joiner.py @@ -11,6 +11,7 @@ "./include/MPSC.h", "./include/Stack.h", "./include/Testlib.h", + "./include/Logger.h", ] # Set to store unique #include statements @@ -70,6 +71,6 @@ def filter_macros(lines): for file_contents in FILES_CONTENTS: f.writelines(file_contents) - f.write("#endif\n") + f.write("#endif // C_THINGS\n") print(f"[INFO]: Combined headers into {combined_header}") diff --git a/include/Logger.h b/include/Logger.h new file mode 100644 index 0000000..9e3631c --- /dev/null +++ b/include/Logger.h @@ -0,0 +1,28 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include + +#include "Common.h" + +typedef enum { + INFO = 0, + WARN = 1, + ERROR = 2, +} LogLevel; + +/// Will convert a LogLevel enum entry to its respective string representation +#define LogLevel(level) \ + (level == INFO) ? "\033[0;32mINFO\033[0m" \ + : (level == WARN) ? "\033[0;33mWARNING\033[0m" \ + : (level == ERROR) ? "\033[0;31mERROR\033[0m" \ + : "UNKNOWN LOG LEVEL" +/** + * Function: Log(FILE *file, LogLevel level, Str_t message, ...) + * + * will log a message to the specified file + * + */ +void Log(FILE *file, LogLevel level, Str_t message, ...); + +#endif // LOGGER_H diff --git a/src/Logger.c b/src/Logger.c new file mode 100644 index 0000000..74965a8 --- /dev/null +++ b/src/Logger.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "../include/Common.h" +#include "../include/Logger.h" + +Str_t datetime_str() { + time_t current_time; + time(¤t_time); + struct tm *local_time = localtime(¤t_time); + char *time_str = malloc(sizeof(char) * 20); + strftime(time_str, 19, "%Y-%m-%d %H:%M:%S", local_time); + return time_str; +} + +void Log(FILE *file, LogLevel level, Str_t message, ...) { + fprintf(file, "[%s] %s ", datetime_str(), LogLevel(level)); + va_list args; + va_start(args, message); + vfprintf(file, message, args); + va_end(args); + fprintf(file, "\n"); +}