ulogger
athreadsafeloggerwithcoloredoutput
|
Go to the documentation of this file.
35 #include <sys/types.h>
51 printf(
"failed to malloc thread_logger\n");
55 thl->
lock = pthread_mutex_lock;
56 thl->
unlock = pthread_mutex_unlock;
59 thl->
debug = with_debug;
60 pthread_mutex_init(&thl->
mutex, NULL);
82 printf(
"failed to malloc file_logger\n");
89 open(output_file, O_WRONLY | O_CREAT | O_SYNC | O_APPEND, 0640);
90 if (file_descriptor <= 0) {
95 printf(
"failed to run posix open function\n");
99 fhl->
fd = file_descriptor;
114 char msg[strlen(message) + 2];
115 memset(msg, 0,
sizeof(msg));
117 strcat(msg, message);
120 int response = write(file_descriptor, msg, strlen(msg));
121 if (response == -1) {
122 printf(
"failed to write file log message");
143 int line,
char *message, ...) {
146 va_start(args, message);
147 char msg[
sizeof(args) + (strlen(message) * 2)];
148 memset(msg, 0,
sizeof(msg));
150 int response = vsnprintf(msg,
sizeof(msg), message, args);
153 printf(
"failed to vsprintf\n");
157 log_func(thl, file_descriptor, msg, level, file, line);
172 memset(time_str, 0,
sizeof(time_str));
176 char location_info[strlen(file) +
sizeof(line) + 4];
177 memset(location_info, 0,
sizeof(location_info));
179 sprintf(location_info,
" %s:%i", file, line);
182 date_msg[strlen(time_str) + strlen(message) + 2 +
sizeof(location_info) + 6];
183 memset(date_msg, 0,
sizeof(date_msg));
185 strcat(date_msg, time_str);
186 strcat(date_msg,
" -");
187 strcat(date_msg, location_info);
188 strcat(date_msg,
"] ");
189 strcat(date_msg, message);
193 info_log(thl, file_descriptor, date_msg);
196 warn_log(thl, file_descriptor, date_msg);
199 error_log(thl, file_descriptor, date_msg);
202 debug_log(thl, file_descriptor, date_msg);
216 size_t msg_size = strlen(message) + strlen(
"[info - ") + 2;
218 memset(msg, 0,
sizeof(msg));
222 strcat(msg,
"[info - ");
223 strcat(msg, message);
225 if (file_descriptor != 0) {
243 size_t msg_size = strlen(message) + strlen(
"[warn - ") + 2;
245 memset(msg, 0,
sizeof(msg));
249 strcat(msg,
"[warn - ");
250 strcat(msg, message);
252 if (file_descriptor != 0) {
270 size_t msg_size = strlen(message) + strlen(
"[error - ") + 2;
272 memset(msg, 0,
sizeof(msg));
276 strcat(msg,
"[error - ");
277 strcat(msg, message);
279 if (file_descriptor != 0) {
296 if (thl->
debug ==
false) {
301 size_t msg_size = strlen(message) + strlen(
"[debug - ") + 2;
303 memset(msg, 0,
sizeof(msg));
307 strcat(msg,
"[debug - ");
308 strcat(msg, message);
310 if (file_descriptor != 0) {
324 pthread_mutex_lock(&thl->
mutex);
325 pthread_mutex_destroy(&thl->
mutex);
348 strftime(date_buffer, date_buffer_len,
"%b %d %r",
349 localtime(&(time_t){time(NULL)}));
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
a thread safe logger with optional printf style logging
mutex_fn lock
used for synchronization across threads
void clear_file_logger(file_logger *fhl)
free resources for the file ogger
thread_logger * thl
the file descriptor used for sending log information to
void get_time_string(char *date_buffer, size_t date_buffer_len)
returns a timestamp of format Jul 06 10:12:20 PM
mutex_fn unlock
helper function for pthread_mutex_lock
log_fn log
helper function for pthread_mutex_unlock
log_fnf logf
function that gets called for all regular logging
void log_func(thread_logger *thl, int file_descriptor, char *message, LOG_LEVELS level, char *file, int line)
main function you should call, which will delegate to the appopriate *_log function
pthread_mutex_t mutex
indicates whether we will action on debug logs
int write_file_log(int file_descriptor, char *message)
used to write a log message to file although this really means a file descriptor
void logf_func(thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *file, int line, char *message,...)
like log_func but for formatted logs
file_logger * new_file_logger(char *output_file, bool with_debug)
returns a new file_logger Calls new_thread_logger internally
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 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