Commit 47f8b85c authored by rswindell's avatar rswindell
Browse files

Added log level support to global log() function (optional first argument,

default: LOG_INFO).
JSexec log mask can be configured with -L command-line option (default: 0xff).
services lprintf() now supports log level.
parent 3fd20968
......@@ -45,6 +45,8 @@
#include "sbbs.h"
#define DEFAULT_LOG_MASK 0xff /* Display all LOG levels */
JSRuntime* js_runtime;
JSContext* js_cx;
JSObject* js_glob;
......@@ -63,6 +65,7 @@ BOOL pause_on_exit=FALSE;
BOOL pause_on_error=FALSE;
BOOL terminated=FALSE;
BOOL terminate_immediately=FALSE;
DWORD log_mask=DEFAULT_LOG_MASK;
void banner(FILE* fp)
{
......@@ -88,6 +91,7 @@ void usage(FILE* fp)
"\t-g <interval> set garbage collection interval (default=%u, 0=never)\n"
"\t-H use local host name (instead of SCFG value)\n"
"\t-h <hostname> use specified host name (instead of SCFG value)\n"
"\t-L <mask> set log mask (default=0x%x)\n"
"\t-t <filename> send console output to stdout and filename\n"
"\t-e send error messages to console instead of stderr\n"
"\t-n send status messages to %s instead of stdout\n"
......@@ -101,6 +105,7 @@ void usage(FILE* fp)
,JAVASCRIPT_BRANCH_LIMIT
,JAVASCRIPT_YIELD_INTERVAL
,JAVASCRIPT_GC_INTERVAL
,DEFAULT_LOG_MASK
,_PATH_DEVNULL
,_PATH_DEVNULL
);
......@@ -152,16 +157,22 @@ void bail(int code)
static JSBool
js_log(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
uintN i;
uintN i=0;
int32 level=LOG_INFO;
JSString* str;
for (i = 0; i < argc; i++) {
if((str=JS_ValueToString(cx, argv[i]))==NULL)
return(JS_FALSE);
fprintf(errfp,"%s",JS_GetStringBytes(str));
if(JSVAL_IS_NUMBER(argv[i]))
JS_ValueToInt32(cx,argv[i++],&level);
if(log_mask&(1<<level)) {
for(; i<argc; i++) {
if((str=JS_ValueToString(cx, argv[i]))==NULL)
return(JS_FALSE);
fprintf(errfp,"%s",JS_GetStringBytes(str));
}
if(argc)
fprintf(errfp,"\n");
}
if(argc)
fprintf(errfp,"\n");
*rval = JSVAL_VOID;
return(JS_TRUE);
......@@ -679,6 +690,9 @@ int main(int argc, char **argv, char** environ)
case 'H':
gethostname(host_name=host_name_buf,sizeof(host_name_buf));
break;
case 'L':
log_mask=strtol(argv[++argn],NULL,0);
break;
case 'e':
errfp=confp;
break;
......
......@@ -431,22 +431,26 @@ DLLCALL js_DefineConstIntegers(JSContext* cx, JSObject* obj, jsConstIntSpec* int
static JSBool
js_log(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
uintN i;
uintN i=0;
int32 level=LOG_INFO;
JSString* str=NULL;
sbbs_t* sbbs;
if((sbbs=(sbbs_t*)JS_GetContextPrivate(cx))==NULL)
return(JS_FALSE);
for (i = 0; i < argc; i++) {
if(JSVAL_IS_NUMBER(argv[i]))
JS_ValueToInt32(cx,argv[i++],&level);
for(; i<argc; i++) {
if((str=JS_ValueToString(cx, argv[i]))==NULL)
return(JS_FALSE);
if(sbbs->online==ON_LOCAL) {
if(startup!=NULL && startup->event_lputs!=NULL)
startup->event_lputs(LOG_INFO,JS_GetStringBytes(str));
startup->event_lputs(level,JS_GetStringBytes(str));
} else
lputs(JS_GetStringBytes(str));
}
}
if(str==NULL)
*rval = JSVAL_VOID;
......@@ -670,9 +674,10 @@ js_prompt(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
}
static jsMethodSpec js_global_functions[] = {
{"log", js_log, 1, JSTYPE_STRING, JSDOCSTR("value [,value]")
{"log", js_log, 1, JSTYPE_STRING, JSDOCSTR("[level,] value [,value]")
,JSDOCSTR("add a line of text to the server and/or system log, "
"<i>values</i> are typically string constants or variables")
"<i>values</i> are typically string constants or variables, "
"<i>level</i> is the debug level/priority (default: <tt>LOG_INFO</tt>)")
},
{"read", js_read, 0, JSTYPE_STRING, JSDOCSTR("[count]")
,JSDOCSTR("read up to count characters from input stream")
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment