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