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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 507 - (hide annotations) (download) (as text)
Wed Apr 19 02:44:25 2006 UTC (14 years, 7 months ago) by johnpye
File MIME type: text/x-c++src
File size: 2370 byte(s)
Fixing for ubuntu GCC 4
1 johnpye 507 #include "config.h"
2     #include "reporter.h"
3 johnpye 132
4 johnpye 190 #include <cstdio>
5 johnpye 132 #include <iostream>
6     using namespace std;
7    
8 johnpye 500 #ifndef ASCXX_USE_PYTHON
9     # error "Where's ASCXX_USE_PYTHON?"
10     #endif
11    
12 johnpye 132
13     static const int REPORTER_MAX_ERROR_MSG = 1024;
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 johnpye 500
67 johnpye 132 char msg[REPORTER_MAX_ERROR_MSG];
68 johnpye 190 vsprintf(msg,fmt,args);
69 johnpye 132
70     //cerr << "reportErrorPython: msg=" << msg ;
71     //cerr << "reportErrorPython: pyfunc=" << pyfunc << endl;
72 johnpye 500
73 johnpye 132 pyarglist = Py_BuildValue("(H,z,i,z)",sev,filename,line,msg); // Build argument list
74     pyresult = PyEval_CallObject(pyfunc,pyarglist); // Call Python
75     Py_DECREF(pyarglist); // Trash arglist
76    
77     int res = 0;
78     if (pyresult) { // If no errors, return int
79     long long_res = PyInt_AsLong(pyresult);
80     res = int(long_res);
81     }else{
82     //cerr << "pyresult = 0"<< endl;
83     }
84 johnpye 500
85 johnpye 132 Py_XDECREF(pyresult);
86 johnpye 500 return res;
87 johnpye 132 }
88    
89     void
90     Reporter::setPythonErrorCallback(PyObject *pyfunc) {
91     setErrorCallback(reporter_error_python, (void *) pyfunc);
92     Py_INCREF(pyfunc);
93     is_python = true;
94     }
95    
96     void
97     Reporter::clearPythonErrorCallback(){
98     if(is_python){
99     PyObject *pyfunc = (PyObject *)client_data;
100     Py_DECREF(pyfunc);
101     is_python=false;
102     }
103     setErrorCallback(NULL);
104     }
105    
106     #endif

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