1919#include "SDL_opengles2.h"
2020
2121#define ENTRYPOINT_MAXLEN 128
22- #define LOG (x ) __android_log_write(ANDROID_LOG_INFO, "python", (x))
22+ #define LOG (n , x ) __android_log_write(ANDROID_LOG_INFO, (n), (x))
23+ #define LOGP (x ) LOG("python", (x))
2324
2425static PyObject * androidembed_log (PyObject * self , PyObject * args ) {
2526 char * logstr = NULL ;
2627 if (!PyArg_ParseTuple (args , "s" , & logstr )) {
2728 return NULL ;
2829 }
29- LOG (logstr );
30+ LOG (getenv ( "PYTHON_NAME" ), logstr );
3031 Py_RETURN_NONE ;
3132}
3233
@@ -70,20 +71,27 @@ int main(int argc, char *argv[]) {
7071
7172 char * env_argument = NULL ;
7273 char * env_entrypoint = NULL ;
74+ char * env_logname = NULL ;
7375 char entrypoint [ENTRYPOINT_MAXLEN ];
7476 int ret = 0 ;
7577 FILE * fd ;
7678
7779 /* AND: Several filepaths are hardcoded here, these must be made
7880 configurable */
7981 /* AND: P4A uses env vars...not sure what's best */
80- LOG ("Initialize Python for Android" );
82+ LOGP ("Initialize Python for Android" );
8183 env_argument = getenv ("ANDROID_ARGUMENT" );
8284 setenv ("ANDROID_APP_PATH" , env_argument , 1 );
8385 env_entrypoint = getenv ("ANDROID_ENTRYPOINT" );
86+ env_logname = getenv ("PYTHON_NAME" );
87+
88+ if (env_logname == NULL ) {
89+ env_logname = "python" ;
90+ setenv ("PYTHON_NAME" , "python" , 1 );
91+ }
8492
85- LOG ("Changing directory to the one provided by ANDROID_ARGUMENT" );
86- LOG (env_argument );
93+ LOGP ("Changing directory to the one provided by ANDROID_ARGUMENT" );
94+ LOGP (env_argument );
8795 chdir (env_argument );
8896
8997 Py_SetProgramName (L"android_python" );
@@ -94,31 +102,31 @@ int main(int argc, char *argv[]) {
94102 PyImport_AppendInittab ("androidembed" , initandroidembed );
95103#endif
96104
97- LOG ("Preparing to initialize python" );
105+ LOGP ("Preparing to initialize python" );
98106
99107 if (dir_exists ("crystax_python/" )) {
100- LOG ("crystax_python exists" );
108+ LOGP ("crystax_python exists" );
101109 char paths [256 ];
102110 snprintf (paths , 256 ,
103111 "%s/crystax_python/stdlib.zip:%s/crystax_python/modules" ,
104112 env_argument , env_argument );
105113 /* snprintf(paths, 256, "%s/stdlib.zip:%s/modules", env_argument,
106114 * env_argument); */
107- LOG ("calculated paths to be..." );
108- LOG (paths );
115+ LOGP ("calculated paths to be..." );
116+ LOGP (paths );
109117
110118#if PY_MAJOR_VERSION >= 3
111119 wchar_t * wchar_paths = Py_DecodeLocale (paths , NULL );
112120 Py_SetPath (wchar_paths );
113121#else
114122 char * wchar_paths = paths ;
115- LOG ("Can't Py_SetPath in python2, so crystax python2 doesn't work yet" );
123+ LOGP ("Can't Py_SetPath in python2, so crystax python2 doesn't work yet" );
116124 exit (1 );
117125#endif
118126
119- LOG ("set wchar paths..." );
127+ LOGP ("set wchar paths..." );
120128 } else {
121- LOG ("crystax_python does not exist" );
129+ LOGP ("crystax_python does not exist" );
122130 }
123131
124132 Py_Initialize ();
@@ -127,11 +135,11 @@ int main(int argc, char *argv[]) {
127135 PySys_SetArgv (argc , argv );
128136#endif
129137
130- LOG ("Initialized python" );
138+ LOGP ("Initialized python" );
131139
132140 /* ensure threads will work.
133141 */
134- LOG ("AND: Init threads" );
142+ LOGP ("AND: Init threads" );
135143 PyEval_InitThreads ();
136144
137145#if PY_MAJOR_VERSION < 3
@@ -147,7 +155,7 @@ int main(int argc, char *argv[]) {
147155 PyRun_SimpleString ("import sys, posix\n" );
148156 if (dir_exists ("lib" )) {
149157 /* If we built our own python, set up the paths correctly */
150- LOG ("Setting up python from ANDROID_PRIVATE" );
158+ LOGP ("Setting up python from ANDROID_PRIVATE" );
151159 PyRun_SimpleString ("private = posix.environ['ANDROID_PRIVATE']\n"
152160 "argument = posix.environ['ANDROID_ARGUMENT']\n"
153161 "sys.path[:] = [ \n"
@@ -194,31 +202,31 @@ int main(int argc, char *argv[]) {
194202 PyRun_SimpleString ("import site; print site.getsitepackages()\n" );
195203#endif
196204
197- LOG ("AND: Ran string" );
205+ LOGP ("AND: Ran string" );
198206
199207 /* run it !
200208 */
201- LOG ("Run user program, change dir and execute entrypoint" );
209+ LOGP ("Run user program, change dir and execute entrypoint" );
202210
203211 /* Get the entrypoint, search the .pyo then .py
204212 */
205213 char * dot = strrchr (env_entrypoint , '.' );
206214 if (dot <= 0 ) {
207- LOG ("Invalid entrypoint, abort." );
215+ LOGP ("Invalid entrypoint, abort." );
208216 return -1 ;
209217 }
210218 if (strlen (env_entrypoint ) > ENTRYPOINT_MAXLEN - 2 ) {
211- LOG ("Entrypoint path is too long, try increasing ENTRYPOINT_MAXLEN." );
219+ LOGP ("Entrypoint path is too long, try increasing ENTRYPOINT_MAXLEN." );
212220 return -1 ;
213221 }
214222 if (!strcmp (dot , ".pyo" )) {
215223 if (!file_exists (env_entrypoint )) {
216224 /* fallback on .py */
217225 strcpy (entrypoint , env_entrypoint );
218226 entrypoint [strlen (env_entrypoint ) - 1 ] = '\0' ;
219- LOG (entrypoint );
227+ LOGP (entrypoint );
220228 if (!file_exists (entrypoint )) {
221- LOG ("Entrypoint not found (.pyo, fallback on .py), abort" );
229+ LOGP ("Entrypoint not found (.pyo, fallback on .py), abort" );
222230 return -1 ;
223231 }
224232 } else {
@@ -232,21 +240,21 @@ int main(int argc, char *argv[]) {
232240 if (!file_exists (entrypoint )) {
233241 /* fallback on pure python version */
234242 if (!file_exists (env_entrypoint )) {
235- LOG ("Entrypoint not found (.py), abort." );
243+ LOGP ("Entrypoint not found (.py), abort." );
236244 return -1 ;
237245 }
238246 strcpy (entrypoint , env_entrypoint );
239247 }
240248 } else {
241- LOG ("Entrypoint have an invalid extension (must be .py or .pyo), abort." );
249+ LOGP ("Entrypoint have an invalid extension (must be .py or .pyo), abort." );
242250 return -1 ;
243251 }
244- // LOG ("Entrypoint is:");
245- // LOG (entrypoint);
252+ // LOGP ("Entrypoint is:");
253+ // LOGP (entrypoint);
246254 fd = fopen (entrypoint , "r" );
247255 if (fd == NULL ) {
248- LOG ("Open the entrypoint failed" );
249- LOG (entrypoint );
256+ LOGP ("Open the entrypoint failed" );
257+ LOGP (entrypoint );
250258 return -1 ;
251259 }
252260
@@ -268,21 +276,24 @@ int main(int argc, char *argv[]) {
268276 Py_Finalize ();
269277 fclose (fd );
270278
271- LOG ("Python for android ended." );
279+ LOGP ("Python for android ended." );
272280 return ret ;
273281}
274282
275283JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart (
276284 JNIEnv * env , jobject thiz , jstring j_android_private ,
277285 jstring j_android_argument , jstring j_service_entrypoint ,
278- jstring j_python_home , jstring j_python_path , jstring j_arg ) {
286+ jstring j_python_name , jstring j_python_home , jstring j_python_path ,
287+ jstring j_arg ) {
279288 jboolean iscopy ;
280289 const char * android_private =
281290 (* env )-> GetStringUTFChars (env , j_android_private , & iscopy );
282291 const char * android_argument =
283292 (* env )-> GetStringUTFChars (env , j_android_argument , & iscopy );
284293 const char * service_entrypoint =
285294 (* env )-> GetStringUTFChars (env , j_service_entrypoint , & iscopy );
295+ const char * python_name =
296+ (* env )-> GetStringUTFChars (env , j_python_name , & iscopy );
286297 const char * python_home =
287298 (* env )-> GetStringUTFChars (env , j_python_home , & iscopy );
288299 const char * python_path =
@@ -293,6 +304,7 @@ JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart(
293304 setenv ("ANDROID_ARGUMENT" , android_argument , 1 );
294305 setenv ("ANDROID_ENTRYPOINT" , service_entrypoint , 1 );
295306 setenv ("PYTHONOPTIMIZE" , "2" , 1 );
307+ setenv ("PYTHON_NAME" , python_name , 1 );
296308 setenv ("PYTHONHOME" , python_home , 1 );
297309 setenv ("PYTHONPATH" , python_path , 1 );
298310 setenv ("PYTHON_SERVICE_ARGUMENT" , arg , 1 );
0 commit comments