Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
application.h
Go to the documentation of this file.
1 //! @file application.h
2 #ifndef CT_BASE_APPLICATION_H
3 #define CT_BASE_APPLICATION_H
4 
5 #include "cantera/base/config.h"
7 #include "cantera/base/logger.h"
8 
9 #include <set>
10 #include <memory>
11 
12 namespace Cantera
13 {
14 
15 class XML_Node;
16 
17 /*!
18  * @defgroup globalData Global Data
19  *
20  * Global data are available anywhere. There are two kinds.
21  * Cantera has an assortment of constant values for physical parameters.
22  * Also, Cantera maintains a collection of global data which is specific
23  * to each process that invokes Cantera functions. This process-specific
24  * data is stored in the class Application.
25  */
26 
27 
28 //! Class to hold global data.
29 /*!
30  * Class Application is the top-level
31  * class that stores data that should persist for the duration of
32  * the process. The class should not be instantiated directly;
33  * instead, it is instantiated as needed by the functions declared
34  * here. At most one instance is created, and it is not destroyed
35  * until the process terminates.
36  *
37  * @ingroup textlogs
38  * @ingroup globalData
39  */
41 {
42 protected:
43  //! Class to carry out messages
44  class Messages
45  {
46  public:
47  //! Constructor for the Messages class
48  /*! Constructor for the Messages class which is a subclass
49  * of the Application class.
50  */
51  Messages();
52 
53  Messages(const Messages& r);
54  Messages& operator=(const Messages& r);
55  ~Messages();
56 
57  //! Set an error condition in the application class without
58  //! throwing an exception.
59  /*!
60  * This routine adds an error message to the end of the stack of errors
61  * that Cantera accumulates in the Application class.
62  * @param r Procedure name which is generating the error condition
63  * @param msg Descriptive message of the error condition.
64  * @ingroup errorhandling
65  */
66  void addError(const std::string& r, const std::string& msg);
67 
68  //! Return the number of errors that have been encountered so far.
69  /*!
70  * @ingroup errorhandling
71  */
72  int getErrorCount() ;
73 
74  //! Discard the last error message
75  /*!
76  * %Cantera saves a stack of exceptions that it has caught in the
77  * Application class. This routine eliminates the last exception to be
78  * added to that stack.
79  *
80  * @ingroup errorhandling
81  */
82  void popError() ;
83 
84  //! Retrieve the last error message in a string
85  /*!
86  * This routine will retrieve the last error message and return it in
87  * the return string.
88  *
89  * @ingroup errorhandling
90  */
91  std::string lastErrorMessage() ;
92 
93  //! Prints all of the error messages to an ostream
94  /*!
95  * Write out all of the saved error messages to the ostream f using
96  * the function Logger::writelog. Cantera saves a stack of exceptions
97  * that it has caught in the Application class. This routine writes
98  * out all of the error messages to the ostream and then clears them
99  * from internal storage.
100  *
101  * @param f ostream which will receive the error messages
102  *
103  * @ingroup errorhandling
104  */
105  void getErrors(std::ostream& f) ;
106 
107  //! Prints all of the error messages using writelog
108  /*!
109  * Print all of the error messages using function writelog.
110  * Cantera saves a stack of exceptions that it
111  * has caught in the Application class. This routine writes
112  * out all of the error messages
113  * and then clears them from internal storage.
114  *
115  * @ingroup errorhandling
116  */
117  void logErrors();
118 
119  //! Write a message to the screen.
120  /*!
121  * The string may be of any length, and may contain end-of-line
122  * characters. This method is used throughout Cantera to write log
123  * messages. It can also be called by user programs. The advantage of
124  * using writelog over writing directly to the standard output is that
125  * messages written with writelog will display correctly even when
126  * Cantera is used from MATLAB or other application that do not have a
127  * standard output stream.
128  *
129  * @param msg c++ string to be written to the screen
130  * @ingroup textlogs
131  */
132  void writelog(const std::string& msg);
133 
134  //! Write an end of line character to the screen and flush output
135  void writelogendl();
136 
137  //! Write an error message and quit.
138  /*!
139  * The default behavior is to write to the standard error stream, and
140  * then call exit(). Note that no end-of-line character is appended
141  * to the message, and so if one is desired it must be included in
142  * the string. Note that this default behavior will terminate the
143  * application Cantera is invoked from (MATLAB, Excel, etc.) If this
144  * is not desired, then derive a class and reimplement this method.
145  *
146  * @param msg Error message to be written to cerr.
147  * @deprecated To be removed after Cantera 2.2
148  */
149  void logerror(const std::string& msg) ;
150 
151  //! Install a logger.
152  /*!
153  * Called by the language interfaces to install an appropriate logger.
154  * The logger is used for the writelog() function
155  *
156  * @param logwriter Pointer to a logger object
157  * @see Logger.
158  * @ingroup textlogs
159  */
160  void setLogger(Logger* logwriter) ;
161 
162  protected:
163  //! Current list of error messages
164  std::vector<std::string> errorMessage;
165 
166  //! Current error Routine
167  std::vector<std::string> errorRoutine;
168 
169  //! Current pointer to the logwriter
171  } ;
172 
173 #ifdef THREAD_SAFE_CANTERA
174  //! Typedef for thread specific messages
175  typedef boost::shared_ptr< Messages > pMessages_t ;
176 
177  //! Class that stores thread messages for each thread, and retrieves them
178  //! based on the thread id.
180  {
181  public:
182  //! Constructor
184 
185  //! Provide a pointer dereferencing overloaded operator
186  /*!
187  * @return returns a pointer to Messages
188  */
189  Messages* operator->();
190 
191  //! Remove a local thread message
192  void removeThreadMessages();
193 
194  //! Typedef for map between a thread and the message
195  typedef std::map< cthreadId_t, pMessages_t > threadMsgMap_t ;
196 
197  private:
198  //! Thread Msg Map
200  } ;
201 #endif
202 
203 
204 protected:
205  //! Constructor for class sets up the initial conditions
206  //! Protected ctor access thru static member function Instance
207  Application();
208 
209 public:
210  //! Return a pointer to the one and only instance of class Application
211  /*!
212  * If the an Application object has not yet been created it is created
213  */
214  static Application* Instance();
215 
216  //! Destructor for class deletes global data
217  /*!
218  * Delete any open XML trees, the logwriter, and
219  * the XML log, if any.
220  */
221  virtual ~Application();
222 
223  //! Static function that destroys the application class's data
224  static void ApplicationDestroy();
225 
226  //! @copydoc Messages::addError
227  void addError(const std::string& r, const std::string& msg) {
228  pMessenger->addError(r, msg) ;
229  }
230 
231  //! @copydoc Messages::getErrorCount
233  return pMessenger->getErrorCount() ;
234  }
235 
236  //! @copydoc Messages::popError
237  void popError() {
238  pMessenger->popError() ;
239  }
240 
241  //! @copydoc Messages::lastErrorMessage
242  std::string lastErrorMessage() {
243  return pMessenger->lastErrorMessage() ;
244  }
245 
246  //! @copydoc Messages::getErrors
247  void getErrors(std::ostream& f) {
248  pMessenger->getErrors(f) ;
249  }
250 
251  //! @copydoc Messages::logErrors
252  void logErrors() {
253  pMessenger->logErrors() ;
254  }
255 
256  //! Add a directory to the data file search path.
257  /*!
258  * @ingroup inputfiles
259  *
260  * @param dir String name for the directory to be added to the search path
261  */
262  void addDataDirectory(const std::string& dir) ;
263 
264  //! Find an input file.
265  /*!
266  * This routine will search for a file in the default locations specified
267  * for the application. See the routine setDefaultDirectories() listed
268  * above.
269  *
270  * The default set of directories specified for the application will be
271  * searched if a '/' or an '\\' is found in the name. If either is found
272  * then a relative path name is presumed, and the default directories are
273  * not searched.
274  *
275  * The presence of the file is determined by whether the file can be
276  * opened for reading by the current user.
277  *
278  * @param name Name of the input file to be searched for
279  *
280  * @return The absolute path name of the first matching file is
281  * returned. If a relative path name is indicated, the relative path
282  * name is returned.
283  *
284  * If the file is not found, a message is written to stdout and a
285  * CanteraError exception is thrown.
286  *
287  * @ingroup inputfiles
288  */
289  std::string findInputFile(const std::string& name) ;
290 
291  //! Return a pointer to the XML tree for a Cantera input file.
292  /*!
293  * This routine will find the file and read the XML file into an
294  * XML tree structure. Then, a pointer will be returned. If the
295  * file has already been processed, then just the pointer will
296  * be returned.
297  *
298  * @param file String containing the relative or absolute file name
299  * @param debug Debug flag
300  */
301  XML_Node* get_XML_File(const std::string& file, int debug=0) ;
302 
303  //! Read a CTI or CTML string and fill up an XML tree.
304  /*!
305  * Return a pointer to the XML tree corresponding to the specified
306  * CTI or XML string. If the given string has been processed before,
307  * the cached XML tree will be returned. Otherwise, the XML tree
308  * will be generated and stored in the cache.
309  * @param text CTI or CTML string
310  * @return Root of the corresponding XML tree
311  */
312  XML_Node* get_XML_from_string(const std::string& text);
313 
314  //! Close an XML File
315  /*!
316  * Close a file that is opened by this application object
317  *
318  * @param file String containing the relative or absolute file name
319  */
320  void close_XML_File(const std::string& file) ;
321 
322 #ifdef _WIN32
323  long int readStringRegistryKey(const std::string& keyName, const std::string& valueName,
324  std::string& value, const std::string& defaultValue);
325 #endif
326 
327  //! @copydoc Messages::writelog
328  void writelog(const std::string& msg) {
329  pMessenger->writelog(msg);
330  }
331 
332  //! Write an endl to the screen and flush output
333  void writelogendl() {
335  }
336 
337  //! @copydoc Messages::logerror
338  void logerror(const std::string& msg) {
339  pMessenger->logerror(msg);
340  }
341 
342  //! Print a warning indicating that *method* is deprecated. Additional
343  //! information (removal version, alternatives) can be specified in
344  //! *extra*. Deprecation warnings are printed once per method per
345  //! invocation of the application.
346  void warn_deprecated(const std::string& method, const std::string& extra="");
347 
348  //! Globally disable printing of deprecation warnings. Used primarily to
349  //! prevent certain tests from failing.
351  m_suppress_deprecation_warnings = true;
352  }
353 
354  //! @copydoc Messages::setLogger
355  void setLogger(Logger* logwriter) {
356  pMessenger->setLogger(logwriter);
357  }
358 
359  //! Delete and free memory allocated per thread in multithreaded applications
360  /*!
361  * Delete the memory allocated per thread by Cantera. It should be called
362  * from within the thread just before the thread terminates. If your
363  * version of Cantera has not been specifically compiled for thread safety
364  * this function does nothing.
365  */
366  void thread_complete() ;
367 
368 protected:
369  //! Set the default directories for input files.
370  /*!
371  * %Cantera searches for input files along a path that includes platform-
372  * specific default locations, and possibly user-specified locations.
373  * This function installs the platform-specific directories on the search
374  * path. It is invoked at startup by appinit(), and never should need to
375  * be called by user programs.
376  *
377  * The current directory (".") is always searched first. Then, on Windows
378  * platforms, if environment variable COMMONPROGRAMFILES is set (which it
379  * should be on Win XP or Win 2000), then directories under this one will
380  * be added to the search path. The %Cantera Windows installer installs
381  * data files to this location.
382  *
383  * On the Mac, directory '/Applications/Cantera/data' is added to the
384  * search path.
385  *
386  * On any platform, if environment variable CANTERA_DATA is set to a
387  * directory name, then this directory is added to the search path.
388  *
389  * Finally, the location where the data files were installed when
390  * %Cantera was built is added to the search path.
391  *
392  * Additional directories may be added by calling function addDirectory.
393  * @ingroup inputfiles
394  */
395  void setDefaultDirectories();
396 
397  //! Current vector of input directories to search for input files
398  std::vector<std::string> inputDirs;
399  //! Current list of error messages
400  //vector<string> errorMessage;
401  //! Current list of warning messages
402  //vector<string> warning;
403  //! Current error Routine
404  //vector<string> errorRoutine;
405  //! Last error message
406  //string msglog;
407  //! Current line length
408  // size_t linelen;
409  //! Current value of stop_on_error
411  //! Current map of options
412  std::map<std::string, std::string> options;
413  //! Current value of tmp_dir
414  std::string tmp_dir;
415  //! Current vector of XML file trees that have been previously parsed
416  //! The second element of the value is used to store the last-modified time
417  //! for the file, to enable change detection.
418  std::map<std::string, std::pair<XML_Node*, int> > xmlfiles;
419  //! Vector of deprecation warnings that have been emitted (to suppress duplicates)
420  std::set<std::string> warnings;
421 
422  bool m_suppress_deprecation_warnings;
423 
424  //! Current pointer to the logwriter
425  //Logger* logwriter;
426 
427 #if defined(THREAD_SAFE_CANTERA)
429 #else
430  std::auto_ptr<Messages> pMessenger ;
431 #endif
432 
433 private:
434  //! Pointer to the single Application instance
435  static Application* s_app ;
436 };
437 
438 }
439 
440 #endif
void addError(const std::string &r, const std::string &msg)
Set an error condition in the application class without throwing an exception.
Definition: application.h:227
void popError()
Discard the last error message.
void writelogendl()
Write an end of line character to the screen and flush output.
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:333
Base class for 'loggers' that write text messages to log files.
Definition: logger.h:39
Class to hold global data.
Definition: application.h:40
int getErrorCount()
Return the number of errors that have been encountered so far.
ThreadMessages pMessenger
Current pointer to the logwriter.
Definition: application.h:428
virtual ~Application()
Destructor for class deletes global data.
Messages()
Constructor for the Messages class.
Definition: application.cpp:69
std::string lastErrorMessage()
Retrieve the last error message in a string.
Definition: application.h:242
std::string findInputFile(const std::string &name)
Find an input file.
static Application * s_app
Pointer to the single Application instance.
Definition: application.h:435
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:100
void logerror(const std::string &msg)
Write an error message and quit.
void addError(const std::string &r, const std::string &msg)
Set an error condition in the application class without throwing an exception.
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:328
XML_Node * get_XML_from_string(const std::string &text)
Read a CTI or CTML string and fill up an XML tree.
boost::shared_ptr< Messages > pMessages_t
Typedef for thread specific messages.
Definition: application.h:175
std::vector< std::string > inputDirs
Current vector of input directories to search for input files.
Definition: application.h:398
std::string lastErrorMessage()
Retrieve the last error message in a string.
void logerror(const std::string &msg)
Write an error message and quit.
Definition: application.h:338
std::map< std::string, std::pair< XML_Node *, int > > xmlfiles
Current vector of XML file trees that have been previously parsed The second element of the value is ...
Definition: application.h:418
std::string tmp_dir
Current value of tmp_dir.
Definition: application.h:414
void setDefaultDirectories()
Set the default directories for input files.
void setLogger(Logger *logwriter)
Install a logger.
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: application.h:350
void warn_deprecated(const std::string &method, const std::string &extra="")
Print a warning indicating that method is deprecated.
Class that stores thread messages for each thread, and retrieves them based on the thread id...
Definition: application.h:179
std::set< std::string > warnings
Vector of deprecation warnings that have been emitted (to suppress duplicates)
Definition: application.h:420
void popError()
Discard the last error message.
Definition: application.h:237
XML_Node * get_XML_File(const std::string &file, int debug=0)
Return a pointer to the XML tree for a Cantera input file.
Application()
Constructor for class sets up the initial conditions Protected ctor access thru static member functio...
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
void logErrors()
Prints all of the error messages using writelog.
Definition: application.h:252
void getErrors(std::ostream &f)
Prints all of the error messages to an ostream.
Definition: application.h:247
static Application * Instance()
Return a pointer to the one and only instance of class Application.
Header for Base class for 'loggers' that write text messages to log files (see Writing messages to th...
void removeThreadMessages()
Remove a local thread message.
std::map< cthreadId_t, pMessages_t > threadMsgMap_t
Typedef for map between a thread and the message.
Definition: application.h:195
Class to carry out messages.
Definition: application.h:44
std::vector< std::string > errorMessage
Current list of error messages.
Definition: application.h:164
threadMsgMap_t m_threadMsgMap
Thread Msg Map.
Definition: application.h:199
std::vector< std::string > errorRoutine
Current error Routine.
Definition: application.h:167
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
static void ApplicationDestroy()
Static function that destroys the application class's data.
Logger * logwriter
Current pointer to the logwriter.
Definition: application.h:170
void writelog(const std::string &msg)
Write a message to the screen.
Messages * operator->()
Provide a pointer dereferencing overloaded operator.
void logErrors()
Prints all of the error messages using writelog.
bool stop_on_error
Current list of error messages.
Definition: application.h:410
void getErrors(std::ostream &f)
Prints all of the error messages to an ostream.
int getErrorCount()
Return the number of errors that have been encountered so far.
Definition: application.h:232
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:355
void close_XML_File(const std::string &file)
Close an XML File.
std::map< std::string, std::string > options
Current map of options.
Definition: application.h:412