diff options
author | portix <none@none> | 2012-05-30 15:46:14 +0200 |
---|---|---|
committer | portix <none@none> | 2012-05-30 15:46:14 +0200 |
commit | 49a4945074f6c8007bd79e7a52527f415ea11873 (patch) | |
tree | ac5615fec15464a967762e70c666a2ff5d42029c | |
parent | 5de803da7bde5953b33fb13bf8f5bf7f0e81fc32 (diff) | |
download | dwb-49a4945074f6c8007bd79e7a52527f415ea11873.zip |
New function io.debug; print debug messages in all scripts in XDG_CONFIG_HOME/dwb/userscripts
-rw-r--r-- | api/jsapi.txt | 19 | ||||
-rw-r--r-- | scripts/lib/io.js | 34 | ||||
-rw-r--r-- | src/dwb.c | 28 | ||||
-rw-r--r-- | src/scripts.c | 10 | ||||
-rw-r--r-- | src/scripts.h | 2 |
5 files changed, 75 insertions, 18 deletions
diff --git a/api/jsapi.txt b/api/jsapi.txt index d7840fcd..0d208e82 100644 --- a/api/jsapi.txt +++ b/api/jsapi.txt @@ -295,6 +295,25 @@ var bookmarks = io.read(data.bookmarks); The +io+ object implements functions for input and output. **** +[[debug]] +[float] +==== *debug()* ==== + +[source,javascript] +---- +void io.debug(String message | Error error, [Error error]) +---- + +Prints a debug message and the call stack to stderr. If the first parameter is +an error object the second parameter is ignored, else the error is optional. + + :: + +_message_;; The message to show +_error_;; A javascript error object, optional as second parameter +**** + +**** [[error]] [float] ==== *error()* ==== diff --git a/scripts/lib/io.js b/scripts/lib/io.js new file mode 100644 index 00000000..61b4b5e0 --- /dev/null +++ b/scripts/lib/io.js @@ -0,0 +1,34 @@ +Object.defineProperties(io, { + "debug" : { + value : function () { + var message = null; + if (arguments.length === 0) { + return; + } + else if (arguments.length === 1) { + if (arguments[0] instanceof Error) { + message = "\nSCRIPT DEBUG: \tException in line " + (arguments[0].line + 1) + ": " + arguments[0].message + "\n" + + "STACK: \t\t[" + arguments[0].stack.match(/[^\n]+/g).slice(0).join("] [")+"]\n"; + } + else { + try { + throw new Error(arguments[0]); + } + catch (e) { + message = "\nSCRIPT DEBUG: \t" + e.message + "\n" + + "STACK: \t\t[" + e.stack.match(/[^\n]+/g).slice(1).join("] [")+"]\n"; + } + } + } + else { + message = "\nSCRIPT DEBUG: \t" + arguments[0] + "\n"; + if (arguments[1] instanceof Error) { + message += "SCRIPT DEBUG: \tException in line " + (arguments[1].line + 1) + ": " + arguments[1].message + "\n"; + message += "STACK: \t\t[" + arguments[1].stack.match(/[^\n]+/g).slice(0).join("] [")+"]\n"; + } + } + io.print(message, "stderr"); + } + } +}); +Object.freeze(io); @@ -2623,12 +2623,14 @@ dwb_get_scripts() { continue; else if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) { realpath = g_file_read_link(path, &error); - if (error != NULL) { + if (realpath != NULL) { fprintf(stderr, "Cannot read %s : %s\n", path, error->message); - continue; + goto loop_end; + } + else { + g_free(path); + path = realpath; } - g_free(path); - path = realpath; } if ( (f = fopen(path, "r")) != NULL) { if (fgetc(f) == '#' && fgetc(f) == '!') { @@ -2645,27 +2647,24 @@ dwb_get_scripts() { if (!javascript && !g_file_test(path, G_FILE_TEST_IS_EXECUTABLE)) { fprintf(stderr, "Warning: userscript %s isn't executable and will be ignored.\n", path); - continue; + goto loop_end; } g_file_get_contents(path, &content, NULL, NULL); - if (content == NULL) - continue; + if (content == NULL) { + goto loop_end; + } if (javascript) { char *script = strchr(content, '\n'); if (script && *(script+1)) { - scripts_init_script(script+1); - g_free(content); - continue; + scripts_init_script(path, script+1); + goto loop_end; } } char **lines = g_strsplit(content, "\n", -1); - g_free(content); - content = NULL; - int i=0; n = NULL; KeyMap *map = dwb_malloc(sizeof(KeyMap)); @@ -2702,6 +2701,9 @@ dwb_get_scripts() { gl = g_list_prepend(gl, map); g_strfreev(lines); +loop_end: + FREE0(path); + FREE0(content); } g_dir_close(dir); } diff --git a/src/scripts.c b/src/scripts.c index edae55e5..be804abf 100644 --- a/src/scripts.c +++ b/src/scripts.c @@ -1427,7 +1427,7 @@ create_global_object() { { 0, 0, 0 }, }; class = create_class("io", io_functions, NULL); - create_object(_global_context, class, global_object, kJSDefaultAttributes, "io", NULL); + create_object(_global_context, class, global_object, kJSPropertyAttributeDontDelete, "io", NULL); JSClassRelease(class); JSStaticFunction system_functions[] = { @@ -1569,13 +1569,15 @@ scripts_remove_tab(JSObjectRef obj) { /* scripts_init_script {{{*/ void -scripts_init_script(const char *script) { +scripts_init_script(const char *path, const char *script) { if (_global_context == NULL) create_global_object(); - JSObjectRef function = js_make_function(_global_context, script); + char *debug = g_strdup_printf("try { %s } catch(e) { io.debug(\"In file %s\", e); }", script, path); + JSObjectRef function = js_make_function(_global_context, debug); if (function != NULL) { _script_list = g_slist_prepend(_script_list, function); } + g_free(debug); }/*}}}*/ void @@ -1617,7 +1619,7 @@ scripts_execute_scripts(char **scripts) { for (int i=0; scripts[i] != NULL; i++) { content = util_get_file_content(scripts[i]); if (content != NULL) { - scripts_init_script(content); + scripts_init_script(scripts[i], content); g_free(content); } } diff --git a/src/scripts.h b/src/scripts.h index b43102dc..4524c1d5 100644 --- a/src/scripts.h +++ b/src/scripts.h @@ -59,7 +59,7 @@ gboolean scripts_emit(ScriptSignal *); void scripts_create_tab(GList *gl); void scripts_remove_tab(JSObjectRef ); void scripts_end(void); -void scripts_init_script(const char *); +void scripts_init_script(const char *, const char *); void scripts_init(void); void scripts_execute_scripts(char **scripts); DwbStatus scripts_eval_key(KeyMap *m, Arg *arg); |