SimTKOpenMMLog.cpp 9.81 KB
Newer Older
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

/* Portions copyright (c) 2006 Stanford University and Simbios.
 * Contributors: Pande Group
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "SimTKOpenMMLog.h"
26
#include <stdlib.h>
27
#include "openmm/OpenMMException.h"
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

// static settings

SimTKOpenMMLog* SimTKOpenMMLog::_simTKOpenMMLog = NULL;

/**---------------------------------------------------------------------------------------

   SimTKOpenMMLog constructor (Simbios)

   @param logFile file reference for logging

   --------------------------------------------------------------------------------------- */

SimTKOpenMMLog::SimTKOpenMMLog( FILE* logFile ) : _logFile( logFile ){

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::SimTKOpenMMLog";

// ---------------------------------------------------------------------------------------

   _logLevel  = LogLowLevel;

}

/**---------------------------------------------------------------------------------------

   SimTKOpenMMLog destructor (Simbios)

   --------------------------------------------------------------------------------------- */

SimTKOpenMMLog::~SimTKOpenMMLog( ){

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::~SimTKOpenMMLog";

// ---------------------------------------------------------------------------------------

}

/**---------------------------------------------------------------------------------------

   Get LogFile

   @return    logFile

   --------------------------------------------------------------------------------------- */

FILE* SimTKOpenMMLog::getLogFile( void ) const {

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::getLogFile";

// ---------------------------------------------------------------------------------------

   return _logFile;

}

/**---------------------------------------------------------------------------------------

   Set LogFile

   @param    input logFile

   --------------------------------------------------------------------------------------- */

97
void SimTKOpenMMLog::setLogFile( FILE* logFile ){
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::setLogFile";

// ---------------------------------------------------------------------------------------

   _logFile = logFile;

}

/**---------------------------------------------------------------------------------------

   Set LogLevel

   @param    input logLevel

   --------------------------------------------------------------------------------------- */

117
void SimTKOpenMMLog::setLogLevel( SimTKOpenMMLog::LogLevels logLevel ){
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::setLogLevel";

// ---------------------------------------------------------------------------------------

   _logLevel = logLevel;

}

/**---------------------------------------------------------------------------------------

   SimTKOpenMMLog log message to log (Simbios)

   @param message         message to log

   --------------------------------------------------------------------------------------- */

137
void SimTKOpenMMLog::logMessage( const std::stringstream& message ) const {
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

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::logMessage";

// ---------------------------------------------------------------------------------------

   if( _logFile ){

//      (void) fprintf( stderr, "%s", message.str().c_str() );
//      (void) fflush( stderr );

      (void) fprintf( _logFile, "%s", message.str().c_str() );
      (void) fflush( _logFile );

   }
}

/**---------------------------------------------------------------------------------------

   Set global simTKOpenMMLog (Simbios)

   @param logFile         file to log to

   @return new SimTKOpenMMLog

   --------------------------------------------------------------------------------------- */

SimTKOpenMMLog* SimTKOpenMMLog::setSimTKOpenMMLog( FILE* logFile ){

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::setSimTKOpenMMLog";

// ---------------------------------------------------------------------------------------

   // allow for multiple logs

/*
   if( _simTKOpenMMLog ){
      delete _simTKOpenMMLog;
   }
*/
   _simTKOpenMMLog = new SimTKOpenMMLog( logFile );

   return _simTKOpenMMLog;
}

/**---------------------------------------------------------------------------------------

   Get global simTKOpenMMLog -- static method (Simbios)

   @return static member

   --------------------------------------------------------------------------------------- */

SimTKOpenMMLog* SimTKOpenMMLog::getSimTKOpenMMLog( void ){

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::getSimTKOpenMMLog";

// ---------------------------------------------------------------------------------------

   if( !_simTKOpenMMLog ){
      _simTKOpenMMLog = new SimTKOpenMMLog( );
   }
   return _simTKOpenMMLog;
}

/**---------------------------------------------------------------------------------------

   Get global simTKOpenMMLog (Simbios)

   @return FILE reference

   --------------------------------------------------------------------------------------- */

FILE* SimTKOpenMMLog::getSimTKOpenMMLogFile( void ){

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::getSimTKOpenMMLogFile";

// ---------------------------------------------------------------------------------------

   SimTKOpenMMLog* simTKOpenMMLog = getSimTKOpenMMLog( );
   return simTKOpenMMLog->getLogFile();
}

/**---------------------------------------------------------------------------------------

   Static method to print message (Simbios)

   @param message         message to log

   --------------------------------------------------------------------------------------- */

236
void SimTKOpenMMLog::printMessage( const std::stringstream& message ){
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::printMessage";

// ---------------------------------------------------------------------------------------

   if( _simTKOpenMMLog ){
      _simTKOpenMMLog->logMessage( message );
   } else {
      (void) fprintf( stderr, "%s", message.str().c_str() );
      (void) fflush( stderr );
   }
}

/**---------------------------------------------------------------------------------------

   Static method to print warning message (Simbios)
   If global _simTKOpenMMLog is not set, then print to stderr

   @param message         message to log

   --------------------------------------------------------------------------------------- */

261
void SimTKOpenMMLog::printWarning( const std::stringstream& message ){
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::printWarning";

// ---------------------------------------------------------------------------------------

   if( _simTKOpenMMLog ){
      std::stringstream messageWithHeader;
      messageWithHeader << "Warning: " << message.str();
      _simTKOpenMMLog->logMessage( messageWithHeader );
   } else {
      (void) fprintf( stderr, "Warning: %s", message.str().c_str() );
   }
}

/**---------------------------------------------------------------------------------------

   Static method to print error message and exit program (Simbios)
   If global _simTKOpenMMLog is not set, then print to stderr

   @param message   message to log

   --------------------------------------------------------------------------------------- */

287
void SimTKOpenMMLog::printError( const std::stringstream& message ){
288
289
290
291
292
293
294

// ---------------------------------------------------------------------------------------

// static const std::string methodName = "\nSimTKOpenMMLog::printError";

// ---------------------------------------------------------------------------------------

295
296
   std::stringstream messageWithHeader;
   messageWithHeader << "Error: " << message.str();
297
298
299
   if( _simTKOpenMMLog ){
      _simTKOpenMMLog->logMessage( messageWithHeader );
   } else {
300
      (void) fprintf( stderr, "%s", messageWithHeader.str().c_str() );
301
302
      (void) fflush( stderr );
   }
303
   throw OpenMM::OpenMMException(messageWithHeader.str());
304
}