Update SMACK, Fix a crash of terminating sequence, etc, ...
[apps/livebox/data-provider-master.git] / src / critical_log.c
index 7469013..75a9796 100644 (file)
@@ -23,6 +23,7 @@
 #include <libgen.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <pthread.h>
 
 #include <dlog.h>
 #include <Eina.h>
@@ -38,11 +39,13 @@ static struct {
        int file_id;
        int nr_of_lines;
        char *filename;
+       pthread_mutex_t cri_lock;
 } s_info = {
        .fp = NULL,
        .file_id = 0,
        .nr_of_lines = 0,
        .filename = NULL,
+       .cri_lock = PTHREAD_MUTEX_INITIALIZER,
 };
 
 
@@ -62,8 +65,10 @@ static inline void rotate_log(void)
        if (filename) {
                snprintf(filename, namelen, "%s/%d_%s.%d", SLAVE_LOG_PATH, s_info.file_id, s_info.filename, getpid());
 
-               if (s_info.fp)
-                       fclose(s_info.fp);
+               if (s_info.fp) {
+                       if (fclose(s_info.fp) != 0)
+                               ErrPrint("fclose: %s\n", strerror(errno));
+               }
 
                s_info.fp = fopen(filename, "w+");
                if (!s_info.fp)
@@ -81,10 +86,15 @@ HAPI int critical_log(const char *func, int line, const char *fmt, ...)
 {
        va_list ap;
        int ret;
+       int status;
 
        if (!s_info.fp)
                return LB_STATUS_ERROR_IO;
 
+       status = pthread_mutex_lock(&s_info.cri_lock);
+       if (status != 0)
+               ErrPrint("lock: %s\n", strerror(status));
+
        fprintf(s_info.fp, "%lf [%s:%d] ", util_timestamp(), util_basename((char *)func), line);
 
        va_start(ap, fmt);
@@ -95,6 +105,11 @@ HAPI int critical_log(const char *func, int line, const char *fmt, ...)
 
        s_info.nr_of_lines++;
        rotate_log();
+
+       status = pthread_mutex_unlock(&s_info.cri_lock);
+       if (status != 0)
+               ErrPrint("unlock: %s\n", strerror(status));
+
        return ret;
 }
 
@@ -149,7 +164,8 @@ HAPI void critical_log_fini(void)
        }
 
        if (s_info.fp) {
-               fclose(s_info.fp);
+               if (fclose(s_info.fp) != 0)
+                       ErrPrint("fclose: %s\n", strerror(errno));
                s_info.fp = NULL;
        }
 }