Remove trailing whitespace systematically
[foam-extend-3.2.git] / src / foam / global / JobInfo / JobInfo.C
bloba1eddf347893177808eee5ac999a29a676b251ba
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     |
5     \\  /    A nd           | For copyright notice see file Copyright
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "JobInfo.H"
27 #include "OSspecific.H"
28 #include "clock.H"
29 #include "OFstream.H"
30 #include "Pstream.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 bool Foam::JobInfo::writeJobInfo(Foam::debug::infoSwitch("writeJobInfo", 0));
35 Foam::JobInfo Foam::jobInfo;
38 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
40 // Null constructor
41 Foam::JobInfo::JobInfo()
43     runningJobPath_(),
44     finishedJobPath_(),
45     cpuTime_()
47     name() = "JobInfo";
49     if (writeJobInfo && Pstream::master())
50     {
51         string baseDir = getEnv("FOAM_JOB_DIR");
52         string jobFile = hostName() + '.' + Foam::name(pid());
54         fileName runningDir(baseDir/"runningJobs");
55         fileName finishedDir(baseDir/"finishedJobs");
57         runningJobPath_  = runningDir/jobFile;
58         finishedJobPath_ = finishedDir/jobFile;
60         if (baseDir.empty())
61         {
62             FatalErrorIn("JobInfo::JobInfo()")
63                 << "Cannot get JobInfo directory $FOAM_JOB_DIR"
64                 << Foam::exit(FatalError);
65         }
67         if (!isDir(runningDir) && !mkDir(runningDir))
68         {
69             FatalErrorIn("JobInfo::JobInfo()")
70                 << "Cannot make JobInfo directory " << runningDir
71                 << Foam::exit(FatalError);
72         }
74         if (!isDir(finishedDir) && !mkDir(finishedDir))
75         {
76             FatalErrorIn("JobInfo::JobInfo()")
77                 << "Cannot make JobInfo directory " << finishedDir
78                 << Foam::exit(FatalError);
79         }
80     }
82     constructed = true;
86 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
88 Foam::JobInfo::~JobInfo()
90     if (writeJobInfo && constructed && Pstream::master())
91     {
92         mv(runningJobPath_, finishedJobPath_);
93     }
95     constructed = false;
99 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
101 bool Foam::JobInfo::write(Ostream& os) const
103     if (writeJobInfo && Pstream::master())
104     {
105         if (os.good())
106         {
107             dictionary::write(os, false);
108             return true;
109         }
110         else
111         {
112             return false;
113         }
114     }
115     else
116     {
117         return true;
118     }
122 void Foam::JobInfo::write() const
124     if (writeJobInfo && Pstream::master())
125     {
126         if (!write(OFstream(runningJobPath_)()))
127         {
128             FatalErrorIn("JobInfo::write() const")
129                 << "Failed to write to JobInfo file "
130                 << runningJobPath_
131                 << Foam::exit(FatalError);
132         }
133     }
137 void Foam::JobInfo::end(const word& terminationType)
139     if (writeJobInfo && constructed && Pstream::master())
140     {
141         add("cpuTime", cpuTime_.elapsedCpuTime());
142         add("endDate", clock::date());
143         add("endTime", clock::clockTime());
145         if (!found("termination"))
146         {
147             add("termination", terminationType);
148         }
150         rm(runningJobPath_);
151         write(OFstream(finishedJobPath_)());
152     }
154     constructed = false;
158 void Foam::JobInfo::end()
160     end("normal");
164 void Foam::JobInfo::exit()
166     end("exit");
170 void Foam::JobInfo::abort()
172     end("abort");
176 void Foam::JobInfo::signalEnd() const
178     if (writeJobInfo && constructed && Pstream::master())
179     {
180         mv(runningJobPath_, finishedJobPath_);
181     }
183     constructed = false;
187 // ************************************************************************* //