~jdpipe/ascend/trunk-old

2306 by jpye
Merging in refactor of the C++ code, which is moved out of 'pygtk' and into 'ascxx'.
1
#include "config.h"
2
#include "reporter.h"
3
4
#include <cstdio>
5
#include <iostream>
6
using namespace std;
7
8
#ifndef ASCXX_USE_PYTHON
9
# error "Where's ASCXX_USE_PYTHON?"
10
#endif
11
12
13
static const int REPORTER_MAX_ERROR_MSG = ERROR_REPORTER_MAX_MSG;
14
15
#ifdef ASCXX_USE_PYTHON
16
// Python-invoking callback function
17
int reporter_error_python(ERROR_REPORTER_CALLBACK_ARGS){
18
	Reporter *reporter = Reporter::Instance();
19
	return reporter->reportErrorPython(ERROR_REPORTER_CALLBACK_VARS);
20
}
21
#endif
22
23
Reporter::Reporter(){
24
	error_reporter_set_callback(NULL);
25
}
26
27
Reporter *Reporter::_instance;
28
29
Reporter *
30
Reporter::Instance(){
31
	if(_instance==0){
32
		_instance = new Reporter();
33
	}
34
	return _instance;
35
}
36
37
Reporter *getReporter(){
38
	return Reporter::Instance();
39
}
40
41
Reporter::~Reporter(){
42
	error_reporter_set_callback(NULL);
43
}
44
45
void
46
Reporter::setErrorCallback(error_reporter_callback_t callback, void *client_data){
47
	this->client_data = client_data;
48
	error_reporter_set_callback(callback);
49
}
50
51
/*
52
int
53
Reporter::reportError(ERROR_REPORTER_CALLBACK_ARGS){
54
	char msg[REPORTER_MAX_ERROR_MSG];
55
	vsnprintf(msg,REPORTER_MAX_ERROR_MSG,fmt,args);
56
	cerr << char(27) << "[32;1m" << msg << char(27) << "[0m";
57
	return strlen(msg) + 11; // 11 chars worth of escape codes
58
}
59
*/
60
61
#ifdef ASCXX_USE_PYTHON
62
int
63
Reporter::reportErrorPython(ERROR_REPORTER_CALLBACK_ARGS){
64
	PyObject *pyfunc, *pyarglist, *pyresult;
65
	pyfunc = (PyObject *)client_data;
66
67
	char msg[REPORTER_MAX_ERROR_MSG];
2828 by jpye
Merged relerrorlist branch back to trunk.
68
	vsnprintf(msg,REPORTER_MAX_ERROR_MSG,fmt,*args);
2306 by jpye
Merging in refactor of the C++ code, which is moved out of 'pygtk' and into 'ascxx'.
69
70
	pyarglist = Py_BuildValue("(H,s,i,s#)",sev,filename,line,msg,strlen(msg));             // Build argument list
71
	pyresult = PyEval_CallObject(pyfunc,pyarglist);     // Call Python
72
	Py_DECREF(pyarglist);                           // Trash arglist
73
74
   	int res = 0;
75
	if (pyresult) {                                 // If no errors, return int
76
    	long long_res = PyInt_AsLong(pyresult);
77
		res = int(long_res);
78
	}
79
80
	Py_XDECREF(pyresult);
81
	return res;
82
}
83
84
void
85
Reporter::setPythonErrorCallback(PyObject *pyfunc) {
86
	setErrorCallback(reporter_error_python, (void *) pyfunc);
87
	Py_INCREF(pyfunc);
88
	is_python = true;
89
}
90
91
void
92
Reporter::clearPythonErrorCallback(){
93
	if(is_python){
94
		PyObject *pyfunc = (PyObject *)client_data;
95
		Py_DECREF(pyfunc);
96
		is_python=false;
97
	}
98
	setErrorCallback(NULL);
99
}
100
101
#endif