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 "../../../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]);

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 "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);

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
#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;
}

View file

@ -1,33 +1,42 @@
#include "Python.h"
#include <string>
#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;
}
};
}

View file

@ -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;
}

View file

@ -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.

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 "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<string, XML_Node*> 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) {