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