From 7563c1ad593f47f1df39c42e25617a23cc14126e Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Fri, 10 Jan 2025 20:51:19 +0530 Subject: [PATCH] update --- include/b_list.h | 8 ++++++ include/b_string.h | 14 ++++++++++ include/bencode.h | 6 ++++ include/log.h | 20 ++++++++++++++ src/b_string.c | 31 +++++++++++++++++++++ src/log.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 148 insertions(+) create mode 100644 include/b_list.h create mode 100644 include/b_string.h create mode 100644 include/bencode.h create mode 100644 include/log.h create mode 100644 src/b_string.c create mode 100644 src/log.c diff --git a/include/b_list.h b/include/b_list.h new file mode 100644 index 0000000..c7e5b7c --- /dev/null +++ b/include/b_list.h @@ -0,0 +1,8 @@ +#ifndef B_LIST +#define B_LIST + +#include + + + +#endif \ No newline at end of file diff --git a/include/b_string.h b/include/b_string.h new file mode 100644 index 0000000..bb21d88 --- /dev/null +++ b/include/b_string.h @@ -0,0 +1,14 @@ +#ifndef B_STRING +#define B_STRING + +#include + +typedef struct b_string_{ + size_t len; + unsigned char str[]; +}b_string_t; + +b_string_t *alloc_b_string(size_t len, const char* str); +void free_b_string(b_string_t *b_str); + +#endif \ No newline at end of file diff --git a/include/bencode.h b/include/bencode.h new file mode 100644 index 0000000..919e0d7 --- /dev/null +++ b/include/bencode.h @@ -0,0 +1,6 @@ +#ifndef BENCODE_H +#define BENCODE_H + + + +#endif \ No newline at end of file diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..8ec8db8 --- /dev/null +++ b/include/log.h @@ -0,0 +1,20 @@ +#ifndef B_LOG +#define B_LOG + +#include + +typedef enum{ + DEBUG_LOG, + INFO_LOG, + WARNING_LOG, + ERROR_LOG, + NO_LOG +}log_type_t; + +#define DEFAULT_LOG_TYPE INFO_LOG + +void log_set_type(log_type_t typ); +void log_set_logfile(FILE *f); +void log_printf(log_type_t typ, const char *fmt, ...); + +#endif \ No newline at end of file diff --git a/src/b_string.c b/src/b_string.c new file mode 100644 index 0000000..41f9c96 --- /dev/null +++ b/src/b_string.c @@ -0,0 +1,31 @@ +#include "b_string.h" + +#include +#include + +b_string_t *alloc_b_string(size_t len, const char* str) +{ + b_string_t *b_str; + b_str = malloc(sizeof(b_string_t) + len + 1); + + if(b_str) + { + memcpy(b_str->str, str, len); + /*NULL-terminate the string to indicate + * end of string. + */ + + b_str->str[len] = '\0'; + b_str->len = len; + } + + return b_str; +} + + + +void free_b_string(b_string_t *b_str) +{ + free(b_str); +} + diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..7c68e1d --- /dev/null +++ b/src/log.c @@ -0,0 +1,69 @@ +#include "log.h" + +#include +#include +#include + +#include +#include + +/* +* This declaration of pthread mutex allow to +* initialize the mutex statically, for quick +* use. It is an alternate for pthread_mutex_init() +* function. +*/ +static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER; +static int log_type = DEFAULT_LOG_TYPE; +static FILE *logfile = NULL; + +void log_set_type(log_type_t typ) +{ + pthread_mutex_lock(&log_lock); + log_type = typ; + pthread_mutex_unlock(&log_lock); +} + + +void log_set_logfile(FILE *f) +{ + pthread_mutex_lock(&log_lock); + logfile = f; + pthread_mutex_unlock(&log_lock); +} + +void log_printf(log_type_t typ, const char *fmt, ...) +{ + va_list args; + long tid = (long)syscall(SYS_gettid); + time_t now = time(0); + char timestr[9]; + + strftime(timestr, sizeof(timestr), "%H:%M:%S", localtime(&now)); + + pthread_mutex_lock(&log_lock); + + if( typ < log_type ) + { + pthread_mutex_lock(&log_lock); + return; + } + + fprintf(logfile, "[%.*s] [%05ld] ", 8, timestr, tid); + switch(typ){ + case WARNING_LOG: + fprintf(logfile, "WARNING: "); + break; + case ERROR_LOG: + fprintf(logfile, "ERROR: "); + break; + } + + va_start(args, fmt); + vfprintf(logfile, fmt, args); + va_end(args); + + pthread_mutex_unlock(&log_lock); + + fflush(logfile); +} \ No newline at end of file