]> 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 void writeout::newsub(string subname) {
25   of[subname] = new ofstream;
26
27   if ( fulldir != "" ) {
28     if(rank>0) of[subname]->open( (fulldir + "/rank" + cRank + "-" + subname + ".tmp").c_str() );
29     else of[subname]->open( (fulldir + "/" + signature + "-" + subname + ".dat").c_str() );
30
31     if ( !of[subname]->is_open() ) {
32       logf << "WRITEOUT: Could not open output-file!" << endl << flush;
33       exit(1);
34     }
35
36     buf[subname] = of[subname]->rdbuf();
37   }
38   else
39     buf[subname] = cout.rdbuf();
40
41   out[subname] = new ostream(buf[subname]);
42 }
43
44 writeout::writeout(const string& wdir, const string& _signature, 
45                    const int& _rank, const int& procs, const long& timestamp)
46 {
47   fulldir = "";
48   signature = _signature;
49   rank = _rank;
50
51   if(wdir != ""){
52     numprocs = procs;
53     sprintf(cRank, "%d", rank);
54     fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
55
56     mkdir(fulldir.c_str(), 0775);
57
58     logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
59
60     if ( !logf.is_open() ) {
61       cerr << "WRITEOUT: Could not open log-file!" << endl << flush;
62       exit(1);
63     }
64
65     logf << "[ " << timestring() << " ] Log starts here." << endl;
66     
67     logbuf = logf.rdbuf();
68   }
69   else{
70     logbuf = cerr.rdbuf();
71   }
72   log = new ostream(logbuf);
73 }
74
75 string writeout::timestring()
76 {
77   time_t rawtime;
78   string timestring;
79   time( &rawtime );
80   timestring = asctime( localtime( &rawtime ) );
81   return timestring.substr(0, timestring.size()-1);;
82 }
83
84 writeout::~writeout()
85 {
86   if(fulldir != "") {
87     for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
88       if( cRank[0] == '0' ) {
89         int jobsdone=0;
90         while(jobsdone<numprocs-1) {
91           string nextfile;
92           if( (nextfile = getdatfile(ofit->first)) == "" ) 
93             sleep(1);
94           else {
95             logf << "collecting " << nextfile << endl;
96                     
97             ifstream myfile( (fulldir + "/" + nextfile).c_str() );
98             while(true) {
99               string line;
100               getline(myfile, line);
101               if( !myfile.good() ) break;
102               *ofit->second << line << endl << flush;
103             }
104             myfile.close();
105             remove( (fulldir + "/" + nextfile).c_str() );
106             jobsdone++;
107           }
108         }
109         *ofit->second << "#end" << endl << flush;
110         ofit->second->close();
111       }
112       else {
113         ofit->second->close();
114         rename((fulldir + "/rank" + cRank + "-" + ofit->first + ".tmp").c_str(),
115                (fulldir + "/rank" + cRank + "-" + ofit->first + ".part").c_str());
116       }
117     }
118     if( cRank[0] == '0' )
119       rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
120   }
121   logf << "[ " << timestring() << " ] Log ends here." << endl;
122   logf.close();
123 }
124
125 string writeout::getdatfile(string subname)
126 {
127   string myfile;
128   DIR *dp;
129   struct dirent *dirp;
130
131   if((dp  = opendir(fulldir.c_str())) == NULL) {
132     logf << "Error(" << errno << ") opening " << fulldir << endl;
133     return "";
134   }
135   
136   while ((dirp = readdir(dp)) != NULL)
137     {
138       myfile = string(dirp->d_name);
139
140       if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part" &&
141          subname == myfile.substr( myfile.find("-")+1, myfile.rfind(".")-myfile.find("-")-1 ) ) {
142         return myfile;
143       }
144     }
145
146   return "";
147 }