1 #include "application.h"
21 #pragma comment(lib, "advapi32")
29 #ifdef THREAD_SAFE_CANTERA
30 cthreadId_t getThisThreadId()
32 #if defined(BOOST_HAS_WINTHREADS)
33 return ::GetCurrentThreadId();
34 #elif defined(BOOST_HAS_PTHREADS)
35 return pthread_self();
69 errorMessage(r.errorMessage),
70 errorRoutine(r.errorRoutine),
76 loglevels(r.loglevels),
77 loggroups(r.loggroups)
104 #ifdef WITH_HTML_LOGS
114 errorMessage.push_back(msg);
115 errorRoutine.push_back(r);
121 return static_cast<int>(errorMessage.size()) ;
126 if (logwriter == _logwriter) {
129 if (logwriter != 0) {
133 logwriter = _logwriter;
139 return logwriter->env() ;
145 logwriter->error(msg) ;
151 logwriter->write(pszmsg) ;
157 logwriter->write(msg);
163 logwriter->writeendl();
166 #ifdef WITH_HTML_LOGS
171 if (_loglevel != -99) {
172 loglevel = _loglevel;
180 loglevels.push_back(loglevel);
182 loggroups.push_back(title);
187 current = &xmllog->addChild(
"ul");
190 current = ¤t->addChild(
"li",
"<b>"+title+
"</b>");
191 current = ¤t->addChild(
"ul");
196 if (loglevel > 0 && current) {
197 current->addChild(
"li",tag+
": "+value);
203 if (loglevel > 0 && current) {
204 current->addChild(
"li",tag+
": "+
fp2str(value));
210 if (loglevel > 0 && current) {
211 current->addChild(
"li",tag+
": "+
int2str(value));
217 if (loglevel > 0 && current) {
218 current->addChild(
"li",msg);
228 "Error while ending a LogGroup. This is probably due to an unmatched"
229 " beginning and ending group");
230 current = current->parent();
232 "Error while ending a LogGroup. This is probably due to an unmatched"
233 " beginning and ending group");
234 current = current->parent();
237 loglevel = loglevels.back();
238 loglevels.pop_back();
239 if (title !=
"" && title != loggroups.back()) {
241 "\n beginLogGroup: "+ loggroups.back()+
242 "\n endLogGroup: "+title+
"\n");
244 }
else if (loggroups.size() == 1) {
249 loggroups.pop_back();
258 std::string::size_type idot = file.rfind(
'.');
259 std::string ext =
"";
260 std::string nm = file;
262 ext = file.substr(idot, file.size());
263 nm = file.substr(0,idot);
272 std::string fname = nm + ext;
273 std::ifstream f(fname.c_str());
279 std::ifstream f(fname.c_str());
291 std::ofstream f(fname.c_str());
293 xmllog->root().write(f);
295 writelog(
"Log file " + fname +
" written.\n");
302 #endif // WITH_HTML_LOGS
304 #ifdef THREAD_SAFE_CANTERA
312 cthreadId_t curId = getThisThreadId() ;
313 threadMsgMap_t::iterator iter = m_threadMsgMap.find(curId) ;
314 if (iter != m_threadMsgMap.end()) {
315 return (iter->second.get()) ;
318 m_threadMsgMap.insert(std::pair< cthreadId_t, pMessages_t >(curId, pMsgs)) ;
325 cthreadId_t curId = getThisThreadId() ;
326 threadMsgMap_t::iterator iter = m_threadMsgMap.find(curId) ;
327 if (iter != m_threadMsgMap.end()) {
328 m_threadMsgMap.erase(iter) ;
331 #endif // THREAD_SAFE_CANTERA
339 #if !defined( THREAD_SAFE_CANTERA )
353 #if defined(THREAD_SAFE_CANTERA)
368 std::map<std::string, XML_Node*>::iterator pos;
370 pos->second->unlock();
386 #if defined(THREAD_SAFE_CANTERA)
395 std::string path =
"";
400 std::replace_if(path.begin(), path.end(),
401 std::bind2nd(std::equal_to<char>(),
'\\'),
'/') ;
412 string::size_type idot = path.rfind(
'.');
415 ext = path.substr(idot, path.size());
420 if (ext !=
".xml" && ext !=
".ctml") {
428 string::size_type islash = path.rfind(
'/');
430 ff = string(
"./")+path.substr(islash+1,idot-islash - 1) +
".xml";
432 ff = string(
"./")+path.substr(0,idot) +
".xml";
435 writelog(
"get_XML_File(): Expected location of xml file = " +
445 writelog(
"get_XML_File(): File, " + ff +
446 ", was previously read." +
447 " Retrieving the stored xml tree.\n");
464 std::ifstream s(ff.c_str());
472 string estring =
"cannot open "+ff+
" for reading.";
473 estring +=
"Note, this error indicates a possible configuration problem.";
491 std::map<string, XML_Node*>::iterator
494 for (; b != e; ++b) {
507 long int Application::readStringRegistryKey(
const std::string& keyName,
const std::string& valueName,
508 std::string& value,
const std::string& defaultValue)
512 long open_error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName.c_str(), 0, KEY_READ, &key);
513 if (open_error != ERROR_SUCCESS) {
516 value = defaultValue;
518 DWORD bufferSize =
sizeof(buffer);
520 error = RegQueryValueEx(key, valueName.c_str(), 0, NULL, (LPBYTE) buffer, &bufferSize);
521 if (ERROR_SUCCESS == error) {
539 if (static_cast<int>(errorMessage.size()) > 0) {
541 "\n\n************************************************\n"
543 "************************************************\n\n";
544 return head+string(
"\nProcedure: ")+errorRoutine.back()
545 +string(
"\nError: ")+errorMessage.back();
547 return "<no Cantera error>";
553 int i =
static_cast<int>(errorMessage.size());
558 f <<
"************************************************" << endl;
559 f <<
" Cantera Error! " << endl;
560 f <<
"************************************************" << endl
563 for (j = 0; j < i; j++) {
565 f <<
"Procedure: " << errorRoutine[j] << endl;
566 f <<
"Error: " << errorMessage[j] << endl;
569 errorMessage.clear();
570 errorRoutine.clear();
575 int i =
static_cast<int>(errorMessage.size());
580 writelog(
"************************************************\n");
582 writelog(
"************************************************\n\n");
584 for (j = 0; j < i; j++) {
586 writelog(
string(
"Procedure: ")+ errorRoutine[j]+
" \n");
587 writelog(
string(
"Error: ")+ errorMessage[j]+
" \n");
590 errorMessage.clear();
591 errorRoutine.clear();
608 std::string installDir;
609 readStringRegistryKey(
"SOFTWARE\\Cantera\\Cantera 2.0",
610 "InstallDir", installDir,
"");
611 if (installDir !=
"") {
612 dirs.push_back(installDir +
"data");
613 dirs.push_back(installDir +
"templates");
617 const char* old_pythonpath = getenv(
"PYTHONPATH");
618 std::string pythonpath =
"PYTHONPATH=" + installDir +
"\\bin";
619 if (old_pythonpath) {
621 pythonpath.append(old_pythonpath);
623 putenv(pythonpath.c_str());
632 dirs.push_back(
"/Applications/Cantera/data");
639 if (getenv(
"CANTERA_DATA") != 0) {
640 string datadir = string(getenv(
"CANTERA_DATA"));
641 dirs.push_back(datadir);
649 string datadir = string(CANTERA_DATA);
650 dirs.push_back(datadir);
664 for (m = 0; m < n; m++) {
676 string::size_type islash = name.find(
'/');
677 string::size_type ibslash = name.find(
'\\');
683 nd =
static_cast<int>(dirs.size());
686 for (i = 0; i < nd; i++) {
687 inname = dirs[i] +
"/" + name;
688 std::ifstream fin(inname.c_str());
695 msg =
"\nInput file " + name
696 +
" not found in director";
697 msg += (nd == 1 ?
"y " :
"ies ");
698 for (i = 0; i < nd; i++) {
699 msg +=
"\n'" + dirs[i] +
"'";
705 msg +=
"To fix this problem, either:\n";
706 msg +=
" a) move the missing files into the local directory;\n";
707 msg +=
" b) define environment variable CANTERA_DATA to\n";
708 msg +=
" point to the directory containing the file.";