/[ascend]/trunk/pygtk/interface/reporter.cpp
ViewVC logotype

Contents of /trunk/pygtk/interface/reporter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 190 - (show annotations) (download) (as text)
Mon Jan 16 08:53:30 2006 UTC (14 years, 10 months ago) by johnpye
File MIME type: text/x-c++src
File size: 2338 byte(s)
- Simplifying many 'error_reporter' calls with new macros:
    ERROR_REPORTER_NOLINE and ERROR_REPORTER_HERE
- Adding some cygwin cases in the PyGTK build.
1 #ifdef ASCXX_USE_PYTHON
2 # include <Python.h>
3 #endif
4
5 #include <cstdio>
6 #include <iostream>
7 using namespace std;
8
9 #include "reporter.h"
10
11 static const int REPORTER_MAX_ERROR_MSG = 1024;
12
13 #ifdef ASCXX_USE_PYTHON
14 // Python-invoking callback function
15 int reporter_error_python(ERROR_REPORTER_CALLBACK_ARGS){
16 Reporter *reporter = Reporter::Instance();
17 return reporter->reportErrorPython(ERROR_REPORTER_CALLBACK_VARS);
18 }
19 #endif
20
21 Reporter::Reporter(){
22 error_reporter_set_callback(NULL);
23 }
24
25 Reporter *Reporter::_instance;
26
27 Reporter *
28 Reporter::Instance(){
29 if(_instance==0){
30 _instance = new Reporter();
31 }
32 return _instance;
33 }
34
35 Reporter *getReporter(){
36 return Reporter::Instance();
37 }
38
39 Reporter::~Reporter(){
40 error_reporter_set_callback(NULL);
41 }
42
43 void
44 Reporter::setErrorCallback(error_reporter_callback_t callback, void *client_data){
45 this->client_data = client_data;
46 error_reporter_set_callback(callback);
47 }
48
49 /*
50 int
51 Reporter::reportError(ERROR_REPORTER_CALLBACK_ARGS){
52 char msg[REPORTER_MAX_ERROR_MSG];
53 vsnprintf(msg,REPORTER_MAX_ERROR_MSG,fmt,args);
54 cerr << char(27) << "[32;1m" << msg << char(27) << "[0m";
55 return strlen(msg) + 11; // 11 chars worth of escape codes
56 }
57 */
58
59 #ifdef ASCXX_USE_PYTHON
60 int
61 Reporter::reportErrorPython(ERROR_REPORTER_CALLBACK_ARGS){
62 PyObject *pyfunc, *pyarglist, *pyresult;
63 pyfunc = (PyObject *)client_data;
64
65 char msg[REPORTER_MAX_ERROR_MSG];
66 vsprintf(msg,fmt,args);
67
68 //cerr << "reportErrorPython: msg=" << msg ;
69 //cerr << "reportErrorPython: pyfunc=" << pyfunc << endl;
70
71 pyarglist = Py_BuildValue("(H,z,i,z)",sev,filename,line,msg); // Build argument list
72 pyresult = PyEval_CallObject(pyfunc,pyarglist); // Call Python
73 Py_DECREF(pyarglist); // Trash arglist
74
75 int res = 0;
76 if (pyresult) { // If no errors, return int
77 long long_res = PyInt_AsLong(pyresult);
78 res = int(long_res);
79 }else{
80 //cerr << "pyresult = 0"<< endl;
81 }
82
83 Py_XDECREF(pyresult);
84 return res;
85 }
86
87 void
88 Reporter::setPythonErrorCallback(PyObject *pyfunc) {
89 setErrorCallback(reporter_error_python, (void *) pyfunc);
90 Py_INCREF(pyfunc);
91 is_python = true;
92 }
93
94 void
95 Reporter::clearPythonErrorCallback(){
96 if(is_python){
97 PyObject *pyfunc = (PyObject *)client_data;
98 Py_DECREF(pyfunc);
99 is_python=false;
100 }
101 setErrorCallback(NULL);
102 }
103
104 #endif

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22