14 #include <boost/filesystem.hpp>
18 string writeout::tstamp(const long& timestamp)
21 if(!timestamp) sstr << time (NULL);
22 else sstr << timestamp;
26 void writeout::newsub(string subname) {
27 of[subname] = new ofstream;
29 if ( fulldir != "" ) {
30 if(rank>0) of[subname]->open( (fulldir + "/rank" + cRank + "-" + subname + ".tmp").c_str() );
31 else of[subname]->open( (fulldir + "/" + signature + "-" + subname + ".dat").c_str() );
33 if ( !of[subname]->is_open() ) {
34 logf << "WRITEOUT: Could not open output-file!" << endl << flush;
38 buf[subname] = of[subname]->rdbuf();
41 buf[subname] = cout.rdbuf();
43 out[subname] = new ostream(buf[subname]);
46 writeout::writeout(const string& wdir, const string& _signature,
47 const int& _rank, const int& procs, const long& timestamp)
50 signature = _signature;
55 sprintf(cRank, "%d", rank);
56 fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
59 mkdir(fulldir.c_str(), 0775);
61 while ( ! ( boost::filesystem::exists(fulldir) && boost::filesystem::is_directory(fulldir) ) ) {
62 cerr << "WRITOUT: Waiting for rank 0 to create out directory." << endl << flush;
67 logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
69 if ( !logf.is_open() ) {
70 cerr << "WRITEOUT: Could not open log-file!" << endl << flush;
74 logf << "[ " << timestring() << " ] Log starts here." << endl;
76 logbuf = logf.rdbuf();
79 logbuf = cerr.rdbuf();
81 log = new ostream(logbuf);
84 string writeout::timestring()
89 timestring = asctime( localtime( &rawtime ) );
90 return timestring.substr(0, timestring.size()-1);;
96 for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
97 if( cRank[0] == '0' ) {
99 while(jobsdone<numprocs-1) {
101 if( (nextfile = getdatfile(ofit->first)) == "" )
104 logf << "collecting " << nextfile << endl;
106 ifstream myfile( (fulldir + "/" + nextfile).c_str() );
109 getline(myfile, line);
110 if( !myfile.good() ) break;
111 *ofit->second << line << endl << flush;
114 remove( (fulldir + "/" + nextfile).c_str() );
118 *ofit->second << "#end" << endl << flush;
119 ofit->second->close();
122 ofit->second->close();
123 rename((fulldir + "/rank" + cRank + "-" + ofit->first + ".tmp").c_str(),
124 (fulldir + "/rank" + cRank + "-" + ofit->first + ".part").c_str());
127 if( cRank[0] == '0' )
128 rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
130 logf << "[ " << timestring() << " ] Log ends here." << endl;
134 string writeout::getdatfile(string subname)
140 if((dp = opendir(fulldir.c_str())) == NULL) {
141 logf << "Error(" << errno << ") opening " << fulldir << endl;
146 while ((dirp = readdir(dp)) != NULL)
148 myfile = string(dirp->d_name);
150 if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part" &&
151 subname == myfile.substr( myfile.find("-")+1, myfile.rfind(".")-myfile.find("-")-1 ) ) {