Menu

[487b0c]: / src / ibsimu.cpp  Maximize  Restore  History

Download this file

284 lines (223 with data), 7.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*! \file ibsimu.cpp
* \brief Ion Beam Simulator global settings
*/
/* Copyright (c) 2010-2012 Taneli Kalvas. All rights reserved.
*
* You can redistribute this software and/or modify it under the terms
* of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library (file "COPYING" included in the package);
* if not, write to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* If you have questions about your rights to use or distribute this
* software, please contact Berkeley Lab's Technology Transfer
* Department at TTD@lbl.gov. Other questions, comments and bug
* reports should be sent directly to the author via email at
* taneli.kalvas@jyu.fi.
*
* NOTICE. This software was developed under partial funding from the
* U.S. Department of Energy. As such, the U.S. Government has been
* granted for itself and others acting on its behalf a paid-up,
* nonexclusive, irrevocable, worldwide license in the Software to
* reproduce, prepare derivative works, and perform publicly and
* display publicly. Beginning five (5) years after the date
* permission to assert copyright is obtained from the U.S. Department
* of Energy, and subject to any subsequent five (5) year renewals,
* the U.S. Government is granted for itself and others acting on its
* behalf a paid-up, nonexclusive, irrevocable, worldwide license in
* the Software to reproduce, prepare derivative works, distribute
* copies to the public, perform publicly and display publicly, and to
* permit others to do so.
*/
#include "config.h"
#include "id.hpp"
#include <iostream>
#include <signal.h>
#include "ibsimu.hpp"
#include "timer.hpp"
#include "error.hpp"
IBSimu::IBSimu( const IBSimu &ibs )
: _t(0), _os(&_ns)
{
}
IBSimu::IBSimu()
: _hello(false), _threadcount(1), _os(&std::cout), _indent(0)
{
// Set message level thresholds to defaults
for( int a = 0; a < MSG_COUNT; a++ )
_message_threshold[a] = 0;
_message_threshold[MSG_WARNING] = 1;
_message_threshold[MSG_ERROR] = 1;
#if defined(_GNU_SOURCE) && defined(HAVE_SIGINFO_T)
// Set a catch for segmentation fault
struct sigaction act_sigsegv;
act_sigsegv.sa_sigaction = SignalHandler::signal_handler_SIGSEGV;
sigemptyset( &act_sigsegv.sa_mask );
act_sigsegv.sa_flags = SA_SIGINFO;
sigaction( SIGSEGV, &act_sigsegv, NULL );
// Set a catch for terminate/kill/int
struct sigaction act_sigterm;
act_sigterm.sa_sigaction = SignalHandler::signal_handler_SIGTERM;
sigemptyset( &act_sigterm.sa_mask );
act_sigterm.sa_flags = SA_SIGINFO;
sigaction( SIGTERM, &act_sigterm, NULL );
sigaction( SIGQUIT, &act_sigterm, NULL );
sigaction( SIGINT, &act_sigterm, NULL );
#else
signal( SIGTERM, SignalHandler::signal_handler_SIGTERM );
signal( SIGINT, SignalHandler::signal_handler_SIGTERM );
#endif
// Start timer for whole simulation
_t = new Timer;
}
IBSimu::~IBSimu()
{
_t->stop();
message( 1 ) << "Ending simulation\n";
message( 1 ) << " time used = " << *_t << "\n";
flush();
// Close the message output file if it is open
if( _fo.is_open() )
_fo.close();
delete _t;
}
std::ostream &IBSimu::message( message_type_e type, int32_t level )
{
if( type >= MSG_COUNT || type < 0 )
throw( Error( ERROR_LOCATION, "invalid output type" ) );
if( level > _message_threshold[type] )
return( _ns );
//return( *_os );
return( _ss );
}
std::ostream &IBSimu::message( int32_t level )
{
if( level > _message_threshold[MSG_VERBOSE] )
return( _ns );
flush();
//return( *_os );
return( _ss );
}
void IBSimu::inc_indent( void )
{
flush( false );
_indent++;
}
void IBSimu::dec_indent( void )
{
flush( false );
_indent--;
}
std::ostream &IBSimu::set_message_output( std::ostream &os )
{
if( _fo.is_open() ) {
_fo.close();
_os = &os;
return( _ns );
} else {
std::ostream *osold = _os;
_os = &os;
return( *osold );
}
}
std::ostream &IBSimu::set_message_output( const std::string &filename )
{
if( _fo.is_open() ) {
_fo.close();
_fo.open( filename.c_str() );
if( !_fo.is_open() )
throw( Error( ERROR_LOCATION, "couldn\'t open file \'" + filename + "\' for writing" ) );
_os = &_fo;
return( _ns );
} else {
std::ostream *osold = _os;
_fo.open( filename.c_str() );
if( !_fo.is_open() )
throw( Error( ERROR_LOCATION, "couldn\'t open file \'" + filename + "\' for writing" ) );
_os = &_fo;
return( *osold );
}
}
bool IBSimu::output_is_cout()
{
return( _os == &std::cout );
}
void IBSimu::set_message_threshold( message_type_e type, int32_t level )
{
if( type >= MSG_COUNT || type < 0 )
throw( Error( ERROR_LOCATION, "invalid output message type" ) );
// Set message threshold level
_message_threshold[type] = level;
// If setting to verbose mode and no greeting has yet been shown
if( type == MSG_VERBOSE && level > 0 && !_hello ) {
_hello = true;
message( 1 ) << "Ion Beam Simulator " VERSION " (" IBSIMU_GIT_ID ")\n";
}
}
int32_t IBSimu::get_message_threshold( message_type_e type )
{
if( type >= MSG_COUNT || type < 0 )
throw( Error( ERROR_LOCATION, "invalid output type" ) );
return( _message_threshold[type] );
}
void IBSimu::flush( bool finishlines )
{
std::string stmp;
bool lineunfinished;
convert_stringstream_to_lines( _ss,
_indent*2,
stmp,
lineunfinished );
*_os << stmp;
*_os << std::flush;
if( lineunfinished && finishlines )
*_os << "\n";
_ss.str( "" );
}
size_t IBSimu::convert_stringstream_to_lines( const std::stringstream &ss,
size_t indentation,
std::string &target,
bool &lineunfinished )
{
std::string stmp( ss.str() );
lineunfinished = false;
if( stmp.size() ) {
for( size_t i = 0; i < indentation; i++ )
target += ' ';
lineunfinished = true;
}
size_t pos = 0;
size_t linecount = 0;
while( pos < stmp.size() ) {
if( stmp[pos] == '\n' ) {
lineunfinished = false;
++linecount;
target += '\n';
} else {
if( lineunfinished == false )
for( size_t i = 0; i < indentation; i++ )
target += ' ';
target += stmp[pos];
lineunfinished = true;
}
pos++;
}
return( linecount );
}
void IBSimu::set_thread_count( uint32_t threadcount )
{
if( threadcount <= 0 )
throw( Error( ERROR_LOCATION, "invalid parameter" ) );
_threadcount = threadcount;
}
/* Global instance */
IBSimu ibsimu;