Skip to content

Commit

Permalink
feat: impl logger system
Browse files Browse the repository at this point in the history
  • Loading branch information
edilson258 committed Jun 24, 2024
1 parent b10d332 commit b59a18a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions docs/LOGGER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Docs comming soon
File renamed without changes.
17 changes: 17 additions & 0 deletions examples/Logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>

#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;
}
3 changes: 2 additions & 1 deletion headers-joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"./include/MPSC.h",
"./include/Stack.h",
"./include/Testlib.h",
"./include/Logger.h",
]

# Set to store unique #include statements
Expand Down Expand Up @@ -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}")
28 changes: 28 additions & 0 deletions include/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>

#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
25 changes: 25 additions & 0 deletions src/Logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "../include/Common.h"
#include "../include/Logger.h"

Str_t datetime_str() {
time_t current_time;
time(&current_time);
struct tm *local_time = localtime(&current_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");
}

0 comments on commit b59a18a

Please sign in to comment.