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