commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1085412 - in /commons/sandbox/runtime/trunk/src/main/native: Makefile.unx.in include/acr/debug.h include/acr/error.h include/acr/string.h shared/clazz.c shared/debug.c shared/error.c shared/memory.c shared/string.c
Date Fri, 25 Mar 2011 14:46:46 GMT
Author: mturk
Date: Fri Mar 25 14:46:46 2011
New Revision: 1085412

URL: http://svn.apache.org/viewvc?rev=1085412&view=rev
Log:
Add debug and simplify old exception throwing code

Added:
    commons/sandbox/runtime/trunk/src/main/native/include/acr/debug.h   (with props)
    commons/sandbox/runtime/trunk/src/main/native/shared/debug.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h
    commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
    commons/sandbox/runtime/trunk/src/main/native/shared/error.c
    commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
    commons/sandbox/runtime/trunk/src/main/native/shared/string.c

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1085412&r1=1085411&r2=1085412&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Fri Mar 25 14:46:46 2011
@@ -87,8 +87,9 @@ LIBSOURCES=\
 	$(TOPDIR)/port/bsdrand.c \
 	$(TOPDIR)/port/bsdstring.c \
 	$(TOPDIR)/port/bsdsys.c \
-	$(TOPDIR)/shared/clazz.c \
 	$(TOPDIR)/shared/bzip2.c \
+	$(TOPDIR)/shared/clazz.c \
+	$(TOPDIR)/shared/debug.c \
 	$(TOPDIR)/shared/error.c \
 	$(TOPDIR)/shared/memory.c \
 	$(TOPDIR)/shared/native.c \

Added: commons/sandbox/runtime/trunk/src/main/native/include/acr/debug.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/debug.h?rev=1085412&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/debug.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/debug.h Fri Mar 25 14:46:46
2011
@@ -0,0 +1,46 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACR_DEBUG_H_
+#define _ACR_DEBUG_H_
+
+#include "acr/error.h"
+#if defined(DEBUG) || defined(_DEBUG)
+# include <assert.h>
+#endif
+
+#if defined(_DEBUG) || defined(DEBUG)
+void
+AcrDebugAssert(const char *, const char*, int, const char *);
+# define ACR_DEBUG_ASSERT(C)        \
+    if (!(C)) AcrDebugAssert(__FILE_FUNC_LINE__, ACR_STRINGIFY(C))
+# define _PR_DEBUG_ASSERT_TRUE(C)   \
+    if ((C))  AcrDebugAssert(__FILE_FUNC_LINE__, ACR_STRINGIFY(C))
+#else
+# define ACR_DEBUG_ASSERT(C)        (void)0
+# define ACR_DEBUG_ASSERT_TRUE(C)   (void)0
+#endif
+
+
+#if defined(_DEBUG) || defined(DEBUG)
+void
+AcrDebugPrintf(const char *, const char*, int, const char *, ...) PRINTF_LIKE(4, 5);
+# define ACR_DEBUG_TRACE(Fmt, ...)  AcrDebugPrintf(__FILE_FUNC_LINE__, Fmt, ##__VA_ARGS__)
+#else
+# define ACR_DEBUG_TRACE(Fmt, ...)  (void)0
+#endif
+
+#endif /* _ACR_DEBUG_H_ */

Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr/debug.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h?rev=1085412&r1=1085411&r2=1085412&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h Fri Mar 25 14:46:46
2011
@@ -1598,23 +1598,32 @@ ACR_INLINE(DWORD) AcrNetOsError()
 
 #endif /* !defined(_WINDOWS) */
 
+#if defined(DEBUG) || defined(_DEBUG)
+#define ACR_THROW_EX(CL, ER)    AcrDebugThrowException(_E, __FILE_FUNC_LINE__, (CL), (ER))
+#define ACR_THROW_ERROR(CL)     AcrDebugThrowException(_E, __FILE_FUNC_LINE__, (CL), ACR_GET_OS_ERROR())
+#else
+#define ACR_THROW_EX(CL, ER)    AcrThrowException(_E, (CL), (ER))
+#define ACR_THROW_ERROR(CL)     AcrThrowException(_E, (CL), ACR_GET_OS_ERROR())
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 void
-AcrFatalError(const char *filename, const char *funcname, int lineno,
+AcrFatalError(JNI_STDENV,
+              const char *filename, const char *funcname, int lineno,
               int error, const char *msg, ...);
 
 void
-AcrThrow(JNI_STDENV,
-         const char *filename, const char *funcname, int lineno,
-         int clazz, int error, const char *msg, ...);
+AcrThrow(JNI_STDENV, const char *clazz, const char *msg);
+
+void
+AcrThrowException(JNI_STDENV, int clazz, int error);
 
 void
-AcrThrowException(JNI_STDENV,
-                  const char *filename, const char *funcname, int lineno,
-                  int clazz, int error);
+AcrDebugThrowException(JNI_STDENV, const char *filename, const char *funcname,
+                       int lineno, int clazz, int error);
 
 #ifdef __cplusplus
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h?rev=1085412&r1=1085411&r2=1085412&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/string.h Fri Mar 25 14:46:46
2011
@@ -35,6 +35,9 @@ extern "C" {
  *
  */
 
+ACR_CLASS_CTOR(String);
+ACR_CLASS_DTOR(String);
+
 int
 AcrGetNativeCodePage(const char *cs);
 

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=1085412&r1=1085411&r2=1085412&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Fri Mar 25 14:46:46 2011
@@ -202,6 +202,7 @@ AcrInitCoreClasses(JNI_STDENV)
     /* Init some core Objects and it's methods */
     ACR_CLASS_LOAD(Object);
     ACR_CLASS_LOAD(Class);
+    ACR_CLASS_LOAD(String);
     ACR_CLASS_LOPT(Unsafe);
     return 0;
 }

Added: commons/sandbox/runtime/trunk/src/main/native/shared/debug.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/debug.c?rev=1085412&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/debug.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/debug.c Fri Mar 25 14:46:46 2011
@@ -0,0 +1,77 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/debug.h"
+#include "acr/port.h"
+
+#if defined(_DEBUG) || defined(DEBUG)
+void
+AcrDebugPrintf(const char *func, const char *file,
+              int line, const char *fmt, ...)
+{
+
+    if (fmt != 0 && *fmt == '\n') {
+        while (*fmt && *fmt == '\n')
+            fputc(*(fmt++), stdout);
+    }
+    fputs("[debug] ", stdout);
+    if (file != 0) {
+        time_t     t;
+        struct tm *l;
+        char   buf[64];
+
+        t = time(0);
+        l = localtime(&t);
+        strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", l);
+        fprintf(stdout, "[%-5u %s] %s: line %4d: %s",
+                (acr_uint32_t)getpid(), buf,
+                basename_c(file), line, func);
+    }
+    if (fmt != 0) {
+        va_list ap;
+        if (file != 0)
+            fputs(": ", stdout);
+        va_start(ap, fmt);
+        vfprintf(stdout, fmt, ap);
+        va_end(ap);
+    }
+    fputs(_PR_CRLF, stdout);
+    fflush(stdout);
+}
+
+void
+AcrDebugAssert(const char *file, const char *func, int line, const char *msg)
+{
+    if (*msg == '\n') {
+        while (*msg && *msg == '\n')
+            fputc(*(msg++), stdout);
+    }
+    fprintf(stderr,
+            "Assertion `%s' failed, file %s, line #%d, func %s" _PR_CRLF,
+            msg, basename_c(file), line, func);
+    fflush(stderr);
+    /* Abort the application */
+#if defined(_WINDOWS)
+    *((int *)0) = 0;
+#else
+    raise(SIGABRT);
+#endif
+    exit(3);
+}
+
+#else
+const char __unused_file_debug_c[] = __FILE__;
+#endif

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/debug.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=1085412&r1=1085411&r2=1085412&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Fri Mar 25 14:46:46 2011
@@ -15,6 +15,8 @@
  */
 
 #include "acr/error.h"
+#include "acr/debug.h"
+#include "acr/jniapi.h"
 #include "acr/port.h"
 
 static const char *_throw_classes[] = {
@@ -497,15 +499,22 @@ _cr_strerror_r(int statcode, char *buf, 
 }
 
 void
-AcrFatalError(const char *file, const char *func, int line,
+AcrFatalError(JNI_STDENV,
+              const char *file, const char *func, int line,
               int error, const char *msg, ...)
 {
+    char buf[ACR_MBUFF_SIZ] = "";
     if (msg) {
         va_list ap;
         va_start(ap, msg);
-        vfprintf(stderr, msg, ap);
+        vsnprintf(buf, sizeof(buf), msg, ap);
         va_end(ap);
     }
+    if (IS_VALID_HANDLE(_E)) {
+        (*_E)->FatalError(_E, buf);
+        return;
+    }
+    fputs(buf, stderr);
     if (file) {
         if (msg)
             fputs(", ", stderr);
@@ -527,10 +536,94 @@ AcrFatalError(const char *file, const ch
 }
 
 void
-AcrThrowException(JNI_STDENV,
-                  const char *file, const char *func, int line,
-                  int cls, int err)
+AcrThrow(JNI_STDENV, const char *clazz, const char *msg)
+{
+    jclass ec;
+
+    if (_E == 0)
+        _E = AcrGetJNIEnv();
+    if (IS_INVALID_HANDLE(_E)) {
+        ACR_DEBUG_TRACE("JNI Environment is invalid or unavailable (%d).", ACR_GET_OS_ERROR());
+        ACR_DEBUG_TRACE("Throwing class '%s'", clazz);
+        if (msg) {
+            ACR_DEBUG_TRACE("%s", msg);
+        }
+        return;
+    }
+    if ((*_E)->ExceptionCheck(_E)) {
+        /* We already have a pending exception. */
+        ACR_DEBUG_TRACE("Exception is already in the queue.", 0);
+        ACR_DEBUG_TRACE("Throwing class '%s'", clazz);
+        if (msg) {
+            ACR_DEBUG_TRACE("%s", msg);
+        }
+        return;
+    }
+    ec = (*_E)->FindClass(_E, clazz);
+    if (ec == 0) {
+        /* If the class cannot be found,
+         * the Exception has already been thrown.
+         * See JNI Find Class for the Exceptions thrown.
+         */
+        ACR_DEBUG_TRACE("Cannot find class '%s'", clazz);
+        if (msg) {
+            ACR_DEBUG_TRACE("%s", msg);
+        }
+        return;
+    }
+    (*_E)->ThrowNew(_E, ec, msg);
+    (*_E)->DeleteLocalRef(_E, ec);
+}
+
+void
+AcrDebugThrowException(JNI_STDENV,
+                       const char *file, const char *func, int line,
+                       int cls, int err)
+{
+    const char *clazz;
+    const char *msg = 0;
+    char *ptr;
+    char buffer[ACR_MBUFF_SIZ * 2] = "";
+
+    if (cls > 0 && cls < ACR_EX_LEN)
+        clazz = _throw_classes[cls];
+    else
+        clazz = _throw_classes[0];
+    ptr = buffer;
+    if (err == 0 || err == ACR_EGENERAL) {
+        msg = 0;
+    }
+    else {
+        msg = buffer;
+        if (file != 0) {
+            time_t     t;
+            struct tm *l;
+            char   buf[64];
+
+            t = time(0);
+            l = localtime(&t);
+            strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", l);
+            snprintf(buffer, ACR_MBUFF_SIZ,
+                    "[%-5u %s] %s: line %4d: %s ",
+                    (acr_uint32_t)getpid(), buf,
+                    basename_c(file), line, func);
+            buffer[ACR_MBUFF_SIZ] = '\0';
+            ptr = buffer + strlen(buffer);
+        }
+        _cr_strerror_r(err, ptr, ACR_MBUFF_SIZ);
+    }
+    AcrThrow(_E, clazz, msg);
+}
+
+void
+AcrThrowException(JNI_STDENV, int cls, int err)
 {
+    const char *clazz;
 
+    if (cls > 0 && cls < ACR_EX_LEN)
+        clazz = _throw_classes[cls];
+    else
+        clazz = _throw_classes[0];
+    AcrThrow(_E, clazz, 0);
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memory.c?rev=1085412&r1=1085411&r2=1085412&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Fri Mar 25 14:46:46 2011
@@ -42,9 +42,10 @@ void
 AcrMemoryAbort(JNI_STDENV, int retcode, size_t required, const char *filename,
                const char *funcname, int lineno)
 {
+    AcrDebugThrowException(_E, filename, funcname, lineno, ACR_EX_ENOMEM, retcode);
     if ((retcode = (*_memory_abort_fn)(retcode, required,
                                        filename, funcname, lineno))) {
-        AcrFatalError(filename, funcname, lineno, retcode,
+        AcrFatalError(_E, filename, funcname, lineno, retcode,
                       "Failed allocating %" ACR_SIZE_T_FMT " bytes", required);
     }
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=1085412&r1=1085411&r2=1085412&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Fri Mar 25 14:46:46 2011
@@ -786,7 +786,7 @@ static jstring new_string_utf_8(JNIEnv *
             if ((ex = conv_utf8_to_ucs2(s, sl, cc, &wl)) == ACR_SUCCESS)
                 rs = (*_E)->NewString(_E, cc, sl);
             else
-                AcrThrowException(_E, __FILE_FUNC_LINE__, ACR_EX_EINVAL, ex);
+                ACR_THROW_EX(ACR_EX_EINVAL, ex);
         }
         else {
             jchar  *cc;
@@ -795,7 +795,7 @@ static jstring new_string_utf_8(JNIEnv *
                 if ((ex = conv_utf8_to_ucs2(s, sl, cc, &wl)) == ACR_SUCCESS)
                     rs = (*_E)->NewString(_E, cc, sl);
                 else
-                    AcrThrowException(_E, __FILE_FUNC_LINE__, ACR_EX_EINVAL, ex);
+                    ACR_THROW_EX(ACR_EX_EINVAL, ex);
                 AcrFree(cc);
             }
         }



Mime
View raw message