From d77ebf7ff20d3bc97cd7522a1a2fce446ac2cc48 Mon Sep 17 00:00:00 2001 From: Dave Goodwin Date: Fri, 15 Oct 2004 23:10:22 +0000 Subject: [PATCH] added environment-specific classes to write log files --- Cantera/matlab/cantera/private/ctmethods.cpp | 15 ++++++ Cantera/matlab/cantera/private/mllogger.h | 48 +++++++++++++++++++ Cantera/python/src/pycantera.cpp | 5 +- Cantera/python/src/pylogger.h | 46 ++++++++++++++++++ Cantera/python/src/pyutils.h | 15 +++--- Cantera/python/src/writelog.cpp | 49 ++++++++++++-------- Cantera/src/ct2ctml.cpp | 12 ++--- Cantera/src/global.h | 3 ++ Cantera/src/logger.h | 28 +++++++++++ Cantera/src/misc.cpp | 34 +++++++++++++- 10 files changed, 220 insertions(+), 35 deletions(-) create mode 100644 Cantera/matlab/cantera/private/mllogger.h create mode 100644 Cantera/python/src/pylogger.h create mode 100644 Cantera/src/logger.h diff --git a/Cantera/matlab/cantera/private/ctmethods.cpp b/Cantera/matlab/cantera/private/ctmethods.cpp index 53be9da87..07b0d883a 100644 --- a/Cantera/matlab/cantera/private/ctmethods.cpp +++ b/Cantera/matlab/cantera/private/ctmethods.cpp @@ -12,6 +12,8 @@ #include "mex.h" #include "../../../clib/src/ct.h" #include "ctmatutils.h" +#include "mllogger.h" +#include "../../../src/global.h" namespace Cantera { void setMatlabMode(bool m); @@ -70,6 +72,14 @@ void onedimmethods( int nlhs, mxArray *plhs[], int nrhs, void funcmethods( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ); +static Cantera::ML_Logger* _logger = 0; + +void initLogger() { + if (!_logger) { + _logger = new Cantera::ML_Logger; + Cantera::setLogger(_logger); + } +} extern "C" { @@ -77,6 +87,11 @@ extern "C" { void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { + + // create a log writer for error messages if this is the + // first MATLAB function call + initLogger(); + // flag specifying the class int iclass = getInt(prhs[0]); diff --git a/Cantera/matlab/cantera/private/mllogger.h b/Cantera/matlab/cantera/private/mllogger.h new file mode 100644 index 000000000..03b0cc2a6 --- /dev/null +++ b/Cantera/matlab/cantera/private/mllogger.h @@ -0,0 +1,48 @@ +#ifndef MLLOGGER_H +#define MLLOGGER_H + +#include "mex.h" +#include +#include "../../../src/logger.h" + +static std::string ss = "disp('"; + +namespace Cantera { + + class ML_Logger : public Logger { + public: + ML_Logger() {} + virtual ~ML_Logger() {} + + + virtual void write(const std::string& s) { + char ch = s[0]; + int n = 0; + while (ch != '\0') { + if (ch =='\n') { + ss += "');"; + mexEvalString(ss.c_str()); + ss = "disp('"; + } + else + ss += ch; + if (ch == '\'') ss += ch; + n++; + ch = s[n]; + } + } + + + virtual void error(const std::string& msg) { + std::string err = "error("+msg+");"; + mexEvalString(err.c_str()); + } + + virtual int env() { + return 1; + } + }; + +} + +#endif diff --git a/Cantera/python/src/pycantera.cpp b/Cantera/python/src/pycantera.cpp index e3bfe8e4a..f113ae18b 100644 --- a/Cantera/python/src/pycantera.cpp +++ b/Cantera/python/src/pycantera.cpp @@ -54,7 +54,8 @@ static PyObject *ErrorObject; #include "methods.h" - +#include "pylogger.h" +#include "../../src/global.h" extern "C" { /* Initialization function for the module */ @@ -69,6 +70,8 @@ extern "C" { /* Create the module and add the functions */ m = Py_InitModule("_cantera", ct_methods); import_array(); + Cantera::Logger* pylog = new Cantera::Py_Logger; + setLogger(pylog); /* Add some symbolic constants to the module */ d = PyModule_GetDict(m); diff --git a/Cantera/python/src/pylogger.h b/Cantera/python/src/pylogger.h new file mode 100644 index 000000000..ad883ecfa --- /dev/null +++ b/Cantera/python/src/pylogger.h @@ -0,0 +1,46 @@ +#ifndef PYLOGGER_H +#define PYLOGGER_H + +#include "Python.h" +#include +#include "../../src/logger.h" + +using namespace std; + +static std::string ss = "print \"\"\" "; + +namespace Cantera { + + class Py_Logger : public Logger { + public: + Py_Logger() {} + virtual ~Py_Logger() {} + + virtual void write(const string& s) { + char ch = s[0]; + int n = 0; + while (ch != '\0') { + if (ch =='\n') { + ss += "\"\"\""; + PyRun_SimpleString((char *)ss.c_str()); + ss = "print \"\"\""; + } + else + ss += ch; + n++; + ch = s[n]; + } + } + + virtual void error(const std::string& msg) { + string err = "raise \""+msg+"\""; + PyRun_SimpleString((char *)err.c_str()); + } + + virtual int env() { + return 2; + } + }; +} + +#endif diff --git a/Cantera/python/src/pyutils.h b/Cantera/python/src/pyutils.h index 2cdfb087d..b38757f8f 100755 --- a/Cantera/python/src/pyutils.h +++ b/Cantera/python/src/pyutils.h @@ -2,14 +2,17 @@ #define CTPY_UTILS #include "Python.h" +#include "../../src/global.h" static PyObject* reportCanteraError() { - char* buf = 0; - int buflen = getCanteraError(0, buf) + 1; - buf = new char[buflen+1]; - getCanteraError(buflen, buf); - PyErr_SetString(ErrorObject,buf); - delete buf; + Cantera::showErrors(); + //char* buf = 0; + //int buflen = getCanteraError(0, buf) + 1; + //buf = new char[buflen+1]; + //getCanteraError(buflen, buf); + //PyErr_SetString(ErrorObject,buf); + //delete buf; + PyErr_SetString(ErrorObject,"An exception was thrown by Cantera."); return NULL; } diff --git a/Cantera/python/src/writelog.cpp b/Cantera/python/src/writelog.cpp index 2246330af..5b9f648be 100644 --- a/Cantera/python/src/writelog.cpp +++ b/Cantera/python/src/writelog.cpp @@ -1,33 +1,42 @@ #include "Python.h" #include +#include "../../src/logger.h" + using namespace std; static std::string ss = "print \"\"\" "; namespace Cantera { - void writelog(const string& s) { - char ch = s[0]; - int n = 0; - while (ch != '\0') { - if (ch =='\n') { - ss += "\"\"\""; - PyRun_SimpleString((char *)ss.c_str()); - ss = "print \"\"\""; + class Py_Logger : public Logger { + public: + Py_Logger() {} + virtual ~Py_Logger() {} + + virtual void write(const string& s) { + char ch = s[0]; + int n = 0; + while (ch != '\0') { + if (ch =='\n') { + ss += "\"\"\""; + PyRun_SimpleString((char *)ss.c_str()); + ss = "print \"\"\""; + } + else + ss += ch; + n++; + ch = s[n]; } - else - ss += ch; - n++; - ch = s[n]; } - } - void error(const std::string& msg) { - string err = "raise \""+msg+"\""; - PyRun_SimpleString((char *)err.c_str()); - } + virtual void error(const std::string& msg) { + string err = "raise \""+msg+"\""; + PyRun_SimpleString((char *)err.c_str()); + } - int userInterface() { - return 2; - } + virtual int env() { + return 2; + } + }; } + diff --git a/Cantera/src/ct2ctml.cpp b/Cantera/src/ct2ctml.cpp index e72e1adc2..2a2751b62 100644 --- a/Cantera/src/ct2ctml.cpp +++ b/Cantera/src/ct2ctml.cpp @@ -39,12 +39,12 @@ namespace ctml { string sp = stripws(string(py)); if (sp.size() > 0) s = sp; } -#ifdef PYTHON_EXE - else { - string se = stripws(string(PYTHON_EXE)); - if (se.size() > 0) s = se; - } -#endif + //#ifdef PYTHON_EXE + //else { + //string se = stripws(string(PYTHON_EXE)); + // if (se.size() > 0) s = se; + //} + //#endif return s; } diff --git a/Cantera/src/global.h b/Cantera/src/global.h index 6995edb88..4c0e2967a 100755 --- a/Cantera/src/global.h +++ b/Cantera/src/global.h @@ -22,6 +22,7 @@ namespace Cantera { class XML_Node; + class Logger; /// Number of errors that have been encountered so far int nErrors(); @@ -99,6 +100,8 @@ namespace Cantera { /// returns 1 for MATLAB, 2 for Python, and 0 for C++ or Fortran. int userInterface(); + void setLogger(Logger* logwriter); + /** * Return the conversion factor to convert unit string 'unit' to * SI units. diff --git a/Cantera/src/logger.h b/Cantera/src/logger.h new file mode 100644 index 000000000..0cbdd3aae --- /dev/null +++ b/Cantera/src/logger.h @@ -0,0 +1,28 @@ +#ifndef CT_LOGGER_H +#define CT_LOGGER_H + +#include +using namespace std; + +namespace Cantera { + + class Logger { + public: + + Logger() {} + virtual ~Logger() {} + + virtual void write(const string& msg) { + cout << msg; + } + + virtual void error(const string& msg) { + cerr << msg << endl; + exit(-1); + } + + virtual int env() { return 0; } + }; + +} +#endif diff --git a/Cantera/src/misc.cpp b/Cantera/src/misc.cpp index aafd986c7..db304d4c1 100755 --- a/Cantera/src/misc.cpp +++ b/Cantera/src/misc.cpp @@ -22,6 +22,7 @@ #include "SpeciesThermoFactory.h" #include "ThermoFactory.h" #include "FalloffFactory.h" +#include "logger.h" //#ifndef WIN32 //#include "ctdir.h" @@ -58,6 +59,7 @@ namespace Cantera { if (sleepstr != 0) { sleep = string(sleepstr); } + logwriter = new Logger(); } virtual ~Application() { @@ -79,8 +81,10 @@ namespace Cantera { string tmp_dir; map xmlfiles; string sleep; + Logger* logwriter; }; + /// Returns a pointer to the one and only instance of Application Application* app(); @@ -118,6 +122,7 @@ namespace Cantera { return __app; } + XML_Node* get_XML_File(string file) { string path = findInputFile(file); string ff = path; @@ -255,7 +260,7 @@ namespace Cantera { if (i == 0) return; f << endl << endl; f << "************************************************" << endl; - f << " Cantera Error! " << endl; + f << " Cantera Error! " << endl; f << "************************************************" << endl << endl; int j; for (j = 0; j < i; j++) { @@ -274,7 +279,7 @@ namespace Cantera { if (i == 0) return; writelog("\n\n"); writelog("************************************************\n"); - writelog(" Cantera Error! \n"); + writelog(" Cantera Error! \n"); writelog("************************************************\n\n"); int j; for (j = 0; j < i; j++) { @@ -363,6 +368,13 @@ namespace Cantera { void addDirectory(string dir) { appinit(); if (__app->inputDirs.size() == 0) setDefaultDirectories(); + string d = stripnonprint(dir); + size_t m, n = __app->inputDirs.size(); + + // don't add if already present + for (m = 0; m < n; m++) + if (d == __app->inputDirs[m]) return; + __app->inputDirs.push_back(stripnonprint(dir)); } @@ -432,6 +444,24 @@ namespace Cantera { } } + void writelog(const string& msg) { + app()->logwriter->write(msg); + } + + void error(const string& msg) { + app()->logwriter->error(msg); + } + + int userInterface() { + return app()->logwriter->env(); + } + + void setLogger(Logger* logwriter) { + appinit(); + delete __app->logwriter; + __app->logwriter = logwriter; + } + void writelog(const char* msg) {writelog(string(msg));} doublereal toSI(string unit) {