Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logging library #32

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = gcc
FLAGS = -Wall -Wextra -Wshadow
SRC = src/main.c src/sock.c src/http.c src/utils.c src/fraction.c src/crc32.c
SRC = src/main.c src/sock.c src/http.c src/utils.c src/fraction.c src/crc32.c src/log.c
OUT = client

all:
Expand Down
2 changes: 2 additions & 0 deletions client/include/fraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <endian.h>

#include "log.h"

#define MAGIC 0xdeadbeef

typedef struct {
Expand Down
2 changes: 2 additions & 0 deletions client/include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "sock.h"
#include "utils.h"

#include "log.h"
// error codes
#define HTTP_SUCCESS 0
#define HTTP_SOCKET_ERR -1
Expand All @@ -26,6 +27,7 @@ typedef struct {
void http_free(http_res_t *res);

int http_get(int sfd, const char *path, http_res_t *res);
/* Keeping in case we end up using it sometime */
int http_post(int sfd,const char* path,const char *content_type, const char* parameters, http_res_t *res);

long parse_http_status_code(const char *buf);
Expand Down
52 changes: 52 additions & 0 deletions client/include/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2020 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See `log.c` for details.
*/

#ifndef LOG_H
#define LOG_H

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>

#define LOG_VERSION "0.1.0"
#define LOG_USE_COLOR 1


typedef struct {
va_list ap;
const char *fmt;
const char *file;
struct tm *time;
void *udata;
int line;
int level;
} log_Event;

typedef void (*log_LogFn)(log_Event *ev);
typedef void (*log_LockFn)(bool lock, void *udata);

enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };

#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)

const char *log_level_string(int level);
void log_set_lock(log_LockFn fn, void *udata);
void log_set_level(int level);
void log_set_quiet(bool enable);
int log_add_callback(log_LogFn fn, void *udata, int level);
int log_add_fp(FILE *fp, int level);

void log_log(int level, const char *file, int line, const char *fmt, ...);
int log_get_level(void);

#endif
40 changes: 22 additions & 18 deletions client/src/fraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ int download_fraction(int sfd, char *url, fraction_t *fraction) {
// Parse the URL to get the path
path = get_path_from_url(url);
if (!path) {
fprintf(stderr, "Invalid URL: %s\n", url);
log_error("Invalid URL: %s\n", url);
return 1;
}

// Perform the HTTP GET request
if (http_get(sfd, path, &res) != HTTP_SUCCESS) {
fprintf(stderr, "Failed to download: %s\n", url);
log_error("Failed to download: %s\n", url);
return 1;
}

Expand Down Expand Up @@ -45,7 +45,7 @@ int fraction_parse(char *data, size_t size, fraction_t *fraction) {

// Ensure the data size is sufficient
if (size < HEADER_SIZE) {
fprintf(stderr, "Insufficient size: %lu\n", size);
log_error("Insufficient size: %lu\n", size);
return 1;

}
Expand All @@ -59,15 +59,15 @@ int fraction_parse(char *data, size_t size, fraction_t *fraction) {

// Check the magic number
if (!check_magic(magic)) {
fprintf(stderr, "Wrong magic number: %02x\n", magic);
log_error("Wrong magic number: %02x\n", magic);
return 1;
}

// Allocate memory for fraction data
data_size = size - HEADER_SIZE;
fraction->data = malloc(data_size);
if (!fraction->data) {
fprintf(stderr, "Failed to allocate data for fraction\n");
log_error("Failed to allocate data for fraction\n");
return 1;
}
// Set the extracted values in the fraction structure
Expand All @@ -93,15 +93,19 @@ int compare_fractions(const void *a, const void *b) {
}

void print_fraction(fraction_t fraction) {
printf("Magic: 0x%08x\n", fraction.magic);
printf("Index: %u\n", fraction.index);
printf("IV: ");
for (size_t i = 0; i < sizeof(fraction.iv); i++) {
printf("%02x ", (unsigned char)fraction.iv[i]);
}
printf("\n");
printf("CRC-32: 0x%08x\n", fraction.crc);
printf("Data size: %lu\n\n", fraction.data_size);
log_debug("Magic: 0x%08x\n", fraction.magic);
log_debug("Index: %u\n", fraction.index);
if (log_get_level() == LOG_DEBUG) {
char iv_str[sizeof(fraction.iv) * 3] = {
0}; // 2 characters for hex + 1 for space
for (size_t i = 0; i < sizeof(fraction.iv); i++) {
snprintf(iv_str + i * 3, 4, "%02x ", (unsigned char)fraction.iv[i]);
}
log_debug("IV: %s\n", iv_str);
}

log_debug("CRC-32: 0x%08x\n", fraction.crc);
log_debug("Data size: %lu\n\n", fraction.data_size);
}

int calc_crc(fraction_t *frac){
Expand All @@ -123,9 +127,9 @@ int calc_crc(fraction_t *frac){
uint32_t calculated_crc = crc32(buffer, offset);

if (calculated_crc != frac->crc) {
printf("Checksum incorrect\n");
printf("Checksum generated: %08X\n", calculated_crc);
printf("Checksum from fraction: %08X\n\n", frac->crc);
log_warn("Checksum incorrect\n");
log_warn("Checksum generated: %08X\n", calculated_crc);
log_warn("Checksum from fraction: %08X\n\n", frac->crc);
}

return calculated_crc == frac->crc;
Expand All @@ -135,7 +139,7 @@ int check_fractions(fraction_t *fraction, size_t size){
int res = 0;
for(size_t i = 0; i < size; i++){
if (!calc_crc(&fraction[i])) {
fprintf(stderr, "Failed to validate integrity of fraction:\n");
log_error("Failed to validate integrity of fraction:\n");
print_fraction(fraction[i]);
res += 1;
}
Expand Down
32 changes: 15 additions & 17 deletions client/src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ const char *POST_REQ_TEMPLATE = "POST %s HTTP/1.1\r\nContent-Type: "

/* Log a http_res_t */
void print_http_res(const http_res_t *res) {
printf("--[ STATUS CODE: %i ]--\n", res->status_code);
printf("--[ REQUEST ]--\n%s\n--[ REQUEST ]--\n", res->request);
// res->data is not null-terminated so use write here
write(1, res->data, res->size);
puts("--[ END ]--\n");
log_debug("[STATUS CODE: %i ]", res->status_code);
log_debug("[REQUEST]\n%s", res->request);
log_debug("[END REQUEST]\n");
}

/* Parse HTTP status code */
Expand Down Expand Up @@ -74,7 +72,7 @@ int parse_http_body(int sfd, char *src, char *dest, long content_length, long to

body_start = strstr(src, "\r\n\r\n");
if (body_start == NULL) {
perror("Header delimeter not found\n");
log_error("Header delimeter not found\n");
return HTTP_INVALID_RESPONSE;
}
body_start += 4;
Expand All @@ -88,7 +86,7 @@ int parse_http_body(int sfd, char *src, char *dest, long content_length, long to
if (content_length > received_length) {
left_length = content_length - received_length;
if (recv_response(sfd, dest + received_length, left_length) < 0) {
perror("Failed to receive left over data\n");
log_error("Failed to receive left over data\n");
return HTTP_SOCKET_ERR;
}
}
Expand Down Expand Up @@ -116,12 +114,12 @@ int http_post(int sfd, const char *path,
strncpy(res->request, req_buffer, req_buf_len - 1);

if (send_request(sfd, req_buffer) < 0) {
perror("Error: failed to send request\n");
log_error("Error: failed to send request\n");
return HTTP_SOCKET_ERR;
}

if (HTTP_VERBOSE)
puts("Sent POST request");

log_debug("Sent POST request\n");

/* Receive response from server */
total_bytes = 0;
Expand Down Expand Up @@ -170,8 +168,8 @@ int http_post(int sfd, const char *path,
return HTTP_INVALID_RESPONSE;
}

if (HTTP_VERBOSE)
puts("Parsed response");

log_debug("Parsed response\n");
if (HTTP_VERBOSE > 1)
print_http_res(res);

Expand All @@ -198,12 +196,12 @@ int http_get(int sfd, const char *path, http_res_t *res) {
req_buf_len = strlen(request_buf);

if (send_request(sfd, request_buf) < 0) {
perror("Error: failed to send request\n");
log_error("Error: failed to send request\n");
err = HTTP_SOCKET_ERR;
goto error;
}

VERBOSE_ONLY(puts("Sent GET request");)
log_debug("Sent GET request\n");

res->request = malloc(req_buf_len + 1);
if (res->request == NULL) {
Expand Down Expand Up @@ -237,7 +235,7 @@ int http_get(int sfd, const char *path, http_res_t *res) {

// by this time buf will be null terminated

VERBOSE_ONLY(puts("Received data from server");)
log_debug("Received data from server");

/* Check if response starts with "HTTP" */
if (memcmp(buf, "HTTP", 4)) {
Expand Down Expand Up @@ -273,9 +271,9 @@ int http_get(int sfd, const char *path, http_res_t *res) {
goto error;
}

VERBOSE_ONLY(puts("Parsed response");)
log_debug("Parsed response");

VERY_VERBOSE_ONLY(print_http_res(res);)
print_http_res(res);

return HTTP_SUCCESS;

Expand Down
Loading
Loading