30 lines
716 B
C
30 lines
716 B
C
|
// Chat GPT code
|
||
|
#include "log.h"
|
||
|
|
||
|
const char* get_current_time()
|
||
|
{
|
||
|
static char buffer[20];
|
||
|
time_t now = time(NULL);
|
||
|
struct tm* tm_info = localtime(&now);
|
||
|
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info);
|
||
|
return buffer;
|
||
|
}
|
||
|
|
||
|
void log_message(LogLevel level, const char* message)
|
||
|
{
|
||
|
const char* level_strings[] = { "INFO", "WARNING", "ERROR" };
|
||
|
|
||
|
// Open the log file in append mode
|
||
|
FILE* log_file = fopen("logfile.txt", "a");
|
||
|
if (log_file == NULL) {
|
||
|
fprintf(stderr, "Could not open log file for writing.\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Write the log message to the file
|
||
|
fprintf(log_file, "[%s] [%s] %s\n", get_current_time(), level_strings[level], message);
|
||
|
|
||
|
// Close the log file
|
||
|
fclose(log_file);
|
||
|
}
|