]> git.treefish.org Git - phys/latlib.git/blobdiff - writeout.cpp
...
[phys/latlib.git] / writeout.cpp
index 4fb68e12a64f5b0a795be0ad86c82037d05533ac..81f1e02a4359fcd615ae692b6430b13236e68dd3 100644 (file)
@@ -9,34 +9,82 @@
 #include <sstream>
 #include <dirent.h>
 #include <errno.h>
 #include <sstream>
 #include <dirent.h>
 #include <errno.h>
+#include <unistd.h>
 
 using namespace std;
 
 
 using namespace std;
 
-string writeout::tstamp()
+bool writeout::DirectoryExists( const char* pzPath )
 {
 {
-    stringstream sstr;
-    sstr << time (NULL);
-    return sstr.str();
+    if ( pzPath == NULL) return false;
+
+    DIR *pDir;
+    bool bExists = false;
+
+    pDir = opendir (pzPath);
+
+    if (pDir != NULL)
+    {
+        bExists = true;    
+        (void) closedir (pDir);
+    }
+
+    return bExists;
+}
+
+string writeout::tstamp(const long& timestamp)
+{
+  stringstream sstr;
+  if(!timestamp) sstr << time (NULL);
+  else sstr << timestamp;
+  return sstr.str();
 }
 
 writeout::writeout(const string& wdir, const string& signature, 
 }
 
 writeout::writeout(const string& wdir, const string& signature, 
-                  const int& rank, const int& procs)
+                  const int& rank, const int& procs, const long& timestamp)
 {
   if(wdir != ""){
     numprocs = procs;
     sprintf(cRank, "%d", rank);
 {
   if(wdir != ""){
     numprocs = procs;
     sprintf(cRank, "%d", rank);
-    fulldir = wdir + "/" + tstamp() + "_" + signature;
-    mkdir( fulldir.c_str(),  0775);
+    fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
+
+    for(int itry=0; itry<100 && (!DirectoryExists(fulldir.c_str())); itry++) {
+      if (rank==0) 
+       mkdir( fulldir.c_str(), 0775);
+      sleep(1);
+    }
 
     if(rank>0) of.open( (fulldir + "/rank" + cRank + ".tmp").c_str() );
     else of.open( (fulldir + "/" + signature + ".dat").c_str() );
 
 
     if(rank>0) of.open( (fulldir + "/rank" + cRank + ".tmp").c_str() );
     else of.open( (fulldir + "/" + signature + ".dat").c_str() );
 
+    logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
+
+    if ( (!of.is_open()) || (!logf.is_open()) ) {
+      cerr << "WRITEOUT: Could not open output- and/or log-file!" << endl << flush;
+      exit(1);
+    }
+
+    logf << "[ " << timestring() << " ] Log starts here." << endl;
+    
     buf = of.rdbuf();
     buf = of.rdbuf();
+    logbuf = logf.rdbuf();
+
+    cout << buf << endl;
   }
   else{
     buf = cout.rdbuf();
   }
   else{
     buf = cout.rdbuf();
+    logbuf = cerr.rdbuf();
   }
   out = new ostream(buf);
   }
   out = new ostream(buf);
+  log = new ostream(logbuf);
+}
+
+string writeout::timestring()
+{
+  time_t rawtime;
+  string timestring;
+  time( &rawtime );
+  timestring = asctime( localtime( &rawtime ) );
+  return timestring.substr(0, timestring.size()-1);;
 }
 
 writeout::~writeout()
 }
 
 writeout::~writeout()
@@ -52,23 +100,23 @@ writeout::~writeout()
              if( (nextfile=getdatfile()) == "" ) sleep(1);
              else
                {
              if( (nextfile=getdatfile()) == "" ) sleep(1);
              else
                {
-                 cerr << "collecting " << nextfile << endl;
+                 logf << "collecting " << nextfile << endl;
 
                  ifstream myfile( (fulldir + "/" + nextfile).c_str() );
 
                  ifstream myfile( (fulldir + "/" + nextfile).c_str() );
-                 while(myfile.good()){
+                 while(true){
                    string line;
                    getline(myfile, line);
                    string line;
                    getline(myfile, line);
-                   of << line << flush;
+                   if( !myfile.good() ) break;
+                   of << line << endl << flush;
                  }
                  myfile.close();
                  }
                  myfile.close();
-                 of << endl << flush;
-
                  remove( (fulldir + "/" + nextfile).c_str() );
                  jobsdone++;
                }
            }
          of << "#end" << endl << flush;
          of.close();
                  remove( (fulldir + "/" + nextfile).c_str() );
                  jobsdone++;
                }
            }
          of << "#end" << endl << flush;
          of.close();
+         rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
        }
       else
        {
        }
       else
        {
@@ -77,6 +125,8 @@ writeout::~writeout()
                 (fulldir + "/rank" + cRank + ".part").c_str());
        }
     }
                 (fulldir + "/rank" + cRank + ".part").c_str());
        }
     }
+  logf << "[ " << timestring() << " ] Log ends here." << endl;
+  logf.close();
 }
 
 string writeout::getdatfile()
 }
 
 string writeout::getdatfile()
@@ -86,7 +136,7 @@ string writeout::getdatfile()
   struct dirent *dirp;
 
   if((dp  = opendir(fulldir.c_str())) == NULL) {
   struct dirent *dirp;
 
   if((dp  = opendir(fulldir.c_str())) == NULL) {
-    cerr << "Error(" << errno << ") opening " << fulldir << endl;
+    logf << "Error(" << errno << ") opening " << fulldir << endl;
     return "";
   }
   
     return "";
   }