diff --git a/src/q3py.c b/src/q3py.c index f2f64e1..90d5b55 100644 --- a/src/q3py.c +++ b/src/q3py.c @@ -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. */ @@ -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; } @@ -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; } @@ -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(); } } @@ -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(); @@ -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(); } @@ -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)) { @@ -225,7 +238,9 @@ 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(); } } @@ -233,7 +248,10 @@ static void init_python() { 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(); } @@ -242,7 +260,9 @@ 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(); } } @@ -250,11 +270,15 @@ static void init_python() { 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? */