Cantera  2.1.2
global.cpp
Go to the documentation of this file.
1 //! @file global.cpp
2 #include "cantera/base/global.h"
3 
6 #include "cantera/base/xml.h"
7 #include "application.h"
8 #include "units.h"
9 
10 #include <cstdio>
11 #include <stdarg.h>
12 
13 using namespace std;
14 
15 namespace Cantera
16 {
17 
18 //! Definition of whether the DEBUG_MODE environment is turned on within Cantera.
19 #if DEBUG_MODE_ENABLED == 1
20 const int g_DEBUG_MODE = 1;
21 #else
22 const int g_DEBUG_MODE = 0;
23 #endif
24 
25 
26 //! Return a pointer to the application object
27 static Application* app()
28 {
29  return Application::Instance() ;
30 }
31 
32 // **************** Text Logging ****************
33 
34 void setLogger(Logger* logwriter)
35 {
36  try {
37  app()->setLogger(logwriter) ;
38  } catch (std::bad_alloc) {
39  logwriter->error("bad alloc thrown by app()");
40  }
41 }
42 
43 void writelog(const std::string& msg)
44 {
45  app()->writelog(msg);
46 }
47 
48 void writelogf(const char* fmt,...)
49 {
50  enum { BUFSIZE = 2048 } ;
51  char sbuf[BUFSIZE] ;
52 
53  va_list args ;
54  va_start(args, fmt) ;
55 
56 #ifdef _MSC_VER
57  _vsnprintf(sbuf, BUFSIZE, fmt, args) ;
58 #else
59  vsprintf(sbuf, fmt, args) ;
60 #endif
61 
62  writelog(sbuf) ;
63  va_end(args) ;
64 }
65 
67 {
68  app()->writelogendl();
69 }
70 
71 void error(const std::string& msg)
72 {
73  app()->logerror(msg);
74 }
75 
76 void warn_deprecated(const std::string& method, const std::string& extra)
77 {
78  app()->warn_deprecated(method, extra);
79 }
80 
82 {
84 }
85 
86 // **************** HTML Logging ****************
87 
88 #ifdef WITH_HTML_LOGS
89 
90 void beginLogGroup(const std::string& title, int loglevel)
91 {
92  app()->beginLogGroup(title, loglevel) ;
93 }
94 
95 void addLogEntry(const std::string& tag, const std::string& value)
96 {
97  app()->addLogEntry(tag, value) ;
98 }
99 
100 void addLogEntry(const std::string& tag, doublereal value)
101 {
102  app()->addLogEntry(tag, value) ;
103 }
104 
105 void addLogEntry(const std::string& tag, int value)
106 {
107  app()->addLogEntry(tag, value) ;
108 }
109 
110 void addLogEntry(const std::string& msg)
111 {
112  app()->addLogEntry(msg) ;
113 }
114 
115 void endLogGroup(const std::string& title)
116 {
117  app()->endLogGroup(title) ;
118 }
119 
120 void write_logfile(const std::string& file)
121 {
122  app()->write_logfile(file) ;
123 }
124 
125 #endif // WITH_HTML_LOGS
126 
127 // **************** Global Data ****************
128 
129 Unit* Unit::s_u = 0;
130 mutex_t Unit::units_mutex;
131 
132 void appdelete()
133 {
134  Application::ApplicationDestroy() ;
135  FactoryBase::deleteFactories();
136  Unit::deleteUnit();
137 }
138 
140 {
141  app()->thread_complete() ;
142 }
143 
144 XML_Node* get_XML_File(const std::string& file, int debug)
145 {
146  XML_Node* xtmp = app()->get_XML_File(file, debug) ;
147  //writelog("get_XML_File: returned from app:get_XML_FILE " + int2str(xtmp) + "\n");
148  return xtmp;
149 }
150 
151 void close_XML_File(const std::string& file)
152 {
153  app()->close_XML_File(file) ;
154 }
155 
156 int nErrors()
157 {
158  return app()->getErrorCount();
159 }
160 
161 void popError()
162 {
163  app()->popError() ;
164 }
165 
167 {
168  return app()->lastErrorMessage() ;
169 }
170 
171 void showErrors(std::ostream& f)
172 {
173  app()->getErrors(f) ;
174 }
175 
177 {
178  app()->logErrors() ;
179 }
180 
181 void setError(const std::string& r, const std::string& msg)
182 {
183  app()->addError(r, msg) ;
184 }
185 
186 void addDirectory(const std::string& dir)
187 {
188  app()->addDataDirectory(dir) ;
189 }
190 
191 std::string findInputFile(const std::string& name)
192 {
193  return app()->findInputFile(name) ;
194 }
195 
196 doublereal toSI(const std::string& unit)
197 {
198  doublereal f = Unit::units()->toSI(unit);
199  if (f) {
200  return f;
201  } else {
202  throw CanteraError("toSI","unknown unit string: "+unit);
203  }
204  return 1.0;
205 }
206 
207 doublereal actEnergyToSI(const std::string& unit)
208 {
209  doublereal f = Unit::units()->actEnergyToSI(unit);
210  if (f) {
211  return f;
212  }
213  return 1.0;
214 }
215 
216 string canteraRoot()
217 {
218  char* ctroot = 0;
219  ctroot = getenv("CANTERA_ROOT");
220  if (ctroot != 0) {
221  return string(ctroot);
222  }
223 #ifdef CANTERA_ROOT
224  return string(CANTERA_ROOT);
225 #else
226  return "";
227 #endif
228 
229 }
230 
231 //! split a string at a '#' sign. Used to separate a file name from an id string.
232 /*!
233  * @param src Original string to be split up. This is unchanged.
234  * @param file Output string representing the first part of the string, which is the filename.
235  * @param id Output string representing the last part of the string, which is the id.
236  */
237 static void split_at_pound(const std::string& src, std::string& file, std::string& id)
238 {
239  string::size_type ipound = src.find('#');
240  if (ipound != string::npos) {
241  id = src.substr(ipound+1,src.size());
242  file = src.substr(0,ipound);
243  } else {
244  id = "";
245  file = src;
246  }
247 }
248 
249 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
250 {
251  std::string fname, idstr;
252  XML_Node* db, *doc;
253  split_at_pound(file_ID, fname, idstr);
254  if (fname == "") {
255  if (!root) throw CanteraError("get_XML_Node",
256  "no file name given. file_ID = "+file_ID);
257  db = root->findID(idstr, 3);
258  } else {
259  doc = get_XML_File(fname);
260  if (!doc) throw CanteraError("get_XML_Node",
261  "get_XML_File failed trying to open "+fname);
262  db = doc->findID(idstr, 3);
263  }
264  if (!db) {
265  throw CanteraError("get_XML_Node",
266  "id tag '"+idstr+"' not found.");
267  }
268  return db;
269 }
270 
271 XML_Node* get_XML_NameID(const std::string& nameTarget,
272  const std::string& file_ID,
273  XML_Node* root)
274 {
275  string fname, idTarget;
276  XML_Node* db, *doc;
277  split_at_pound(file_ID, fname, idTarget);
278  if (fname == "") {
279  if (!root) {
280  return 0;
281  }
282  db = root->findNameID(nameTarget, idTarget);
283  } else {
284  doc = get_XML_File(fname);
285  if (!doc) {
286  return 0;
287  }
288  db = doc->findNameID(nameTarget, idTarget);
289  }
290  return db;
291 }
292 
293 std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
294 
295 }
XML_Node * get_XML_Node(const std::string &file_ID, XML_Node *root)
This routine will locate an XML node in either the input XML tree or in another input file specified ...
Definition: global.cpp:249
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
XML_Node * findNameID(const std::string &nameTarget, const std::string &idTarget) const
This routine carries out a recursive search for an XML node based on both the xml element name and th...
Definition: xml.cpp:627
XML_Node * get_XML_File(const std::string &file, int debug)
Return a pointer to the XML tree for a Cantera input file.
Definition: global.cpp:144
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
void popError()
Discard the last error message.
Definition: global.cpp:161
Class to hold global data.
Definition: application.h:41
void beginLogGroup(const std::string &title, int loglevel)
Create a new group for log messages.
Definition: global.cpp:90
string lastErrorMessage()
Retrieve the last error message in a string.
Definition: global.cpp:166
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string 'unit' to Kelvin...
Definition: global.cpp:207
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.
Definition: global.cpp:191
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
Definition: global.cpp:196
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:139
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:173
void setLogger(Logger *logwriter)
Install a logger.
Definition: global.cpp:34
void setError(const std::string &r, const std::string &msg)
Set an error condition in the application class without throwing an exception.
Definition: global.cpp:181
static Application * app()
Return a pointer to the application object.
Definition: global.cpp:27
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:100
void showErrors()
Prints all of the error messages using writelog.
Definition: global.cpp:176
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:76
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
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, HTML_logs (see Input File Handling, Diagnostic Output, Writing messages to the screen and Writing HTML Logfiles).
virtual void error(const std::string &msg)
Write an error message and quit.
Definition: logger.h:84
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:430
void logerror(const std::string &msg)
Write an error message and quit.
Definition: application.h:440
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:151
void writelogf(const char *fmt,...)
Write a formatted message to the screen.
Definition: global.cpp:48
const int g_DEBUG_MODE
Definition of whether the DEBUG_MODE environment is turned on within Cantera.
Definition: global.cpp:22
void error(const std::string &msg)
Write an error message and quit.
Definition: global.cpp:71
Classes providing support for XML data files.
void endLogGroup(const std::string &title)
Close the current group of log messages.
Definition: application.h:497
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
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.
Header for units conversion utilities, which are used to translate user input from input files (See I...
void appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:132
void popError()
Discard the last error message.
Definition: application.h:350
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.
Definition: global.cpp:115
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
void writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:66
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
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:81
XML_Node * findID(const std::string &id, const int depth=100) const
This routine carries out a recursive search for an XML node based on the xml element attribute...
Definition: xml.cpp:697
void write_logfile(const std::string &file)
Write the HTML log file.
Definition: application.h:502
string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:216
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
void write_logfile(const std::string &file)
Write the HTML log file.
Definition: global.cpp:120
void writelog(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:43
void addLogEntry(const std::string &msg)
Add an entry to an HTML log file.
Definition: global.cpp:110
int nErrors()
Return the number of errors that have been encountered so far.
Definition: global.cpp:156
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:186
int getErrorCount()
Return the number of errors that have been encountered so far.
Definition: application.h:345
XML_Node * get_XML_NameID(const std::string &nameTarget, const std::string &file_ID, XML_Node *root)
This routine will locate an XML node in either the input XML tree or in another input file specified ...
Definition: global.cpp:271
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
File contains the FactoryBase class declarations.
static void split_at_pound(const std::string &src, std::string &file, std::string &id)
split a string at a '#' sign. Used to separate a file name from an id string.
Definition: global.cpp:237
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:457
void close_XML_File(const std::string &file)
Close an XML File.