sigWriteNow.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "sigWriteNow.H"
27 #include "error.H"
28 #include "JobInfo.H"
29 #include "IOstreams.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 // Signal number to catch
37 int sigWriteNow::signal_
38 (
39  debug::optimisationSwitch("writeNowSignal", -1)
40 );
41 // Register re-reader
43 :
45 {
46 public:
48  :
50  {}
52  {}
53  virtual void readData(Foam::Istream& is)
54  {
55  sigWriteNow::signal_ = readLabel(is);
56  sigWriteNow::set(true);
57  }
58  virtual void writeData(Foam::Ostream& os) const
59  {
60  os << sigWriteNow::signal_;
61  }
62 };
64 
65 }
66 
67 
68 static Foam::Time* runTimePtr_ = NULL;
69 
70 
71 struct sigaction Foam::sigWriteNow::oldAction_;
72 
73 
74 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
75 
76 void Foam::sigWriteNow::sigHandler(int)
77 {
78  Info<< "sigWriteNow :"
79  << " setting up write at end of the next iteration" << nl << endl;
80  runTimePtr_->writeOnce();
81 
83  //raise(signal_);
84 }
85 
86 
87 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88 
90 {}
91 
92 
93 Foam::sigWriteNow::sigWriteNow(const bool verbose, Time& runTime)
94 {
95  // Store runTime
96  runTimePtr_ = &runTime;
97 
98  set(verbose);
99 }
100 
101 
102 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
103 
105 {
106  // Reset old handling
107  if (signal_ > 0)
108  {
109  if (sigaction(signal_, &oldAction_, NULL) < 0)
110  {
112  << "Cannot reset " << signal_ << " trapping"
113  << abort(FatalError);
114  }
115  }
116 }
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
121 void Foam::sigWriteNow::set(const bool verbose)
122 {
123  if (signal_ >= 0)
124  {
125  struct sigaction newAction;
126  newAction.sa_handler = sigHandler;
127  newAction.sa_flags = SA_NODEFER;
128  sigemptyset(&newAction.sa_mask);
129  if (sigaction(signal_, &newAction, &oldAction_) < 0)
130  {
132  << "Cannot set " << signal_ << " trapping"
133  << abort(FatalError);
134  }
135 
136  if (verbose)
137  {
138  Info<< "sigWriteNow :"
139  << " Enabling writing upon signal " << signal_
140  << endl;
141  }
142  }
143 }
144 
145 
147 {
148  return signal_ > 0;
149 }
150 
151 
152 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
bool active() const
Is active?
Definition: sigWriteNow.C:146
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigWriteNow.C:58
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:184
~sigWriteNow()
Destructor.
Definition: sigWriteNow.C:104
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
errorManip< error > abort(error &err)
Definition: errorManip.H:131
label readLabel(Istream &is)
Definition: label.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static void set(const bool verbose)
(re)set signal catcher
Definition: sigWriteNow.C:121
static const char nl
Definition: Ostream.H:262
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:501
messageStream Info
addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal")
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:236
sigWriteNow()
Construct null.
Definition: sigWriteNow.C:89
virtual void readData(Foam::Istream &is)
Read.
Definition: sigWriteNow.C:53
addwriteNowSignalToOpt(const char *name)
Definition: sigWriteNow.C:47
Namespace for OpenFOAM.