-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b10d332
commit b59a18a
Showing
7 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Docs comming soon |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¤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"); | ||
} |