16 string writeout::tstamp(const long& timestamp)
19 if(!timestamp) sstr << time (NULL);
20 else sstr << timestamp;
24 void writeout::newsub(string subname) {
25 of[subname] = new ofstream;
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() );
31 if ( !of[subname]->is_open() ) {
32 logf << "WRITEOUT: Could not open output-file!" << endl << flush;
36 buf[subname] = of[subname]->rdbuf();
39 buf[subname] = cout.rdbuf();
41 out[subname] = new ostream(buf[subname]);
44 writeout::writeout(const string& wdir, const string& _signature,
45 const int& _rank, const int& procs, const long& timestamp)
48 signature = _signature;
53 sprintf(cRank, "%d", rank);
54 fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
56 mkdir(fulldir.c_str(), 0775);
58 logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
60 if ( !logf.is_open() ) {
61 cerr << "WRITEOUT: Could not open log-file!" << endl << flush;
65 logf << "[ " << timestring() << " ] Log starts here." << endl;
67 logbuf = logf.rdbuf();
70 logbuf = cerr.rdbuf();
72 log = new ostream(logbuf);
75 string writeout::timestring()
80 timestring = asctime( localtime( &rawtime ) );
81 return timestring.substr(0, timestring.size()-1);;
87 for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
88 if( cRank[0] == '0' ) {
90 while(jobsdone<numprocs-1) {
92 if( (nextfile = getdatfile(ofit->first)) == "" )
95 logf << "collecting " << nextfile << endl;
97 ifstream myfile( (fulldir + "/" + nextfile).c_str() );
100 getline(myfile, line);
101 if( !myfile.good() ) break;
102 *ofit->second << line << endl << flush;
105 remove( (fulldir + "/" + nextfile).c_str() );
109 *ofit->second << "#end" << endl << flush;
110 ofit->second->close();
113 ofit->second->close();
114 rename((fulldir + "/rank" + cRank + "-" + ofit->first + ".tmp").c_str(),
115 (fulldir + "/rank" + cRank + "-" + ofit->first + ".part").c_str());
118 if( cRank[0] == '0' )
119 rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
121 logf << "[ " << timestring() << " ] Log ends here." << endl;
125 string writeout::getdatfile(string subname)
131 if((dp = opendir(fulldir.c_str())) == NULL) {
132 logf << "Error(" << errno << ") opening " << fulldir << endl;
136 while ((dirp = readdir(dp)) != NULL)
138 myfile = string(dirp->d_name);
140 if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part" &&
141 subname == myfile.substr( myfile.find("-")+1, myfile.rfind(".")-myfile.find("-")-1 ) ) {