Skip to content

Commit

Permalink
allow console as log filename for emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlf committed Apr 17, 2024
1 parent 8a5b4d2 commit cef9cec
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
23 changes: 19 additions & 4 deletions share/emscripten/gpac.html
Expand Up @@ -1032,12 +1032,24 @@
var ansiCodes = []
// Replace with markup.
var ret = text.replace(/\033\[(\d+)m/g, function (match, seq) {
if (seq=='0') {
r = '';
ansiCodes.forEach( c => {
if (c == '3') r+='</i>';
else if (c == '4') r+='</b>';
});
ansiCodes.length=0;
return r+'</span>';
}
var ot = _openTags[seq]
if (ot) {
// If current sequence has been opened, close it.
if (!!~ansiCodes.indexOf(seq)) { // eslint-disable-line no-extra-boolean-cast
ansiCodes.pop()
return '</span>'
var r='';
var c = ansiCodes.pop()
if (c == '3') r+='</i>';
else if (c == '4') r+='</b>';
return r+'</span>'
}
// Open tag.
ansiCodes.push(seq)
Expand Down Expand Up @@ -1918,6 +1930,7 @@
{
let gpac_has_threads = LIBGPAC.cwrap('gpac_has_threads', 'number', []);
_lib.has_threads = gpac_has_threads();
_lib.in_logs = false;
//error codes
_lib.SCRIPT_INFO = 3;
_lib.PENDING_PACKET = 2;
Expand Down Expand Up @@ -2115,8 +2128,10 @@
};
})(),
printErr: function(text) {
//console.log(text);
LIBGPAC.print(text);
if (LIBGPAC.in_logs)
console.log(text);
else
LIBGPAC.print(text);
},

canvas: (function() {
Expand Down
4 changes: 4 additions & 0 deletions src/filter_core/filter_session.c
Expand Up @@ -1602,7 +1602,11 @@ Bool em_check_yield_th_proc(u32 thid, GF_SessionThread *sess_thread, u64 enter_t
}
#endif

#ifndef GPAC_DISABLE_THREADS
const char *gf_th_log_name(GF_Thread *t);
#else
#define gf_th_log_name(_t) "Main Process"
#endif

static u32 gf_fs_thread_proc(GF_SessionThread *sess_thread)
{
Expand Down
1 change: 0 additions & 1 deletion src/utils/downloader.c
Expand Up @@ -7715,7 +7715,6 @@ EM_JS(int, dm_fetch_init, (int sess, int _url, int _method, int _headers, int nb
libgpac._fetcher_set_header(fetcher.sess, 0, 0);
} else {
libgpac._fetcher_set_reply(fetcher.sess, response.status, null);
do_log_err('fetcher for ' + url + ' failed ' + response.status);
fetcher._state = 3;
}
})
Expand Down
17 changes: 17 additions & 0 deletions src/utils/error.c
Expand Up @@ -833,6 +833,9 @@ void default_log_callback_color(void *cbck, GF_LOG_Level level, GF_LOG_Tool tool
static void *user_log_cbk = NULL;
gf_log_cbk log_cbk = default_log_callback_color;
static Bool log_exit_on_error = GF_FALSE;
#ifdef GPAC_CONFIG_EMSCRIPTEN
Bool gpac_log_console = GF_FALSE;
#endif

GF_EXPORT
Bool gf_log_use_color()
Expand All @@ -843,12 +846,26 @@ Bool gf_log_use_color()
GF_EXPORT
void gf_log(const char *fmt, ...)
{
#ifdef GPAC_CONFIG_EMSCRIPTEN
if (gpac_log_console && (call_tool!=GF_LOG_APP)) {
EM_ASM({
libgpac.in_logs = true;
});
}
#endif
va_list vl;
va_start(vl, fmt);
gf_mx_p(logs_mx);
log_cbk(user_log_cbk, call_lev, call_tool, fmt, vl);
gf_mx_v(logs_mx);
va_end(vl);
#ifdef GPAC_CONFIG_EMSCRIPTEN
if (gpac_log_console && (call_tool!=GF_LOG_APP)) {
EM_ASM({
libgpac.in_logs = false;
});
}
#endif
if (log_exit_on_error && (call_lev==GF_LOG_ERROR) && (call_tool != GF_LOG_MEMORY)) {
exit(1);
}
Expand Down
6 changes: 5 additions & 1 deletion src/utils/os_config_init.c
Expand Up @@ -1386,7 +1386,11 @@ GF_GPACArg GPAC_Args[] = {
GF_DEF_ARG("tmp", NULL, "specify directory for temporary file creation instead of OS-default temporary file management", NULL, NULL, GF_ARG_STRING, 0),
GF_DEF_ARG("noprog", NULL, "disable progress messages", NULL, NULL, GF_ARG_BOOL, GF_ARG_HINT_ADVANCED|GF_ARG_SUBSYS_LOG),
GF_DEF_ARG("quiet", NULL, "disable all messages, including errors", NULL, NULL, GF_ARG_BOOL, GF_ARG_HINT_ADVANCED|GF_ARG_SUBSYS_LOG),
GF_DEF_ARG("log-file", "lf", "set output log file", NULL, NULL, GF_ARG_STRING, GF_ARG_SUBSYS_LOG),
GF_DEF_ARG("log-file", "lf", "set output log file"
#ifdef GPAC_CONFIG_EMSCRIPTEN
" - use `console` for browser console log"
#endif
, NULL, NULL, GF_ARG_STRING, GF_ARG_SUBSYS_LOG),
GF_DEF_ARG("log-clock", "lc", "log time in micro sec since start time of GPAC before each log line except for `app` tool", NULL, NULL, GF_ARG_BOOL, GF_ARG_SUBSYS_LOG),
GF_DEF_ARG("log-utc", "lu", "log UTC time in ms before each log line except for `app` tool", NULL, NULL, GF_ARG_BOOL, GF_ARG_SUBSYS_LOG),
GF_DEF_ARG("logs", NULL, "set log tools and levels. \n"
Expand Down
15 changes: 12 additions & 3 deletions src/utils/os_divers.c
Expand Up @@ -900,6 +900,9 @@ Bool gf_sys_is_cov_mode()
const char *gpac_log_file_name=NULL;
#ifndef GPAC_DISABLE_LOG
extern Bool gpac_log_dual;
#ifdef GPAC_CONFIG_EMSCRIPTEN
extern Bool gpac_log_console;
#endif
#endif

GF_EXPORT
Expand All @@ -908,6 +911,14 @@ void gf_log_reset_file()
#ifndef GPAC_DISABLE_LOG
if (gpac_log_file_name) {
if (gpac_log_file) gf_fclose(gpac_log_file);
#ifdef GPAC_CONFIG_EMSCRIPTEN
gpac_log_console = GF_FALSE;
if (gpac_log_file_name && !strcmp(gpac_log_file_name, "console")) {
gpac_log_file = NULL;
gpac_log_console = GF_TRUE;
return;
}
#endif
gpac_log_file = gf_fopen(gpac_log_file_name, "wt");
}
#endif
Expand Down Expand Up @@ -1051,9 +1062,7 @@ GF_Err gf_sys_set_args(s32 argc, const char **argv)


#ifndef GPAC_DISABLE_LOG
if (gpac_log_file_name) {
gpac_log_file = gf_fopen(gpac_log_file_name, "wt");
}
gf_log_reset_file();
#endif
if (gf_opts_get_bool("core", "rmt"))
gf_sys_enable_remotery(GF_TRUE, GF_FALSE);
Expand Down

0 comments on commit cef9cec

Please sign in to comment.