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