sigWriteNow.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 
37 // Signal number to catch
38 int sigWriteNow::signal_
39 (
40  debug::optimisationSwitch("writeNowSignal", -1)
41 );
42 
43 // Register re-reader
45 :
47 {
48 
49 public:
50 
52  :
53  ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
54  {}
55 
57  {}
58 
59  virtual void readData(Foam::Istream& is)
60  {
61  sigWriteNow::signal_ = readLabel(is);
63  }
64 
65  virtual void writeData(Foam::Ostream& os) const
66  {
67  os << sigWriteNow::signal_;
68  }
69 };
70 
72 
73 } // End namespace Foam
74 
75 
76 Foam::Time* Foam::sigWriteNow::runTimePtr_ = nullptr;
77 struct sigaction Foam::sigWriteNow::oldAction_;
78 
79 
80 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
81 
82 void Foam::sigWriteNow::sigHandler(int)
83 {
84  Info<< "sigWriteNow :"
85  << " setting up write at end of the next iteration" << nl << endl;
86  runTimePtr_->writeOnce();
87 
89  // raise(signal_);
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
96 {}
97 
98 
100 {
101  // Store runTime
102  runTimePtr_ = &runTime;
103 
104  set(verbose);
105 }
106 
107 
108 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
109 
111 {
112  // Reset old handling
113  if (signal_ > 0)
114  {
115  if (sigaction(signal_, &oldAction_, nullptr) < 0)
116  {
118  << "Cannot reset " << signal_ << " trapping"
119  << abort(FatalError);
120  }
121  }
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
127 void Foam::sigWriteNow::set(const bool verbose)
128 {
129  if (signal_ >= 0)
130  {
131  struct sigaction newAction;
132  newAction.sa_handler = sigHandler;
133  newAction.sa_flags = SA_NODEFER;
134  sigemptyset(&newAction.sa_mask);
135  if (sigaction(signal_, &newAction, &oldAction_) < 0)
136  {
138  << "Cannot set " << signal_ << " trapping"
139  << abort(FatalError);
140  }
141 
142  if (verbose)
143  {
144  Info<< "sigWriteNow :"
145  << " Enabling writing upon signal " << signal_
146  << endl;
147  }
148  }
149 }
150 
151 
153 {
154  return signal_ > 0;
155 }
156 
157 
158 // ************************************************************************* //
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigWriteNow.C:65
Abstract base class for registered object with I/O. Used in debug symbol registration.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
bool writeInfoHeader
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:196
~sigWriteNow()
Destructor.
Definition: sigWriteNow.C:110
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...
bool active() const
Is active?
Definition: sigWriteNow.C:152
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:54
static void set(const bool verbose)
(re)set signal catcher
Definition: sigWriteNow.C:127
static const char nl
Definition: Ostream.H:260
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
messageStream Info
addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal")
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:261
sigWriteNow()
Construct null.
Definition: sigWriteNow.C:95
virtual void readData(Foam::Istream &is)
Read.
Definition: sigWriteNow.C:59
addwriteNowSignalToOpt(const char *name)
Definition: sigWriteNow.C:51