20 string writeout::longToStr (long arg)
27 void writeout::newsub(string subname) {
28 of[subname] = new ofstream;
30 if ( fulldir != "" ) {
31 if(rank>0) of[subname]->open( (fulldir + "/rank" + cRank + "-" + subname + ".tmp").c_str() );
32 else of[subname]->open( (fulldir + "/" + signature + "-" + subname + ".dat").c_str() );
34 if ( !of[subname]->is_open() ) {
35 logf << "WRITEOUT: Could not open output-file!" << endl << flush;
39 buf[subname] = of[subname]->rdbuf();
42 buf[subname] = cout.rdbuf();
44 out[subname] = new ostream(buf[subname]);
47 writeout::writeout(const string& wdir, const string& _signature,
48 const int& _rank, const int& procs)
54 signature = _signature;
59 sprintf(cRank, "%d", rank);
62 timestamp = time(NULL);
65 fulldir = wdir + "/" + longToStr(timestamp) + "." + longToStr(iseq) + "_" + signature + ".tmp";
66 if ( mkdir(fulldir.c_str(), 0775) == 0 )
68 else if ( errno != EEXIST ) {
69 cerr << "WRITEOUT: Could not create outdir!" << endl << flush;
76 for(int idest=1; idest<numprocs; idest++) {
77 MPI_Send(×tamp, 1, MPI_LONG, idest, 123, MPI_COMM_WORLD);
78 MPI_Send(&iseq, 1, MPI_LONG, idest, 124, MPI_COMM_WORLD);
86 MPI_Recv(×tamp, 1, MPI_LONG, 0, 123, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
87 MPI_Recv(&iseq, 1, MPI_LONG, 0, 124, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
88 fulldir = wdir + "/" + longToStr(timestamp) + "." + longToStr(iseq) + "_" + signature + ".tmp";
92 logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
94 if ( !logf.is_open() ) {
95 cerr << "WRITEOUT: Could not open log-file!" << endl << flush;
99 logf << "[ " << timestring() << " ] Log starts here." << endl;
101 logbuf = logf.rdbuf();
104 logbuf = cerr.rdbuf();
106 log = new ostream(logbuf);
109 string writeout::timestring()
114 timestring = asctime( localtime( &rawtime ) );
115 return timestring.substr(0, timestring.size()-1);;
118 writeout::~writeout()
121 for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
122 if( cRank[0] == '0' ) {
124 while(jobsdone<numprocs-1) {
126 if( (nextfile = getdatfile(ofit->first)) == "" )
129 logf << "collecting " << nextfile << endl;
131 ifstream myfile( (fulldir + "/" + nextfile).c_str() );
134 getline(myfile, line);
135 if( !myfile.good() ) break;
136 *ofit->second << line << endl << flush;
139 remove( (fulldir + "/" + nextfile).c_str() );
143 *ofit->second << "#end" << endl << flush;
144 ofit->second->close();
147 ofit->second->close();
148 rename((fulldir + "/rank" + cRank + "-" + ofit->first + ".tmp").c_str(),
149 (fulldir + "/rank" + cRank + "-" + ofit->first + ".part").c_str());
152 if( cRank[0] == '0' )
153 rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
155 logf << "[ " << timestring() << " ] Log ends here." << endl;
159 string writeout::getdatfile(string subname)
165 if((dp = opendir(fulldir.c_str())) == NULL) {
166 logf << "Error(" << errno << ") opening " << fulldir << endl;
171 while ((dirp = readdir(dp)) != NULL)
173 myfile = string(dirp->d_name);
175 if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part" &&
176 subname == myfile.substr( myfile.find("-")+1, myfile.rfind(".")-myfile.find("-")-1 ) ) {