Cantera  2.1.2
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 HTML_logs
38  * @ingroup textlogs
39  * @ingroup globalData
40  */
42 {
43 protected:
44  //! Class to carry out messages
45  /*!
46  * @ingroup HTML_logs
47  */
48  class Messages
49  {
50  public:
51  //! Constructor for the Messages class
52  /*! Constructor for the Messages class which is a subclass
53  * of the Application class.
54  */
55  Messages();
56 
57  Messages(const Messages& r);
58  Messages& operator=(const Messages& r);
59  ~Messages();
60 
61  //! Set an error condition in the application class without
62  //! throwing an exception.
63  /*!
64  * This routine adds an error message to the end of the stack of errors
65  * that Cantera accumulates in the Application class.
66  * @param r Procedure name which is generating the error condition
67  * @param msg Descriptive message of the error condition.
68  * @ingroup errorhandling
69  */
70  void addError(const std::string& r, const std::string& msg);
71 
72  //! Return the number of errors that have been encountered so far.
73  /*!
74  * @ingroup errorhandling
75  */
76  int getErrorCount() ;
77 
78  //! Discard the last error message
79  /*!
80  * %Cantera saves a stack of exceptions that it has caught in the
81  * Application class. This routine eliminates the last exception to be
82  * added to that stack.
83  *
84  * @ingroup errorhandling
85  */
86  void popError() ;
87 
88  //! Retrieve the last error message in a string
89  /*!
90  * This routine will retrieve the last error message and return it in
91  * the return string.
92  *
93  * @ingroup errorhandling
94  */
95  std::string lastErrorMessage() ;
96 
97  //! Prints all of the error messages to an ostream
98  /*!
99  * Write out all of the saved error messages to the ostream f using
100  * the function Logger::writelog. Cantera saves a stack of exceptions
101  * that it has caught in the Application class. This routine writes
102  * out all of the error messages to the ostream and then clears them
103  * from internal storage.
104  *
105  * @param f ostream which will receive the error messages
106  *
107  * @ingroup errorhandling
108  */
109  void getErrors(std::ostream& f) ;
110 
111  //! Prints all of the error messages using writelog
112  /*!
113  * Print all of the error messages using function writelog.
114  * Cantera saves a stack of exceptions that it
115  * has caught in the Application class. This routine writes
116  * out all of the error messages
117  * and then clears them from internal storage.
118  *
119  * @ingroup errorhandling
120  */
121  void logErrors();
122 
123  //! Write a message to the screen.
124  /*!
125  * The string may be of any length, and may contain end-of-line
126  * characters. This method is used throughout Cantera to write log
127  * messages. It can also be called by user programs. The advantage of
128  * using writelog over writing directly to the standard output is that
129  * messages written with writelog will display correctly even when
130  * Cantera is used from MATLAB or other application that do not have a
131  * standard output stream.
132  *
133  * @param msg c++ string to be written to the screen
134  * @ingroup textlogs
135  */
136  void writelog(const std::string& msg);
137 
138  //! Write an end of line character to the screen and flush output
139  void writelogendl();
140 
141  //! Write an error message and quit.
142  /*!
143  * The default behavior is to write to the standard error stream, and
144  * then call exit(). Note that no end-of-line character is appended
145  * to the message, and so if one is desired it must be included in
146  * the string. Note that this default behavior will terminate the
147  * application Cantera is invoked from (MATLAB, Excel, etc.) If this
148  * is not desired, then derive a class and reimplement this method.
149  *
150  * @param msg Error message to be written to cerr.
151  */
152  void logerror(const std::string& msg) ;
153 
154  //! Install a logger.
155  /*!
156  * Called by the language interfaces to install an appropriate logger.
157  * The logger is used for the writelog() function
158  *
159  * @param logwriter Pointer to a logger object
160  * @see Logger.
161  * @ingroup textlogs
162  */
163  void setLogger(Logger* logwriter) ;
164 
165 #ifdef WITH_HTML_LOGS
166 
167  //!Create a new group for log messages.
168  /*!
169  * Usually this is called upon entering the function, with the title
170  * parameter equal to the name of the function or method. Subsequent
171  * messages written with addLogEntry will appear grouped under this
172  * heading, until endLogGroup() is called.
173  *
174  * @param title String name of the LogGroup
175  * @param loglevel loglevel of the group.
176  * @ingroup HTML_logs
177  * @deprecated HTML logs will be removed in Cantera 2.2
178  */
179  void beginLogGroup(const std::string& title, int loglevel) ;
180 
181  //! Add an entry to an HTML log file.
182  /*!
183  * Entries appear in the form "tag:value".
184  *
185  * @param tag tag
186  * @param value string value
187  *
188  * @ingroup HTML_logs
189  * @deprecated HTML logs will be removed in Cantera 2.2
190  */
191  void addLogEntry(const std::string& tag, const std::string& value) ;
192 
193  //! Add an entry to an HTML log file.
194  /*!
195  * Entries appear in the form "tag:value".
196  *
197  * @param tag tag
198  * @param value double value
199  *
200  * @ingroup HTML_logs
201  * @deprecated HTML logs will be removed in Cantera 2.2
202  */
203  void addLogEntry(const std::string& tag, doublereal value) ;
204 
205  //! Add an entry to an HTML log file.
206  /*!
207  * Entries appear in the form "tag:value".
208  *
209  * @param tag tag
210  * @param value integer value
211  *
212  * @ingroup HTML_logs
213  * @deprecated HTML logs will be removed in Cantera 2.2
214  */
215  void addLogEntry(const std::string& tag, int value) ;
216 
217  //! Add an entry to an HTML log file.
218  /*!
219  * Entries appear in the form "msg".
220  *
221  * @param msg Message to be added
222  *
223  * @ingroup HTML_logs
224  * @deprecated HTML logs will be removed in Cantera 2.2
225  */
226  void addLogEntry(const std::string& msg) ;
227 
228  //! Close the current group of log messages.
229  /*!
230  * This is typically called just before leaving a function or method,
231  * to close the group of messages that were output from this
232  * function. Subsequent messages written with addLogEntry() will
233  * appear at the next-higher level in the outline, unless
234  * beginLogGroup() is called first to create a new group.
235  *
236  * @param title Name of the log group. It defaults to the most recent
237  * log group created.
238  * @deprecated HTML logs will be removed in Cantera 2.2
239  */
240  void endLogGroup(const std::string& title) ;
241 
242  //! Write the HTML log file.
243  /*!
244  * Log entries are stored in memory in an XML tree until this
245  * function is called, which writes the tree to a file and clears the
246  * entries stored in memory. The output file will have the name
247  * specified in the 'file' argument. If this argument has no
248  * extension, the extension '.html' will be appended. Also, if the
249  * file already exists, an integer will be appended to the name so
250  * that no existing log file will be overwritten. WITH_HTML_LOGS must
251  * be defined.
252  *
253  * @param file Name of the file to be written
254  * @deprecated HTML logs will be removed in Cantera 2.2
255  */
256  void write_logfile(const std::string& file);
257 #endif
258 
259  protected:
260  //! Current list of error messages
261  std::vector<std::string> errorMessage;
262 
263  //! Current error Routine
264  std::vector<std::string> errorRoutine;
265 
266  //! Current pointer to the logwriter
268 #ifdef WITH_HTML_LOGS
269  //! Current pointer to the top of the XML_Node tree for the current HTML log
271 
272  //! Pointer to the last current position in the XML_Node tree for the current HTML log
274 
275  //! Current value of the loglevel
276  int loglevel;
277 
278  //! Vector of loglevels for loggroups that are open
279  std::vector<int> loglevels;
280 
281  //! Current vector of loggroups that are open
282  std::vector<std::string> loggroups;
283 #endif
284  } ;
285 
286 #ifdef THREAD_SAFE_CANTERA
287  //! Typedef for thread specific messages
288  typedef boost::shared_ptr< Messages > pMessages_t ;
289 
290  //! Class that stores thread messages for each thread, and retrieves them
291  //! based on the thread id.
293  {
294  public:
295  //! Constructor
297 
298  //! Provide a pointer dereferencing overloaded operator
299  /*!
300  * @return returns a pointer to Messages
301  */
302  Messages* operator->();
303 
304  //! Remove a local thread message
305  void removeThreadMessages();
306 
307  //! Typedef for map between a thread and the message
308  typedef std::map< cthreadId_t, pMessages_t > threadMsgMap_t ;
309 
310  private:
311  //! Thread Msg Map
313  } ;
314 #endif
315 
316 
317 protected:
318  //! Constructor for class sets up the initial conditions
319  //! Protected ctor access thru static member function Instance
320  Application();
321 
322 public:
323  //! Return a pointer to the one and only instance of class Application
324  /*!
325  * If the an Application object has not yet been created it is created
326  */
327  static Application* Instance();
328 
329  //! Destructor for class deletes global data
330  /*!
331  * Delete any open XML trees, the logwriter, and
332  * the XML log, if any.
333  */
334  virtual ~Application();
335 
336  //! Static function that destroys the application class's data
337  static void ApplicationDestroy();
338 
339  //! @copydoc Messages::addError
340  void addError(const std::string& r, const std::string& msg) {
341  pMessenger->addError(r, msg) ;
342  }
343 
344  //! @copydoc Messages::getErrorCount
346  return pMessenger->getErrorCount() ;
347  }
348 
349  //! @copydoc Messages::popError
350  void popError() {
351  pMessenger->popError() ;
352  }
353 
354  //! @copydoc Messages::lastErrorMessage
355  std::string lastErrorMessage() {
356  return pMessenger->lastErrorMessage() ;
357  }
358 
359  //! @copydoc Messages::getErrors
360  void getErrors(std::ostream& f) {
361  pMessenger->getErrors(f) ;
362  }
363 
364  //! @copydoc Messages::logErrors
365  void logErrors() {
366  pMessenger->logErrors() ;
367  }
368 
369  //! Add a directory to the data file search path.
370  /*!
371  * @ingroup inputfiles
372  *
373  * @param dir String name for the directory to be added to the search path
374  */
375  void addDataDirectory(const std::string& dir) ;
376 
377  //! Find an input file.
378  /*!
379  * This routine will search for a file in the default locations specified
380  * for the application. See the routine setDefaultDirectories() listed
381  * above.
382  *
383  * The default set of directories specified for the application will be
384  * searched if a '/' or an '\\' is found in the name. If either is found
385  * then a relative path name is presumed, and the default directories are
386  * not searched.
387  *
388  * The presence of the file is determined by whether the file can be
389  * opened for reading by the current user.
390  *
391  * @param name Name of the input file to be searched for
392  *
393  * @return The absolute path name of the first matching file is
394  * returned. If a relative path name is indicated, the relative path
395  * name is returned.
396  *
397  * If the file is not found, a message is written to stdout and a
398  * CanteraError exception is thrown.
399  *
400  * @ingroup inputfiles
401  */
402  std::string findInputFile(const std::string& name) ;
403 
404  //! Return a pointer to the XML tree for a Cantera input file.
405  /*!
406  * This routine will find the file and read the XML file into an
407  * XML tree structure. Then, a pointer will be returned. If the
408  * file has already been processed, then just the pointer will
409  * be returned.
410  *
411  * @param file String containing the relative or absolute file name
412  * @param debug Debug flag
413  */
414  XML_Node* get_XML_File(const std::string& file, int debug=0) ;
415 
416  //! Close an XML File
417  /*!
418  * Close a file that is opened by this application object
419  *
420  * @param file String containing the relative or absolute file name
421  */
422  void close_XML_File(const std::string& file) ;
423 
424 #ifdef _WIN32
425  long int readStringRegistryKey(const std::string& keyName, const std::string& valueName,
426  std::string& value, const std::string& defaultValue);
427 #endif
428 
429  //! @copydoc Messages::writelog
430  void writelog(const std::string& msg) {
431  pMessenger->writelog(msg);
432  }
433 
434  //! Write an endl to the screen and flush output
435  void writelogendl() {
437  }
438 
439  //! @copydoc Messages::logerror
440  void logerror(const std::string& msg) {
441  pMessenger->logerror(msg);
442  }
443 
444  //! Print a warning indicating that *method* is deprecated. Additional
445  //! information (removal version, alternatives) can be specified in
446  //! *extra*. Deprecation warnings are printed once per method per
447  //! invocation of the application.
448  void warn_deprecated(const std::string& method, const std::string& extra="");
449 
450  //! Globally disable printing of deprecation warnings. Used primarily to
451  //! prevent certain tests from failing.
453  m_suppress_deprecation_warnings = true;
454  }
455 
456  //! @copydoc Messages::setLogger
457  void setLogger(Logger* logwriter) {
458  pMessenger->setLogger(logwriter);
459  }
460 
461  //! Delete and free memory allocated per thread in multithreaded applications
462  /*!
463  * Delete the memory allocated per thread by Cantera. It should be called
464  * from within the thread just before the thread terminates. If your
465  * version of Cantera has not been specifically compiled for thread safety
466  * this function does nothing.
467  */
468  void thread_complete() ;
469 
470 #ifdef WITH_HTML_LOGS
471  //! @copydoc Messages::beginLogGroup
472  void beginLogGroup(const std::string& title, int loglevel) {
473  pMessenger->beginLogGroup(title,loglevel);
474  }
475 
476  //! @copydoc Messages::addLogEntry(const std::string&, const std::string&)
477  void addLogEntry(const std::string& tag, const std::string& value) {
478  pMessenger->addLogEntry(tag, value);
479  }
480 
481  //! @copydoc Messages::addLogEntry(const std::string&, doublereal)
482  void addLogEntry(const std::string& tag, doublereal value) {
483  pMessenger->addLogEntry(tag, value);
484  }
485 
486  //! @copydoc Messages::addLogEntry(const std::string&, int)
487  void addLogEntry(const std::string& tag, int value) {
488  pMessenger->addLogEntry(tag, value);
489  }
490 
491  //! @copydoc Messages::addLogEntry(const std::string&)
492  void addLogEntry(const std::string& msg) {
493  pMessenger->addLogEntry(msg);
494  }
495 
496  //! @copydoc Messages::endLogGroup
497  void endLogGroup(const std::string& title) {
498  pMessenger->endLogGroup(title) ;
499  }
500 
501  //! @copydoc Messages::write_logfile
502  void write_logfile(const std::string& file) {
503  pMessenger->write_logfile(file) ;
504  }
505 #endif
506 
507 protected:
508  //! Set the default directories for input files.
509  /*!
510  * %Cantera searches for input files along a path that includes platform-
511  * specific default locations, and possibly user-specified locations.
512  * This function installs the platform-specific directories on the search
513  * path. It is invoked at startup by appinit(), and never should need to
514  * be called by user programs.
515  *
516  * The current directory (".") is always searched first. Then, on Windows
517  * platforms, if environment variable COMMONPROGRAMFILES is set (which it
518  * should be on Win XP or Win 2000), then directories under this one will
519  * be added to the search path. The %Cantera Windows installer installs
520  * data files to this location.
521  *
522  * On the Mac, directory '/Applications/Cantera/data' is added to the
523  * search path.
524  *
525  * On any platform, if environment variable CANTERA_DATA is set to a
526  * directory name, then this directory is added to the search path.
527  *
528  * Finally, the location where the data files were installed when
529  * %Cantera was built is added to the search path.
530  *
531  * Additional directories may be added by calling function addDirectory.
532  * @ingroup inputfiles
533  */
534  void setDefaultDirectories();
535 
536  //! Current vector of input directories to search for input files
537  std::vector<std::string> inputDirs;
538  //! Current list of error messages
539  //vector<string> errorMessage;
540  //! Current list of warning messages
541  //vector<string> warning;
542  //! Current error Routine
543  //vector<string> errorRoutine;
544  //! Last error message
545  //string msglog;
546  //! Current line length
547  // size_t linelen;
548  //! Current value of stop_on_error
550  //! Current map of options
551  std::map<std::string, std::string> options;
552  //! Current value of tmp_dir
553  std::string tmp_dir;
554  //! Current vector of xml file trees that have been previously parsed
555  std::map<std::string, XML_Node*> xmlfiles;
556  //! Vector of deprecation warnings that have been emitted (to suppress duplicates)
557  std::set<std::string> warnings;
558 
559  bool m_suppress_deprecation_warnings;
560 
561  //! Current pointer to the logwriter
562  //Logger* logwriter;
563 #ifdef WITH_HTML_LOGS
564  //! Current pointer to the top of the XML_Node tree for the current HTML log
565  //XML_Node *xmllog;
566  //! Pointer to the last current position in the XML_Node tree for the current HTML log
567  //XML_Node *current;
568  //! Current value of loglevel
569  //int loglevel;
570  //! Vector of loglevels for loggroups that are open
571  //vector<int> loglevels;
572  //! Current vector of loggroups that are open
573  //vector<string> loggroups;
574 #endif
575 
576 #if defined(THREAD_SAFE_CANTERA)
578 #else
579  std::auto_ptr<Messages> pMessenger ;
580 #endif
581 
582 private:
583  //! Pointer to the single Application instance
584  static Application* s_app ;
585 };
586 
587 }
588 
589 #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:340
void popError()
Discard the last error message.
void writelogendl()
Write an end of line character to the screen and flush output.
std::map< std::string, XML_Node * > xmlfiles
Current vector of xml file trees that have been previously parsed.
Definition: application.h:555
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:435
Base class for 'loggers' that write text messages to log files.
Definition: logger.h:39
Class to hold global data.
Definition: application.h:41
int getErrorCount()
Return the number of errors that have been encountered so far.
ThreadMessages pMessenger
Current pointer to the logwriter.
Definition: application.h:577
virtual ~Application()
Destructor for class deletes global data.
std::vector< std::string > loggroups
Current vector of loggroups that are open.
Definition: application.h:282
Messages()
Constructor for the Messages class.
Definition: application.cpp:53
std::string lastErrorMessage()
Retrieve the last error message in a string.
Definition: application.h:355
std::string findInputFile(const std::string &name)
Find an input file.
XML_Node * xmllog
Current pointer to the top of the XML_Node tree for the current HTML log.
Definition: application.h:270
void beginLogGroup(const std::string &title, int loglevel)
Create a new group for log messages.
static Application * s_app
Pointer to the single Application instance.
Definition: application.h:584
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 beginLogGroup(const std::string &title, int loglevel)
Create a new group for log messages.
Definition: application.h:472
void addLogEntry(const std::string &tag, const std::string &value)
Add an entry to an HTML log file.
Definition: application.h:477
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:430
boost::shared_ptr< Messages > pMessages_t
Typedef for thread specific messages.
Definition: application.h:288
XML_Node * current
Pointer to the last current position in the XML_Node tree for the current HTML log.
Definition: application.h:273
std::vector< std::string > inputDirs
Current vector of input directories to search for input files.
Definition: application.h:537
std::string lastErrorMessage()
Retrieve the last error message in a string.
void addLogEntry(const std::string &tag, const std::string &value)
Add an entry to an HTML log file.
void logerror(const std::string &msg)
Write an error message and quit.
Definition: application.h:440
std::string tmp_dir
Current value of tmp_dir.
Definition: application.h:553
void addLogEntry(const std::string &msg)
Add an entry to an HTML log file.
Definition: application.h:492
void setDefaultDirectories()
Set the default directories for input files.
void addLogEntry(const std::string &tag, int value)
Add an entry to an HTML log file.
Definition: application.h:487
void endLogGroup(const std::string &title)
Close the current group of log messages.
Definition: application.h:497
void setLogger(Logger *logwriter)
Install a logger.
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: application.h:452
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:292
std::set< std::string > warnings
Vector of deprecation warnings that have been emitted (to suppress duplicates)
Definition: application.h:557
void popError()
Discard the last error message.
Definition: application.h:350
void write_logfile(const std::string &file)
Write the HTML log file.
XML_Node * get_XML_File(const std::string &file, int debug=0)
Return a pointer to the XML tree for a Cantera input file.
void endLogGroup(const std::string &title)
Close the current group of log messages.
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:365
void getErrors(std::ostream &f)
Prints all of the error messages to an ostream.
Definition: application.h:360
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:308
void addLogEntry(const std::string &tag, doublereal value)
Add an entry to an HTML log file.
Definition: application.h:482
Class to carry out messages.
Definition: application.h:48
std::vector< std::string > errorMessage
Current list of error messages.
Definition: application.h:261
threadMsgMap_t m_threadMsgMap
Thread Msg Map.
Definition: application.h:312
void write_logfile(const std::string &file)
Write the HTML log file.
Definition: application.h:502
std::vector< int > loglevels
Vector of loglevels for loggroups that are open.
Definition: application.h:279
std::vector< std::string > errorRoutine
Current error Routine.
Definition: application.h:264
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:267
void writelog(const std::string &msg)
Write a message to the screen.
int loglevel
Current value of the loglevel.
Definition: application.h:276
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:549
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:345
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:457
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:551