Exemple #1
0
        public void print_exception(LogLevel logLevel = LogLevel.Error, string title = "")
        {
            var ex             = JSApi.JS_GetException(this);
            var err_fileName   = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_fileName);
            var err_lineNumber = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_lineNumber);
            var err_message    = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_message);
            var err_stack      = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_stack);

            var fileName   = err_fileName.IsNullish() ? "native" : GetString(err_fileName);
            var lineNumber = err_lineNumber.IsNullish() ? null : GetString(err_lineNumber);
            var message    = GetString(err_message);
            var stack      = GetString(err_stack);

            var logger = ScriptEngine.GetLogger(this);

            if (string.IsNullOrEmpty(lineNumber))
            {
                logger.ScriptWrite(logLevel, "[{0}] {1} {2}\n{3}",
                                   fileName, title, message, stack);
            }
            else
            {
                logger.ScriptWrite(logLevel, "[{0}:{1}] {2} {3}\n{4}",
                                   fileName, lineNumber, title, message, stack);
            }

            JSApi.JS_FreeValue(this, err_fileName);
            JSApi.JS_FreeValue(this, err_lineNumber);
            JSApi.JS_FreeValue(this, err_message);
            JSApi.JS_FreeValue(this, err_stack);
            JSApi.JS_FreeValue(this, ex);
        }
Exemple #2
0
        // 在外部已确定存在异常
        public string GetExceptionString()
        {
            var ex             = JSApi.JS_GetException(this);
            var err_fileName   = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_fileName);
            var err_lineNumber = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_lineNumber);
            var err_message    = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_message);
            var err_stack      = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_stack);

            try
            {
                var fileName        = JSApi.GetString(this, err_fileName);
                var lineNumber      = JSApi.GetString(this, err_lineNumber);
                var message         = JSApi.GetString(this, err_message);
                var stack           = JSApi.GetString(this, err_stack);
                var exceptionString = string.Format("[JS] {0}:{1} {2}\n{3}", fileName, lineNumber, message, stack);

                return(exceptionString);
            }
            finally
            {
                JSApi.JS_FreeValue(this, err_fileName);
                JSApi.JS_FreeValue(this, err_lineNumber);
                JSApi.JS_FreeValue(this, err_message);
                JSApi.JS_FreeValue(this, err_stack);
                JSApi.JS_FreeValue(this, ex);
            }
        }
Exemple #3
0
        public void print_exception(IScriptLogger logger, LogLevel logLevel, string title)
        {
            var ex = JSApi.JS_GetException(this);

            if (logger != null)
            {
                var err_fileName   = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_fileName);
                var err_lineNumber = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_lineNumber);
                var err_message    = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_message);
                var err_stack      = JSApi.JS_GetProperty(this, ex, JSApi.JS_ATOM_stack);

                var fileName   = err_fileName.IsNullish() ? "native" : GetString(err_fileName);
                var lineNumber = err_lineNumber.IsNullish() ? null : GetString(err_lineNumber);
                var message    = GetString(err_message);
                var stack      = GetString(err_stack);

                if (string.IsNullOrEmpty(lineNumber))
                {
                    if (string.IsNullOrEmpty(stack))
                    {
                        logger.Write(logLevel, "[{0}] {1} {2}",
                                     fileName, title, message);
                    }
                    else
                    {
                        logger.Write(logLevel, "[{0}] {1} {2}\nJavascript stack:\n{3}",
                                     fileName, title, message, stack);
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(stack))
                    {
                        logger.Write(logLevel, "[{0}:{1}] {2} {3}",
                                     fileName, lineNumber, title, message);
                    }
                    else
                    {
                        logger.Write(logLevel, "[{0}:{1}] {2} {3}\nJavascript stack:\n{4}",
                                     fileName, lineNumber, title, message, stack);
                    }
                }

                JSApi.JS_FreeValue(this, err_fileName);
                JSApi.JS_FreeValue(this, err_lineNumber);
                JSApi.JS_FreeValue(this, err_message);
                JSApi.JS_FreeValue(this, err_stack);
            }

            JSApi.JS_FreeValue(this, ex);
        }