Skip to content

Commit e834ec5

Browse files
committed
Added support for logging level 'log' as well as 'debug'; made specification of logging levels no longer case sensitive: 'debug' works as well as 'DEBUG'; Overhauled PRINT_DEBUG statements to clean up, remove redundancy, and make some of them LOG_PRINT statements.
1 parent c99497c commit e834ec5

File tree

1 file changed

+26
-49
lines changed

1 file changed

+26
-49
lines changed

xtext/org.icyphy.linguafranca/src/lib/Python/pythontarget.c

+26-49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

3232
#include "pythontarget.h"
33+
#include "util.h"
3334

3435
//////////// set Function(s) /////////////
3536
/**
@@ -71,7 +72,7 @@ static PyObject* py_SET(PyObject *self, PyObject *args) {
7172

7273
generic_port_instance_struct* port = PyCapsule_GetPointer(p->port, "port");
7374
if (port == NULL) {
74-
fprintf(stderr, "Null pointer received.\n");
75+
error_print("Null pointer received.");
7576
exit(1);
7677
}
7778

@@ -124,7 +125,7 @@ static PyObject* py_schedule(PyObject *self, PyObject *args) {
124125

125126
void* action = PyCapsule_GetPointer(act->action,"action");
126127
if (action == NULL) {
127-
fprintf(stderr, "Null pointer received.\n");
128+
error_print("Null pointer received.");
128129
exit(1);
129130
}
130131

@@ -170,7 +171,7 @@ static PyObject* py_schedule_copy(PyObject *self, PyObject *args) {
170171

171172
void* action = PyCapsule_GetPointer(act->action,"action");
172173
if (action == NULL) {
173-
fprintf(stderr, "Null pointer received.\n");
174+
error_print("Null pointer received.");
174175
exit(1);
175176
}
176177

@@ -320,7 +321,7 @@ port_iter(PyObject *self) {
320321

321322
generic_port_instance_struct **cport = (generic_port_instance_struct **)PyCapsule_GetPointer(port->port,"port");
322323
if (cport == NULL) {
323-
fprintf(stderr, "Null pointer received.\n");
324+
error_print("Null pointer received.");
324325
exit(1);
325326
}
326327

@@ -386,8 +387,7 @@ port_capsule_get_item(PyObject *self, PyObject *item) {
386387
generic_port_instance_struct **cport = (generic_port_instance_struct **)PyCapsule_GetPointer(port->port,"port");
387388
if (cport == NULL)
388389
{
389-
fprintf(stderr, "Null pointer received.\n");
390-
exit(1);
390+
error_print_and_exit("Null pointer received.");
391391
}
392392

393393
//Py_INCREF(cport[index]->value);
@@ -433,9 +433,7 @@ port_capsule_assign_get_item(PyObject *self, PyObject *item, PyObject* value) {
433433
static Py_ssize_t
434434
port_length(PyObject *self) {
435435
generic_port_capsule_struct* port = (generic_port_capsule_struct*)self;
436-
#ifdef VERBOSE
437-
printf("Getting the length, which is %d.\n", port->width);
438-
#endif
436+
DEBUG_PRINT("Getting the length, which is %d.", port->width);
439437
return (Py_ssize_t)port->width;
440438
}
441439

@@ -834,15 +832,13 @@ PyObject* convert_C_port_to_py(void* port, int width) {
834832
// Create the action struct in Python
835833
PyObject* cap = PyObject_GC_New(generic_port_capsule_struct, &port_capsule_t);
836834
if (cap == NULL) {
837-
fprintf(stderr, "Failed to convert port.\n");
838-
exit(1);
835+
error_print_and_exit("Failed to convert port.");
839836
}
840837

841838
// Create the capsule to hold the void* port
842839
PyObject* capsule = PyCapsule_New(port, "port", NULL);
843840
if (capsule == NULL) {
844-
fprintf(stderr, "Failed to convert port.\n");
845-
exit(1);
841+
error_print_and_exit("Failed to convert port.");
846842
}
847843

848844
// Fill in the Python port struct
@@ -902,15 +898,13 @@ PyObject* convert_C_action_to_py(void* action) {
902898
// Create the action struct in Python
903899
PyObject* cap = PyObject_GC_New(generic_action_capsule_struct, &action_capsule_t);
904900
if (cap == NULL) {
905-
fprintf(stderr, "Failed to convert action.\n");
906-
exit(1);
901+
error_print_and_exit("Failed to convert action.");
907902
}
908903

909904
// Create the capsule to hold the void* action
910905
PyObject* capsule = PyCapsule_New(action, "action", NULL);
911906
if (capsule == NULL) {
912-
fprintf(stderr, "Failed to convert action.\n");
913-
exit(1);
907+
error_print_and_exit("Failed to convert action.");
914908
}
915909

916910
// Fill in the Python action struct
@@ -964,9 +958,7 @@ get_python_function(string module, string class, int instance_id, string func) {
964958
is_initialized = 1;
965959
}
966960

967-
#ifdef VERBOSE
968-
printf("Starting the function start()\n");
969-
#endif
961+
DEBUG_PRINT("Starting the function start().");
970962

971963
// Necessary PyObject variables to load the react() function from test.py
972964
PyObject *pFileName, *pModule, *pDict, *pClasses, *pClass, *pFunc;
@@ -976,9 +968,7 @@ get_python_function(string module, string class, int instance_id, string func) {
976968
// Initialize the Python interpreter
977969
Py_Initialize();
978970

979-
#ifdef VERBOSE
980-
printf("Initialized the Python interpreter.\n");
981-
#endif
971+
DEBUG_PRINT("Initialized the Python interpreter.");
982972

983973
// If the Python module is already loaded, skip this.
984974
if (globalPythonModule == NULL) {
@@ -989,8 +979,7 @@ get_python_function(string module, string class, int instance_id, string func) {
989979
char cwd[PATH_MAX];
990980
if ( getcwd(cwd, sizeof(cwd)) == NULL)
991981
{
992-
fprintf(stderr, "Failed to get the current working directory.\n");
993-
exit(0);
982+
error_print_and_exit("Failed to get the current working directory.");
994983
}
995984

996985
wchar_t wcwd[PATH_MAX];
@@ -999,15 +988,11 @@ get_python_function(string module, string class, int instance_id, string func) {
999988

1000989
Py_SetPath(wcwd);
1001990

1002-
#ifdef VERBOSE
1003-
printf("Loading module %s in %s.\n", module, cwd);
1004-
#endif
991+
DEBUG_PRINT("Loading module %s in %s.", module, cwd);
1005992

1006993
pModule = PyImport_Import(pFileName);
1007994

1008-
#ifdef VERBOSE
1009-
printf("Loaded module %p.\n", pModule);
1010-
#endif
995+
DEBUG_PRINT("Loaded module %p.", pModule);
1011996

1012997
// Free the memory occupied by pFileName
1013998
Py_DECREF(pFileName);
@@ -1018,7 +1003,7 @@ get_python_function(string module, string class, int instance_id, string func) {
10181003
pDict = PyModule_GetDict(pModule);
10191004
if (pDict == NULL) {
10201005
PyErr_Print();
1021-
fprintf(stderr, "Failed to load contents of module %s.\n", module);
1006+
error_print("Failed to load contents of module %s.", module);
10221007
return 1;
10231008
}
10241009

@@ -1040,7 +1025,7 @@ get_python_function(string module, string class, int instance_id, string func) {
10401025
pClasses = PyDict_GetItem(globalPythonModuleDict, list_name);
10411026
if (pClasses == NULL){
10421027
PyErr_Print();
1043-
fprintf(stderr, "Failed to load class list \"%s\" in module %s.\n", class, module);
1028+
error_print("Failed to load class list \"%s\" in module %s.", class, module);
10441029
return 1;
10451030
}
10461031

@@ -1049,27 +1034,21 @@ get_python_function(string module, string class, int instance_id, string func) {
10491034
pClass = PyList_GetItem(pClasses, instance_id);
10501035
if (pClass == NULL) {
10511036
PyErr_Print();
1052-
fprintf(stderr, "Failed to load class \"%s[%d]\" in module %s.\n", class, instance_id, module);
1037+
error_print("Failed to load class \"%s[%d]\" in module %s.", class, instance_id, module);
10531038
return 1;
10541039
}
10551040

1056-
#ifdef VERBOSE
1057-
printf("Loading function %s.\n", func);
1058-
#endif
1041+
DEBUG_PRINT("Loading function %s.", func);
1042+
10591043
// Get the function react from test.py
10601044
pFunc = PyObject_GetAttrString(pClass, func);
10611045

1062-
#ifdef VERBOSE
1063-
printf("Loaded function %p.\n", pFunc);
1064-
#endif
1065-
1046+
DEBUG_PRINT("Loaded function %p.", pFunc);
10661047

10671048
// Check if the funciton is loaded properly
10681049
// and if it is callable
10691050
if (pFunc && PyCallable_Check(pFunc)) {
1070-
#ifdef VERBOSE
1071-
printf("Attempting to call function %s from class %s[%d].\n", func , class, instance_id);
1072-
#endif
1051+
DEBUG_PRINT("Calling function %s from class %s[%d].", func , class, instance_id);
10731052
Py_INCREF(pFunc);
10741053
return pFunc;
10751054
}
@@ -1079,19 +1058,17 @@ get_python_function(string module, string class, int instance_id, string func) {
10791058
{
10801059
PyErr_Print();
10811060
}
1082-
fprintf(stderr, "Function %s was not found or is not callable.\n", func);
1061+
error_print("Function %s was not found or is not callable.", func);
10831062
}
10841063
Py_XDECREF(pFunc);
10851064
Py_DECREF(globalPythonModule);
10861065
}
10871066
else {
10881067
PyErr_Print();
1089-
fprintf(stderr, "Failed to load \"%s\"\n", module);
1068+
error_print("Failed to load \"%s\".", module);
10901069
}
10911070

1092-
#ifdef VERBOSE
1093-
printf("Done with start()\n");
1094-
#endif
1071+
DEBUG_PRINT("Done with start().");
10951072

10961073
if (is_initialized == 0) {
10971074
/* We are the first to initilize the Pyton interpreter. Destroy it when done. */

0 commit comments

Comments
 (0)