Go to the documentation of this file.
18 #include <sys/types.h>
22 #include "../../include/utils/logger.h"
31 printf(
"failed to malloc thread_logger\n");
34 thl->
lock = pthread_mutex_lock;
35 thl->
unlock = pthread_mutex_unlock;
38 thl->
debug = with_debug;
39 pthread_mutex_init(&thl->
mutex, NULL);
53 printf(
"failed to malloc file_logger\n");
58 int file_descriptor = open(output_file, O_WRONLY | O_CREAT | O_SYNC | O_APPEND, 0640);
60 if (file_descriptor <= 0) {
65 printf(
"failed to run posix open function\n");
75 char *msg = calloc(
sizeof(
char), strlen(message) + 2);
77 printf(
"failed to calloc msg\n");
82 int response = write(file_descriptor, msg, strlen(msg));
84 printf(
"failed to write file log message");
97 va_start(
args, message);
98 char *msg = malloc(
sizeof(
args) + strlen(message) + 1);
102 int response = vsprintf(msg, message,
args);
105 printf(
"failed to vsprintf\n");
108 log_func(thl, file_descriptor, msg, level);
114 if (time_str == NULL) {
118 char *date_msg = calloc(
sizeof(
char), strlen(time_str) + strlen(message) + 2);
119 if (date_msg == NULL) {
120 printf(
"failed to calloc date_msg\n");
123 strcat(date_msg, time_str);
124 strcat(date_msg, message);
127 info_log(thl, file_descriptor, date_msg);
130 warn_log(thl, file_descriptor, date_msg);
133 error_log(thl, file_descriptor, date_msg);
136 debug_log(thl, file_descriptor, date_msg);
146 char *msg = calloc(
sizeof(
char), strlen(message) + strlen(
"[info - ") + (
size_t)2);
148 printf(
"failed to calloc info_log msg");
151 strcat(msg,
"[info - ");
152 strcat(msg, message);
153 if (file_descriptor != 0) {
164 char *msg = calloc(
sizeof(
char), strlen(message) + strlen(
"[warn - ") + (
size_t) 2);
166 printf(
"failed to calloc warn_log msg");
169 strcat(msg,
"[warn - ");
170 strcat(msg, message);
171 if (file_descriptor != 0) {
186 char *msg = calloc(
sizeof(
char), strlen(message) + strlen(
"[error - ") + (
size_t)2);
188 printf(
"failed to calloc error_log msg");
191 strcat(msg,
"[error - ");
192 strcat(msg, message);
193 if (file_descriptor != 0) {
203 if (thl->
debug ==
false) {
209 char *msg = calloc(
sizeof(
char), strlen(message) + strlen(
"[debug - ") + (
size_t) 2);
211 printf(
"failed to calloc debug_log msg");
214 strcat(msg,
"[debug - ");
215 strcat(msg, message);
216 if (file_descriptor != 0) {
236 strftime(date,
sizeof date,
"%b %d %r", localtime(&(time_t){time(NULL)}));
238 char *msg = calloc(
sizeof(
char),
sizeof(date) + 2);
240 printf(
"failed to calloc get_time_string\n");
void error_log(thread_logger *thl, int file_descriptor, char *message)
logs an error styled message - called by log_fn
void clear_thread_logger(thread_logger *thl)
free resources for the threaded logger
void info_log(thread_logger *thl, int file_descriptor, char *message)
logs an info styled message - called by log_fn
void clear_file_logger(file_logger *fhl)
free resources for the file ogger
int write_file_log(int file_descriptor, char *message)
used to write a log message to file
file_logger * new_file_logger(char *output_file, bool with_debug)
returns a new file_logger Calls new_thread_logger internally
char * get_time_string()
private function that returns a timestamp of format Jul 06 10:12:20 PM
void logf_func(thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *message,...)
like log_func but for formatted logs
void warn_log(thread_logger *thl, int file_descriptor, char *message)
logs a warned styled message - called by log_fn
void debug_log(thread_logger *thl, int file_descriptor, char *message)
logs a debug styled message - called by log_fn
void log_func(thread_logger *thl, int file_descriptor, char *message, LOG_LEVELS level)
main function you should call, which will delegate to the appopriate *_log function
void print_colored(COLORS color, char *message)
prints message to stdout with the given color
thread_logger * new_thread_logger(bool with_debug)
returns a new thread safe logger if with_debug is false, then all debug_log calls will be ignored