Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Commit

Permalink
Add q3py logging functions
Browse files Browse the repository at this point in the history
Add q3py_log() and q3py_error() functions in preparation for issue #9
  • Loading branch information
robo9k committed Jun 1, 2014
1 parent 2e1d805 commit bda6af5
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/q3py.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ static syscallptr *q3_syscall = NULL;
static PyObject *q3py_vmMain = NULL;


static void q3py_log(const char* message) {
fprintf(stdout, "Q3PY: %s\n", message);
}

static void q3py_error(const char* message) {
fprintf(stderr, "Q3PY: %s\n", message);
}


/**
* Tries to finalize Python, then exits.
*/
Expand Down Expand Up @@ -102,7 +111,7 @@ PyObject* PyInit_q3py() {

PyObject *module = PyModule_Create(&q3pymodule);
if (NULL == module) {
fprintf(stderr, "Failed to create module\n");
q3py_error("Failed to create module '" Q3PY_MODULE_NAME "'");
return NULL;
}

Expand All @@ -112,13 +121,14 @@ PyObject* PyInit_q3py() {
PyObject *capsule = PyCapsule_New((void *)Q3Py_API, Q3PY_MODULE_NAME "." Q3PY_CAPI_CAPSULE_NAME, NULL);
if (capsule != NULL) {
if (-1 == PyModule_AddObject(module, Q3PY_CAPI_CAPSULE_NAME, capsule)) {
fprintf(stderr, "Failed to add capsule to module\n");
q3py_error("Failed to add capsule '" Q3PY_CAPI_CAPSULE_NAME "' "
"to module '" Q3PY_MODULE_NAME "'");
return NULL;
}
}
else {
PyErr_Print();
fprintf(stderr, "Could not create capsule\n");
q3py_error("Could not create capsule");
return NULL;
}

Expand All @@ -134,7 +144,7 @@ PyObject* PyInit_q3py() {
*/
void check_vmMainPy() {
if (NULL == q3py_vmMain) {
fprintf(stderr, "vmMain Python method has not been set\n");
q3py_error("vmMain Python method has not been set");
q3py_exit();
}
}
Expand Down Expand Up @@ -167,13 +177,13 @@ Q3_API intptr_t vmMain(int command, int arg0, int arg1, int arg2,
if (PyErr_Occurred()) {
PyErr_Print();
}
fprintf(stderr, "vmMain result is not a long\n");
q3py_error("vmMain result is not a long");
Py_XDECREF(result);
}
}
else {
PyErr_Print();
fprintf(stderr, "Failed building value for vmMain call\n");
q3py_error("Failed building value for vmMain call");
}

q3py_exit();
Expand All @@ -189,7 +199,7 @@ Q3_API intptr_t vmMain(int command, int arg0, int arg1, int arg2,
static void init_python() {
const int inittab = PyImport_AppendInittab(Q3PY_MODULE_NAME, &PyInit_q3py);
if (-1 == inittab) {
fprintf(stderr, "Could not append Python module '%s'\n", Q3PY_MODULE_NAME);
q3py_error("Could not append Python module '" Q3PY_MODULE_NAME "'");
q3py_exit();
}

Expand All @@ -213,6 +223,9 @@ static void init_python() {
PyObject *module = PyImport_Import(moduleName);
Py_DECREF(moduleName);

/* Buffer for formatted error messages, size chosen arbitrarily */
char error_message_buffer[128];

if (module != NULL) {
PyObject *function = PyObject_GetAttrString(module, funcname);
if (function && PyCallable_Check(function)) {
Expand All @@ -225,15 +238,20 @@ static void init_python() {
Py_DECREF(module);

PyErr_Print();
fprintf(stderr, "Calling init function '%s' failed\n", funcname);
snprintf(error_message_buffer, sizeof(error_message_buffer),
"Calling init method '%s' failed", funcname);
q3py_error(error_message_buffer);
q3py_exit();
}
}
else {
if (PyErr_Occurred()) {
PyErr_Print();
}
fprintf(stderr, "Cannot find function '%s'\n", funcname);
snprintf(error_message_buffer, sizeof(error_message_buffer),
"Can not find method '%s' in module '%s'",
funcname, modname);
q3py_error(error_message_buffer);
q3py_exit();
}

Expand All @@ -242,19 +260,25 @@ static void init_python() {
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load module '%s'\n", modname);
snprintf(error_message_buffer, sizeof(error_message_buffer),
"Failed to load module '%s'", modname);
q3py_error(error_message_buffer);
q3py_exit();
}
}

Q3_API void dllEntry(const syscallptr * const syscallptr) {
/* A NULL pointer is the only invalid value we can check for */
if (NULL == syscallptr) {
fprintf(stderr, "NULL syscall pointer\n");
q3py_error("NULL syscall pointer");
q3py_exit();
}

q3_syscall = syscallptr;
char message_buffer[128];
snprintf(message_buffer, sizeof(message_buffer),
"dllEntry called with syscall %p", syscallptr);
q3py_log(message_buffer);

init_python();
/* TODO: Split initialization of Python and dllEntry? */
Expand Down

0 comments on commit bda6af5

Please sign in to comment.