17 string writeout::longToStr (long arg)
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)
51 signature = _signature;
56 sprintf(cRank, "%d", rank);
59 timestamp = time(NULL);
62 fulldir = wdir + "/" + longToStr(timestamp) + "." + longToStr(iseq) + "_" + signature + ".tmp";
63 if ( mkdir(fulldir.c_str(), 0775) == 0 )
65 else if ( errno != EEXIST ) {
66 cerr << "WRITEOUT: Could not create outdir!" << endl << flush;
73 for(int idest=1; idest<numprocs; idest++) {
74 MPI_Send(×tamp, 1, MPI_LONG, idest, 123, MPI_COMM_WORLD);
75 MPI_Send(&iseq, 1, MPI_LONG, idest, 124, MPI_COMM_WORLD);
83 MPI_Recv(×tamp, 1, MPI_LONG, 0, 123, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
84 MPI_Recv(&iseq, 1, MPI_LONG, 0, 124, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
85 fulldir = wdir + "/" + longToStr(timestamp) + "." + longToStr(iseq) + "_" + signature + ".tmp";
89 logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
91 if ( !logf.is_open() ) {
92 cerr << "WRITEOUT: Could not open log-file!" << endl << flush;
96 logf << "[ " << timestring() << " ] Log starts here." << endl;
98 logbuf = logf.rdbuf();
101 logbuf = cerr.rdbuf();
103 log = new ostream(logbuf);
106 string writeout::timestring()
111 timestring = asctime( localtime( &rawtime ) );
112 return timestring.substr(0, timestring.size()-1);;
115 writeout::~writeout()
118 for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
119 if( cRank[0] == '0' ) {
121 while(jobsdone<numprocs-1) {
123 if( (nextfile = getdatfile(ofit->first)) == "" )
126 logf << "collecting " << nextfile << endl;
128 ifstream myfile( (fulldir + "/" + nextfile).c_str() );
131 getline(myfile, line);
132 if( !myfile.good() ) break;
133 *ofit->second << line << endl << flush;
136 remove( (fulldir + "/" + nextfile).c_str() );
140 *ofit->second << "#end" << endl << flush;
141 ofit->second->close();
144 ofit->second->close();
145 rename((fulldir + "/rank" + cRank + "-" + ofit->first + ".tmp").c_str(),
146 (fulldir + "/rank" + cRank + "-" + ofit->first + ".part").c_str());
149 if( cRank[0] == '0' )
150 rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
152 logf << "[ " << timestring() << " ] Log ends here." << endl;
156 string writeout::getdatfile(string subname)
162 if((dp = opendir(fulldir.c_str())) == NULL) {
163 logf << "Error(" << errno << ") opening " << fulldir << endl;
168 while ((dirp = readdir(dp)) != NULL)
170 myfile = string(dirp->d_name);
172 if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part" &&
173 subname == myfile.substr( myfile.find("-")+1, myfile.rfind(".")-myfile.find("-")-1 ) ) {