Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add redirection to NSLog, if KIVY_NSLOG=1 is set #791

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions kivy_ios/recipes/ios/src/ios.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = <bytes>message

ios_nslog(j_message)


from pyobjus import autoclass, selector, protocol
from pyobjus.protocols import protocols
Expand Down
4 changes: 4 additions & 0 deletions kivy_ios/recipes/ios/src/ios_utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,7 @@ padding ios_get_safe_area() {
}
return safearea;
}

void ios_nslog(char *str) {
NSLog(@"%s", str);
}
2 changes: 2 additions & 0 deletions kivy_ios/recipes/ios/src/ios_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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" \
Expand Down