add logging code and get it setup

This commit is contained in:
Nayan
2025-05-08 20:54:10 -04:00
parent 6a17569284
commit 6468cc8d55
5 changed files with 103 additions and 8 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
*.out
*.o
*.o
/logs

View File

@@ -5,7 +5,7 @@ RELEASE_FLAGS = -Wall -Wextra
LFLAGS = -lpthread
# Source files
SRCS = webserver.c client_handler.c request_handler.c http_stuff.c response_builder.c
SRCS = webserver.c client_handler.c request_handler.c http_stuff.c response_builder.c logging.c
# Object files definition
OBJS = $(SRCS:.c=.o)

68
logging.c Normal file
View File

@@ -0,0 +1,68 @@
#include "logging.h"
FILE* log_file = NULL;
pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
int log_level = LOG_DEBUG; // Should pull from env variables eventually
void set_log_level(int level) { log_level = level; }
void open_log_file() {
log_file = fopen(LOG_FILE_NAME, "a");
if (log_file == NULL) {
perror("Failed to open log file\n");
}
}
void close_log_file() {
if (log_file != NULL) {
fclose(log_file);
log_file = NULL;
}
}
void log_message(int level, const char* fmt, ...) {
if (level < log_level) {
return;
}
char timestamp[32];
time_t now = time(NULL);
struct tm* timeinfo = localtime(&now);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", timeinfo);
char* level_str = level == LOG_DEBUG ? "DEBUG"
: level == LOG_INFO ? "INFO"
: level == LOG_WARN ? "WARN"
: "ERROR";
// Use variadic arguments to format string
va_list args;
va_start(args, fmt);
// Log to console
fprintf(stderr, "[%s] [%s] ", timestamp, level_str);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
// Reset variadic arguments
va_end(args);
va_start(args, fmt);
// Log to file
pthread_mutex_lock(&log_mutex);
if (log_file == NULL) {
open_log_file();
}
if (log_file == NULL) {
perror("Failed to open log file\n");
}
if (log_file != NULL) {
fprintf(log_file, "[%s] [%s] ", timestamp, level_str);
vfprintf(log_file, fmt, args);
fprintf(log_file, "\n");
}
fflush(log_file);
pthread_mutex_unlock(&log_mutex);
va_end(args);
}

21
logging.h Normal file
View File

@@ -0,0 +1,21 @@
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#define LOG_FILE_NAME "./logs/log.txt"
#define LOG_DEBUG 0
#define LOG_INFO 1
#define LOG_WARN 2
#define LOG_ERROR 3
void set_log_level(int level);
void log_message(int level, const char* fmt, ...);
void open_log_file();
void close_log_file();

View File

@@ -9,17 +9,21 @@
#include <unistd.h>
#include "client_handler.h"
#include "logging.h"
struct sockaddr_in server_addr;
#define DEBUG 1
int main() {
open_log_file();
set_log_level(DEBUG ? LOG_DEBUG : LOG_INFO);
// Create socket
int server = socket(AF_INET, SOCK_STREAM, 0);
if (server < 0) {
perror("socket creation failed");
log_message(LOG_ERROR, "Socket creation failed");
exit(1);
}
log_message(LOG_INFO, "Socket created");
// Socket address config
server_addr.sin_family = AF_INET;
@@ -27,6 +31,7 @@ int main() {
server_addr.sin_port = htons(8080);
// Allow reuse of port when debugging
if (DEBUG) {
log_message(LOG_DEBUG, "Allowing reuse of port");
int reuse = 1;
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
}
@@ -34,13 +39,13 @@ int main() {
// Bind socket
if (bind(server, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
0) {
perror("bind failed");
log_message(LOG_ERROR, "Socket bind failed");
exit(1);
}
// Listen for connections
if (listen(server, 5) < 0) {
perror("listen failed");
log_message(LOG_ERROR, "Socket listen failed");
exit(1);
}
@@ -62,7 +67,7 @@ int main() {
if (c == 'q') {
close(server);
shutdown(server, SHUT_RDWR);
printf("Server closed\n");
log_message(LOG_INFO, "Shutting down server");
break;
}
}
@@ -76,7 +81,7 @@ int main() {
*client = accept(server, (struct sockaddr *)&client_addr,
&client_len);
if (*client < 0) {
perror("Failed to accept client");
log_message(LOG_ERROR, "Socket accept failed");
continue;
}
@@ -86,6 +91,6 @@ int main() {
}
}
}
close_log_file();
return 0;
}