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-2018 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);
62  sigWriteNow::set(true);
63  }
64 
65  virtual void writeData(Foam::Ostream& os) const
66  {
67  os << sigWriteNow::signal_;
68  }
69 };
70 
72 
73 }
74 
75 
76 Foam::Time* Foam::sigWriteNow::runTimePtr_ = nullptr;
77 
78 
79 struct sigaction Foam::sigWriteNow::oldAction_;
80 
81 
82 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
83 
84 void Foam::sigWriteNow::sigHandler(int)
85 {
86  Info<< "sigWriteNow :"
87  << " setting up write at end of the next iteration" << nl << endl;
88  runTimePtr_->writeOnce();
89 
91  // raise(signal_);
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
96 
98 {}
99 
100 
102 {
103  // Store runTime
104  runTimePtr_ = &runTime;
105 
106  set(verbose);
107 }
108 
109 
110 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
111 
113 {
114  // Reset old handling
115  if (signal_ > 0)
116  {
117  if (sigaction(signal_, &oldAction_, nullptr) < 0)
118  {
120  << "Cannot reset " << signal_ << " trapping"
121  << abort(FatalError);
122  }
123  }
124 }
125 
126 
127 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
128 
129 void Foam::sigWriteNow::set(const bool verbose)
130 {
131  if (signal_ >= 0)
132  {
133  struct sigaction newAction;
134  newAction.sa_handler = sigHandler;
135  newAction.sa_flags = SA_NODEFER;
136  sigemptyset(&newAction.sa_mask);
137  if (sigaction(signal_, &newAction, &oldAction_) < 0)
138  {
140  << "Cannot set " << signal_ << " trapping"
141  << abort(FatalError);
142  }
143 
144  if (verbose)
145  {
146  Info<< "sigWriteNow :"
147  << " Enabling writing upon signal " << signal_
148  << endl;
149  }
150  }
151 }
152 
153 
155 {
156  return signal_ > 0;
157 }
158 
159 
160 // ************************************************************************* //
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
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:256
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:112
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:154
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:129
static const char nl
Definition: Ostream.H:265
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:97
virtual void readData(Foam::Istream &is)
Read.
Definition: sigWriteNow.C:59
addwriteNowSignalToOpt(const char *name)
Definition: sigWriteNow.C:51