diff --git a/kivy_ios/recipes/ios/src/ios.pyx b/kivy_ios/recipes/ios/src/ios.pyx index 7556e388..eb64ed3f 100644 --- a/kivy_ios/recipes/ios/src/ios.pyx +++ b/kivy_ios/recipes/ios/src/ios.pyx @@ -22,6 +22,7 @@ cdef extern from "ios_wrapper.h": void load_url_webview(char *url, int x, int y, int width, int height) float ios_uiscreen_get_scale() int ios_uiscreen_get_dpi() + void ios_nslog(char *str) padding ios_get_safe_area() cdef void _send_email_done(char *status, void *data): @@ -217,6 +218,22 @@ def get_safe_area(): ''' return ios_get_safe_area() +def nslog(message): + '''Log a message to the console using NSLog + + :Parameters: + `message`: str + The message to log + ''' + cdef char *j_message = NULL + + if message is not None: + if type(message) is unicode: + message = message.encode('UTF-8') + j_message = message + + ios_nslog(j_message) + from pyobjus import autoclass, selector, protocol from pyobjus.protocols import protocols diff --git a/kivy_ios/recipes/ios/src/ios_utils.m b/kivy_ios/recipes/ios/src/ios_utils.m index 1e3589f7..e333f21e 100644 --- a/kivy_ios/recipes/ios/src/ios_utils.m +++ b/kivy_ios/recipes/ios/src/ios_utils.m @@ -171,3 +171,7 @@ padding ios_get_safe_area() { } return safearea; } + +void ios_nslog(char *str) { + NSLog(@"%s", str); +} diff --git a/kivy_ios/recipes/ios/src/ios_wrapper.h b/kivy_ios/recipes/ios/src/ios_wrapper.h index 5afded7c..3184a00e 100644 --- a/kivy_ios/recipes/ios/src/ios_wrapper.h +++ b/kivy_ios/recipes/ios/src/ios_wrapper.h @@ -19,4 +19,6 @@ typedef void (*ios_send_email_cb)(char *, void *); int ios_send_email(char *subject, char *text, char *mimetype, char *filename, char *filename_alias, ios_send_email_cb callback, void *userdata); +void ios_nslog(char *str); + #endif diff --git a/kivy_ios/tools/templates/{{ cookiecutter.project_name }}-ios/main.m b/kivy_ios/tools/templates/{{ cookiecutter.project_name }}-ios/main.m index 40b5ae5f..252b30a2 100644 --- a/kivy_ios/tools/templates/{{ cookiecutter.project_name }}-ios/main.m +++ b/kivy_ios/tools/templates/{{ cookiecutter.project_name }}-ios/main.m @@ -45,6 +45,10 @@ int main(int argc, char *argv[]) { putenv("KIVY_NO_CONSOLELOG=1"); #endif + // In production, if you want to redirect stdout/stderr to NSLog, uncomment + // the following line. This is useful to debug python errors in production. + //putenv("KIVY_NSLOG=1"); + // Export orientation preferences for Kivy export_orientation(); @@ -156,6 +160,23 @@ void load_custom_builtin_importer() { " def flush(self, *args, **kw): pass\n" \ " sys.stdout = fakestd()\n" \ " sys.stderr = fakestd()\n" \ + "else if environ.get('KIVY_NSLOG', '1') == '1':\n" \ + " from ios import nslog\n" \ + " class NslogRedirect(object):\n" \ + " def __init__(self):\n" \ + " self.buffer = ""\n" \ + " def write(self, chunk):\n" \ + " self.buffer += chunk\n" \ + " if '\n' in self.buffer:\n" \ + " lines = self.buffer.split('\n')\n" \ + " for line in lines[:-1]:\n" \ + " nslog(line)\n" \ + " self.buffer = lines[-1]\n" \ + " def flush(self):\n" \ + " nslog(self.buffer)\n" \ + " self.buffer = ""\n" \ + " sys.stdout = NslogRedirect()\n" \ + " sys.stderr = NslogRedirect()\n" \ "# Custom builtin importer for precompiled modules\n" \ "class CustomBuiltinImporter(object):\n" \ " def find_module(self, fullname, mpath=None):\n" \