added environment-specific classes to write log files

This commit is contained in:
Dave Goodwin 2004-10-15 23:10:22 +00:00
parent 58112591b9
commit d77ebf7ff2
10 changed files with 220 additions and 35 deletions

View file

@ -12,6 +12,8 @@
#include "mex.h" #include "mex.h"
#include "../../../clib/src/ct.h" #include "../../../clib/src/ct.h"
#include "ctmatutils.h" #include "ctmatutils.h"
#include "mllogger.h"
#include "../../../src/global.h"
namespace Cantera { namespace Cantera {
void setMatlabMode(bool m); void setMatlabMode(bool m);
@ -70,6 +72,14 @@ void onedimmethods( int nlhs, mxArray *plhs[], int nrhs,
void funcmethods( int nlhs, mxArray *plhs[], int nrhs, void funcmethods( int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[] ); const mxArray *prhs[] );
static Cantera::ML_Logger* _logger = 0;
void initLogger() {
if (!_logger) {
_logger = new Cantera::ML_Logger;
Cantera::setLogger(_logger);
}
}
extern "C" { extern "C" {
@ -77,6 +87,11 @@ extern "C" {
void mexFunction( int nlhs, mxArray *plhs[], void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ) 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 // flag specifying the class
int iclass = getInt(prhs[0]); int iclass = getInt(prhs[0]);

View file

@ -0,0 +1,48 @@
#ifndef MLLOGGER_H
#define MLLOGGER_H
#include "mex.h"
#include <string>
#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

View file

@ -54,7 +54,8 @@ static PyObject *ErrorObject;
#include "methods.h" #include "methods.h"
#include "pylogger.h"
#include "../../src/global.h"
extern "C" { extern "C" {
/* Initialization function for the module */ /* Initialization function for the module */
@ -69,6 +70,8 @@ extern "C" {
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule("_cantera", ct_methods); m = Py_InitModule("_cantera", ct_methods);
import_array(); import_array();
Cantera::Logger* pylog = new Cantera::Py_Logger;
setLogger(pylog);
/* Add some symbolic constants to the module */ /* Add some symbolic constants to the module */
d = PyModule_GetDict(m); d = PyModule_GetDict(m);

View file

@ -0,0 +1,46 @@
#ifndef PYLOGGER_H
#define PYLOGGER_H
#include "Python.h"
#include <string>
#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

View file

@ -2,14 +2,17 @@
#define CTPY_UTILS #define CTPY_UTILS
#include "Python.h" #include "Python.h"
#include "../../src/global.h"
static PyObject* reportCanteraError() { static PyObject* reportCanteraError() {
char* buf = 0; Cantera::showErrors();
int buflen = getCanteraError(0, buf) + 1; //char* buf = 0;
buf = new char[buflen+1]; //int buflen = getCanteraError(0, buf) + 1;
getCanteraError(buflen, buf); //buf = new char[buflen+1];
PyErr_SetString(ErrorObject,buf); //getCanteraError(buflen, buf);
delete buf; //PyErr_SetString(ErrorObject,buf);
//delete buf;
PyErr_SetString(ErrorObject,"An exception was thrown by Cantera.");
return NULL; return NULL;
} }

View file

@ -1,33 +1,42 @@
#include "Python.h" #include "Python.h"
#include <string> #include <string>
#include "../../src/logger.h"
using namespace std; using namespace std;
static std::string ss = "print \"\"\" "; static std::string ss = "print \"\"\" ";
namespace Cantera { namespace Cantera {
void writelog(const string& s) { class Py_Logger : public Logger {
char ch = s[0]; public:
int n = 0; Py_Logger() {}
while (ch != '\0') { virtual ~Py_Logger() {}
if (ch =='\n') {
ss += "\"\"\""; virtual void write(const string& s) {
PyRun_SimpleString((char *)ss.c_str()); char ch = s[0];
ss = "print \"\"\""; 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) { virtual void error(const std::string& msg) {
string err = "raise \""+msg+"\""; string err = "raise \""+msg+"\"";
PyRun_SimpleString((char *)err.c_str()); PyRun_SimpleString((char *)err.c_str());
} }
int userInterface() { virtual int env() {
return 2; return 2;
} }
};
} }

View file

@ -39,12 +39,12 @@ namespace ctml {
string sp = stripws(string(py)); string sp = stripws(string(py));
if (sp.size() > 0) s = sp; if (sp.size() > 0) s = sp;
} }
#ifdef PYTHON_EXE //#ifdef PYTHON_EXE
else { //else {
string se = stripws(string(PYTHON_EXE)); //string se = stripws(string(PYTHON_EXE));
if (se.size() > 0) s = se; // if (se.size() > 0) s = se;
} //}
#endif //#endif
return s; return s;
} }

View file

@ -22,6 +22,7 @@
namespace Cantera { namespace Cantera {
class XML_Node; class XML_Node;
class Logger;
/// Number of errors that have been encountered so far /// Number of errors that have been encountered so far
int nErrors(); int nErrors();
@ -99,6 +100,8 @@ namespace Cantera {
/// returns 1 for MATLAB, 2 for Python, and 0 for C++ or Fortran. /// returns 1 for MATLAB, 2 for Python, and 0 for C++ or Fortran.
int userInterface(); int userInterface();
void setLogger(Logger* logwriter);
/** /**
* Return the conversion factor to convert unit string 'unit' to * Return the conversion factor to convert unit string 'unit' to
* SI units. * SI units.

28
Cantera/src/logger.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef CT_LOGGER_H
#define CT_LOGGER_H
#include <iostream>
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

View file

@ -22,6 +22,7 @@
#include "SpeciesThermoFactory.h" #include "SpeciesThermoFactory.h"
#include "ThermoFactory.h" #include "ThermoFactory.h"
#include "FalloffFactory.h" #include "FalloffFactory.h"
#include "logger.h"
//#ifndef WIN32 //#ifndef WIN32
//#include "ctdir.h" //#include "ctdir.h"
@ -58,6 +59,7 @@ namespace Cantera {
if (sleepstr != 0) { if (sleepstr != 0) {
sleep = string(sleepstr); sleep = string(sleepstr);
} }
logwriter = new Logger();
} }
virtual ~Application() { virtual ~Application() {
@ -79,9 +81,11 @@ namespace Cantera {
string tmp_dir; string tmp_dir;
map<string, XML_Node*> xmlfiles; map<string, XML_Node*> xmlfiles;
string sleep; string sleep;
Logger* logwriter;
}; };
/// Returns a pointer to the one and only instance of Application /// Returns a pointer to the one and only instance of Application
Application* app(); Application* app();
@ -118,6 +122,7 @@ namespace Cantera {
return __app; return __app;
} }
XML_Node* get_XML_File(string file) { XML_Node* get_XML_File(string file) {
string path = findInputFile(file); string path = findInputFile(file);
string ff = path; string ff = path;
@ -255,7 +260,7 @@ namespace Cantera {
if (i == 0) return; if (i == 0) return;
f << endl << endl; f << endl << endl;
f << "************************************************" << endl; f << "************************************************" << endl;
f << " Cantera Error! " << endl; f << " Cantera Error! " << endl;
f << "************************************************" << endl << endl; f << "************************************************" << endl << endl;
int j; int j;
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
@ -274,7 +279,7 @@ namespace Cantera {
if (i == 0) return; if (i == 0) return;
writelog("\n\n"); writelog("\n\n");
writelog("************************************************\n"); writelog("************************************************\n");
writelog(" Cantera Error! \n"); writelog(" Cantera Error! \n");
writelog("************************************************\n\n"); writelog("************************************************\n\n");
int j; int j;
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
@ -363,6 +368,13 @@ namespace Cantera {
void addDirectory(string dir) { void addDirectory(string dir) {
appinit(); appinit();
if (__app->inputDirs.size() == 0) setDefaultDirectories(); 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)); __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));} void writelog(const char* msg) {writelog(string(msg));}
doublereal toSI(string unit) { doublereal toSI(string unit) {