From f492b4d378451c5d8031ee22cc485360f5451f6b Mon Sep 17 00:00:00 2001 From: Jake Drahos <j@kedrahos.com> Date: Sun, 16 Oct 2016 20:46:30 -0500 Subject: [PATCH] Made logger a little bit more palatable - Thread safety - Some unignorable fixes in the filename creation. - That part is still terribad --- .gitignore | 3 +- src/logger.c | 77 +++++++++++++++++++++++++++++++++++++-------- src/logger.h | 3 +- src/microcart_cli.c | 7 ++++- 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 27e34144..367bf3b3 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,5 @@ src/vrpn/build* src/vrpn/pc_linux64/* #Exacutables -./BlueTooth +BlueTooth +logs diff --git a/src/logger.c b/src/logger.c index 1d8d5896..028cfeac 100644 --- a/src/logger.c +++ b/src/logger.c @@ -4,21 +4,30 @@ */ #include "logger.h" #include <stdio.h> +#include <err.h> +#include <pthread.h> -int quadlog_file; +static FILE * quadlog_file = NULL; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int createLogFile(int argc, char* argv) { - char log_file[300] = {'l', 'o', 'g','s', '/'}; + if (quadlog_file != NULL) { + return -1; + } + + if (pthread_mutex_lock(&mutex)) { + err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__); + } + + char log_file[300]; + strcpy(log_file, "logs/"); if(argc >= 2) { - strcat(log_file, argv); + strncat(log_file, argv, 294); printf("Creating log file '%s'...\n",log_file); - quadlog_file = open(log_file, O_WRONLY | O_CREAT, 0666); - return quadlog_file; - } - else - { + quadlog_file = fopen(log_file, "a"); + } else { time_t rawtime; char timestr [30]; time(&rawtime); @@ -26,7 +35,7 @@ int createLogFile(int argc, char* argv) // Lets convert space to _ in char *p = timestr; - int i = 0; + size_t i = 0; while(i < strlen(timestr)) { if (*p == ' ') @@ -40,18 +49,60 @@ int createLogFile(int argc, char* argv) strncat(log_file, timestr, strlen(timestr) -1 ); strcat(log_file, ".txt"); printf("Creating log file '%s'...\n",log_file); - quadlog_file = open(log_file, O_WRONLY | O_CREAT, 0666); - return quadlog_file; + quadlog_file = fopen(log_file, "a"); + } + if (pthread_mutex_unlock(&mutex)) { + err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__); } + return 0; } int updateLogFile(const struct ucart_vrpn_TrackerData * td) { - return dprintf(quadlog_file, "FPS: %lf Pos (xyz): (%lf %lf %lf) Att (pry): (%lf %lf %lf)\n", + int retval; + + if (pthread_mutex_lock(&mutex)) { + err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__); + } + + retval = fprintf(quadlog_file, + "FPS: %lf Pos (xyz): (%lf %lf %lf) Att (pry): (%lf %lf %lf)\n", td->fps, td->x, td->y, td->z, td->pitch, td->roll, td->yaw); + + if (pthread_mutex_unlock(&mutex)) { + err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__); + } + + return retval; } int writeStringToLog(const char * string) { - return dprintf(quadlog_file, "%s", string); + int retval; + + if (pthread_mutex_lock(&mutex)) { + err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__); + } + + retval = fprintf(quadlog_file, "%s", string); + + if (pthread_mutex_unlock(&mutex)) { + err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__); + } + + return retval; +} + +void closeLogFile(void) +{ + if (pthread_mutex_lock(&mutex)) { + err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__); + } + + fclose(quadlog_file); + + if (pthread_mutex_unlock(&mutex)) { + err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__); + } + } diff --git a/src/logger.h b/src/logger.h index 5b5ae754..edfb365f 100644 --- a/src/logger.h +++ b/src/logger.h @@ -15,6 +15,7 @@ int createLogFile(int, char*); int writeStringToLog(const char*); int updateLogFile(const struct ucart_vrpn_TrackerData* ); +void closeLogFile(); -#endif \ No newline at end of file +#endif diff --git a/src/microcart_cli.c b/src/microcart_cli.c index 40224482..55b30fdd 100644 --- a/src/microcart_cli.c +++ b/src/microcart_cli.c @@ -43,6 +43,11 @@ int safe_fd_clr(int , fd_set* , int* ); static void cb(struct ucart_vrpn_TrackerData *); +/* Thread-safe wrappers */ +pthread_mutex_t quadSocketMutex, logFileMutex; +ssize_t writeQuad(const char * buf, size_t count); +ssize_t dprintfLog(const char * fmt, ...); + // global variables static volatile int keepRunning = 1; const char *TRACKER_IP = "UAV@192.168.0.120:3883"; @@ -329,4 +334,4 @@ int safe_fd_clr(int fd, fd_set* fds, int* max_fd) { (*max_fd)--; } return 0; -} \ No newline at end of file +} -- GitLab