]> pilppa.org Git - lib1wire.git/blobdiff - src/W1Store.cc
Initial support for reading log-data.
[lib1wire.git] / src / W1Store.cc
index a6bbaa0b1be78a7ee650fcfa4e7d5061c20139dd..f40623e6c3a9ff3b2268d984a68d871727dc6be7 100644 (file)
@@ -7,7 +7,6 @@
 
 #include <list>
 #include <string>
-#include <iostream>
 #include <fstream>
 
 #include <time.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <plp/log.h>
+
+#include "W1Configure.hh"
 #include "W1Store.hh"
+#include "W1Util.hh"
 
 using namespace std;
 using namespace w1;
 
-std::string W1Store::location = "/tmp/";
+#define DIR_BUFFER_SIZE        20
+
+std::string W1Store::location_base_dir = DEFAULT_STORAGE_BASE_DIR;
+
 
 W1Store::W1Store() {
        // TODO Auto-generated constructor stub
@@ -31,66 +37,80 @@ W1Store::~W1Store() {
        // TODO Auto-generated destructor stub
 }
 
-void W1Store::set_location(string location_param) {
-       string::size_type pos;
+void W1Store::set_location_base_dir(string location_param) {
+       int     pos;
+       int     b_count;
 
-       location        = location_param;
-       pos             = location.find_last_of("/");
-       if (pos != location.length()) {
-               location        = location + "/";
+       pos     = location_param.find_last_of("/");
+       b_count = location_param.length();
+       if (pos == (b_count - 1)) {
+               location_base_dir       = location_param;
        }
+       else {
+               location_base_dir       = location_param + "/";
+       }
+}
 
+string W1Store::get_location_base_dir() {
+       return location_base_dir;
 }
 
-void W1Store::store(std::string device_id, std::list<std::string> *string_list) {
+string W1Store::get_location_dir(string device_id, struct tm *ltime) {
+       char    buffer[DIR_BUFFER_SIZE];
+       string  year;
+       string  month;
+       string  ret_val;
+
+       strftime(buffer, DIR_BUFFER_SIZE, "%Y", ltime);
+       year    = buffer;
+       strftime(buffer, DIR_BUFFER_SIZE, "%m", ltime);
+       month   = buffer;
+       ret_val = W1Util::concat_paths(location_base_dir, device_id);
+       ret_val = ret_val + "/" + year + "/" + month;
+       return ret_val;
+}
 
-       string file_path;
-       string text_line;
+string W1Store::get_location_file(string device_id, struct tm *ltime) {
+       char    buffer[DIR_BUFFER_SIZE];
+       string  ret_val;
 
+       strftime(buffer, DIR_BUFFER_SIZE, "%Y-%m-%d", ltime);
+       ret_val = get_location_dir(device_id, ltime);
+       ret_val = ret_val + "/" + buffer + DATAFILE_SUFFIX;
+       return ret_val;
+}
+
+void W1Store::store(std::string device_id, std::list<std::string> *string_list) {
+       string          f_path;
+       string          line;
        time_t          wtime;
        struct tm       *ltime;
-       char            buffer[80];
-       string          year;
-       string          month;
-       string          date;
+       ofstream        *ostream;
 
        time(&wtime);
        ltime   = localtime(&wtime);
-       strftime(buffer, 80, "%Y", ltime);
-       year    = buffer;
-       strftime(buffer, 80, "%m", ltime);
-       month   = buffer;
-       strftime(buffer, 80, "%Y-%m-%d", ltime);
-       date    = buffer;
-
-       struct tm * gmtime(const time_t *timer);
-       struct tm * localtime(const time_t * timer);
-       struct stat st;
-
-       file_path       = location + year;
-       if (stat(file_path.c_str() ,&st) != 0) {
-               mkdir(file_path.c_str(), 0755);
-       }
-       file_path       = file_path + "/" + month;
-       if (stat(file_path.c_str() ,&st) != 0) {
-               mkdir(file_path.c_str(), 0755);
-       }
-       file_path       = file_path + "/" + device_id + "_" + date + ".txt";
-       ofstream data_file(file_path.c_str(), ios::app);
-       cout << "storing to " << file_path << ", data size " << string_list->size() << endl;
+       f_path  = get_location_file(device_id, ltime);
+       ostream = W1Util::open_for_writing(f_path.c_str());
        // TODO: add mutex to protect string_list while it's read and emptied
-       if (data_file.is_open()) {
-               while(string_list->size() > 0) {
-                       text_line       = string_list->front();
-                       string_list->pop_front();
-                       if (text_line.length() > 0) {
-                               cout << "storing line: " << text_line << endl;
-                               data_file << text_line << endl;
+       if (ostream != NULL) {
+               if (ostream->is_open()) {
+                       log_info("[%s] writing %d data values to file: %s\n", device_id.c_str(), string_list->size(), f_path.c_str());
+                       while(string_list->size() > 0) {
+                               line    = string_list->front();
+                               string_list->pop_front();
+                               if (line.length() > 0) {
+                                       log_debug("storing line: %s\n", line.c_str());
+                                       *ostream << line << endl;
+                               }
                        }
+                       ostream->close();
+               }
+               else {
+                       log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
                }
-               data_file.close();
+               delete(ostream);
        }
        else {
-               cout << "could not open file " << file_path << " for writing data." << endl;
+               log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
        }
 }