~jdpipe/ascend/trunk-old

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "config.h"
#include "reporter.h"

#include <cstdio>
#include <iostream>
using namespace std;

#ifndef ASCXX_USE_PYTHON
# error "Where's ASCXX_USE_PYTHON?"
#endif


static const int REPORTER_MAX_ERROR_MSG = ERROR_REPORTER_MAX_MSG;

#ifdef ASCXX_USE_PYTHON
// Python-invoking callback function
int reporter_error_python(ERROR_REPORTER_CALLBACK_ARGS){
	Reporter *reporter = Reporter::Instance();
	return reporter->reportErrorPython(ERROR_REPORTER_CALLBACK_VARS);
}
#endif

Reporter::Reporter(){
	error_reporter_set_callback(NULL);
}

Reporter *Reporter::_instance;

Reporter *
Reporter::Instance(){
	if(_instance==0){
		_instance = new Reporter();
	}
	return _instance;
}

Reporter *getReporter(){
	return Reporter::Instance();
}

Reporter::~Reporter(){
	error_reporter_set_callback(NULL);
}

void
Reporter::setErrorCallback(error_reporter_callback_t callback, void *client_data){
	this->client_data = client_data;
	error_reporter_set_callback(callback);
}

/*
int
Reporter::reportError(ERROR_REPORTER_CALLBACK_ARGS){
	char msg[REPORTER_MAX_ERROR_MSG];
	vsnprintf(msg,REPORTER_MAX_ERROR_MSG,fmt,args);
	cerr << char(27) << "[32;1m" << msg << char(27) << "[0m";
	return strlen(msg) + 11; // 11 chars worth of escape codes
}
*/

#ifdef ASCXX_USE_PYTHON
int
Reporter::reportErrorPython(ERROR_REPORTER_CALLBACK_ARGS){
	PyObject *pyfunc, *pyarglist, *pyresult;
	pyfunc = (PyObject *)client_data;

	char msg[REPORTER_MAX_ERROR_MSG];
	vsnprintf(msg,REPORTER_MAX_ERROR_MSG,fmt,*args);

	pyarglist = Py_BuildValue("(H,s,i,s#)",sev,filename,line,msg,strlen(msg));             // Build argument list
	pyresult = PyEval_CallObject(pyfunc,pyarglist);     // Call Python
	Py_DECREF(pyarglist);                           // Trash arglist

   	int res = 0;
	if (pyresult) {                                 // If no errors, return int
    	long long_res = PyInt_AsLong(pyresult);
		res = int(long_res);
	}

	Py_XDECREF(pyresult);
	return res;
}

void
Reporter::setPythonErrorCallback(PyObject *pyfunc) {
	setErrorCallback(reporter_error_python, (void *) pyfunc);
	Py_INCREF(pyfunc);
	is_python = true;
}

void
Reporter::clearPythonErrorCallback(){
	if(is_python){
		PyObject *pyfunc = (PyObject *)client_data;
		Py_DECREF(pyfunc);
		is_python=false;
	}
	setErrorCallback(NULL);
}

#endif