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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 132 - (hide annotations) (download) (as text)
Wed Dec 21 00:14:13 2005 UTC (14 years, 11 months ago) by johnpye
File MIME type: text/x-c++src
File size: 2344 byte(s)
Adding the PyGTK interface to CMU repository. Whaddyareckon?
1 johnpye 132 #ifdef ASCXX_USE_PYTHON
2     # include <Python.h>
3     #endif
4    
5     #include <iostream>
6     using namespace std;
7    
8     #include "reporter.h"
9    
10     static const int REPORTER_MAX_ERROR_MSG = 1024;
11    
12     #ifdef ASCXX_USE_PYTHON
13     // Python-invoking callback function
14     int reporter_error_python(ERROR_REPORTER_CALLBACK_ARGS){
15     Reporter *reporter = Reporter::Instance();
16     return reporter->reportErrorPython(ERROR_REPORTER_CALLBACK_VARS);
17     }
18     #endif
19    
20     Reporter::Reporter(){
21     error_reporter_set_callback(NULL);
22     }
23    
24     Reporter *Reporter::_instance;
25    
26     Reporter *
27     Reporter::Instance(){
28     if(_instance==0){
29     _instance = new Reporter();
30     }
31     return _instance;
32     }
33    
34     Reporter *getReporter(){
35     return Reporter::Instance();
36     }
37    
38     Reporter::~Reporter(){
39     error_reporter_set_callback(NULL);
40     }
41    
42     void
43     Reporter::setErrorCallback(error_reporter_callback_t callback, void *client_data){
44     this->client_data = client_data;
45     error_reporter_set_callback(callback);
46     }
47    
48     /*
49     int
50     Reporter::reportError(ERROR_REPORTER_CALLBACK_ARGS){
51     char msg[REPORTER_MAX_ERROR_MSG];
52     vsnprintf(msg,REPORTER_MAX_ERROR_MSG,fmt,args);
53     cerr << char(27) << "[32;1m" << msg << char(27) << "[0m";
54     return strlen(msg) + 11; // 11 chars worth of escape codes
55     }
56     */
57    
58     #ifdef ASCXX_USE_PYTHON
59     int
60     Reporter::reportErrorPython(ERROR_REPORTER_CALLBACK_ARGS){
61     PyObject *pyfunc, *pyarglist, *pyresult;
62     pyfunc = (PyObject *)client_data;
63    
64     char msg[REPORTER_MAX_ERROR_MSG];
65     vsnprintf(msg,REPORTER_MAX_ERROR_MSG,fmt,args);
66    
67     //cerr << "reportErrorPython: msg=" << msg ;
68     //cerr << "reportErrorPython: pyfunc=" << pyfunc << endl;
69    
70     pyarglist = Py_BuildValue("(H,z,i,z)",sev,filename,line,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     }else{
79     //cerr << "pyresult = 0"<< endl;
80     }
81    
82     Py_XDECREF(pyresult);
83     return res;
84     }
85    
86     void
87     Reporter::setPythonErrorCallback(PyObject *pyfunc) {
88     setErrorCallback(reporter_error_python, (void *) pyfunc);
89     Py_INCREF(pyfunc);
90     is_python = true;
91     }
92    
93     void
94     Reporter::clearPythonErrorCallback(){
95     if(is_python){
96     PyObject *pyfunc = (PyObject *)client_data;
97     Py_DECREF(pyfunc);
98     is_python=false;
99     }
100     setErrorCallback(NULL);
101     }
102    
103     #endif

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