slog in a separate thread
authorJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 7 Sep 2012 07:48:40 +0000 (07:48 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 7 Sep 2012 07:48:40 +0000 (07:48 +0000)
src/server/server.c

index bdb785c..afe5a3c 100644 (file)
@@ -306,11 +306,33 @@ static int cbk_http_request(void *cls,
        return ret;
 }
 
+static void *slog_routine(void *data)
+{
+       char *path;
+
+       path = (char *)data;
+
+       log_debug("slog_routine");
+
+       slog_init(path, server_data.sensors);
+
+       while (1) {
+               pthread_mutex_lock(&mutex);
+               slog_write_sensors(server_data.sensors);
+               pthread_mutex_unlock(&mutex);
+
+               sleep(5);
+       }
+
+       pthread_exit(0);
+}
+
 int main(int argc, char *argv[])
 {
        struct MHD_Daemon *d;
-       int port, opti, optc, cmdok;
+       int port, opti, optc, cmdok, ret;
        char *log_file, *slog_file;
+       pthread_t slog_thread;
 
        program_name = argv[0];
 
@@ -404,8 +426,14 @@ int main(int argc, char *argv[])
        log_info(_("WWW directory: %s"), server_data.www_dir);
        log_info(_("URL: http://localhost:%d"), port);
 
-       if (slog_file)
-               slog_init(slog_file, server_data.sensors);
+       if (slog_file) {
+               ret = pthread_create(&slog_thread,
+                                    NULL,
+                                    slog_routine,
+                                    slog_file);
+               if (ret)
+                       log_err(_("Failed to create sensor log thread."));
+       }
 
        while (!server_stop_requested) {
                pthread_mutex_lock(&mutex);
@@ -418,8 +446,6 @@ int main(int argc, char *argv[])
 
                psensor_log_measures(server_data.sensors);
 
-               slog_write_sensors(server_data.sensors);
-
                pthread_mutex_unlock(&mutex);
                sleep(5);
        }