]> git.treefish.org Git - phys/latlib.git/blob - writeout.cpp
...
[phys/latlib.git] / writeout.cpp
1 #include "writeout.h"
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <iostream>
8 #include <time.h>
9 #include <sstream>
10 #include <dirent.h>
11 #include <errno.h>
12 #include <unistd.h>
13
14 using namespace std;
15
16 string writeout::tstamp(const long& timestamp)
17 {
18   stringstream sstr;
19   if(!timestamp) sstr << time (NULL);
20   else sstr << timestamp;
21   return sstr.str();
22 }
23
24 writeout::writeout(const string& wdir, const string& signature, 
25                    const int& rank, const int& procs, const long& timestamp)
26 {
27   if(wdir != ""){
28     numprocs = procs;
29     sprintf(cRank, "%d", rank);
30     fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
31     mkdir( fulldir.c_str(),  0775);
32
33     if(rank>0) of.open( (fulldir + "/rank" + cRank + ".tmp").c_str() );
34     else of.open( (fulldir + "/" + signature + ".dat").c_str() );
35
36     logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
37
38     logf << "[ " << timestring() << " ] Log starts here." << endl;
39     
40     buf = of.rdbuf();
41     logbuf = logf.rdbuf();
42   }
43   else{
44     buf = cout.rdbuf();
45     logbuf = cerr.rdbuf();
46   }
47   out = new ostream(buf);
48   log = new ostream(logbuf);
49 }
50
51 string writeout::timestring()
52 {
53   time_t rawtime;
54   string timestring;
55   time( &rawtime );
56   timestring = asctime( localtime( &rawtime ) );
57   return timestring.substr(0, timestring.size()-1);;
58 }
59
60 writeout::~writeout()
61 {
62   if(fulldir != "")
63     {
64       if( cRank[0] == '0' )
65         {
66           int jobsdone=0;
67           while(jobsdone<numprocs-1)
68             {
69               string nextfile;
70               if( (nextfile=getdatfile()) == "" ) sleep(1);
71               else
72                 {
73                   cerr << "collecting " << nextfile << endl;
74
75                   ifstream myfile( (fulldir + "/" + nextfile).c_str() );
76                   while(true){
77                     string line;
78                     getline(myfile, line);
79                     if( !myfile.good() ) break;
80                     of << line << endl << flush;
81                   }
82                   myfile.close();
83                   remove( (fulldir + "/" + nextfile).c_str() );
84                   jobsdone++;
85                 }
86             }
87           of << "#end" << endl << flush;
88           of.close();
89           rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
90         }
91       else
92         {
93           of.close();
94           rename((fulldir + "/rank" + cRank + ".tmp").c_str(),
95                  (fulldir + "/rank" + cRank + ".part").c_str());
96         }
97     }
98   logf << "[ " << timestring() << " ] Log ends here." << endl;
99   logf.close();
100 }
101
102 string writeout::getdatfile()
103 {
104   string myfile;
105   DIR *dp;
106   struct dirent *dirp;
107
108   if((dp  = opendir(fulldir.c_str())) == NULL) {
109     cerr << "Error(" << errno << ") opening " << fulldir << endl;
110     return "";
111   }
112   
113   while ((dirp = readdir(dp)) != NULL)
114     {
115       myfile = string(dirp->d_name);
116       if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part")
117         return myfile;
118     }
119
120   return "";
121 }