Date: Tuesday, December 5, 2006 @ 01:13:17
  Author: marc
    Path: /cvsroot/carob/carob

Modified: include/CarobException.hpp (1.50 -> 1.51) src/CarobException.cpp
          (1.20 -> 1.21) test/CarobProtector.hpp (1.16 -> 1.17)

Implemented pretty-printer operator<< for BackTrace, StackTraceElement and 
StackTrace
based on original test/CarobProtector.hpp code.


----------------------------+
 include/CarobException.hpp |   15 +++++++-
 src/CarobException.cpp     |   67 ++++++++++++++++++++++++++++++++----
 test/CarobProtector.hpp    |   79 ++++++++++++-------------------------------
 3 files changed, 96 insertions(+), 65 deletions(-)


Index: carob/include/CarobException.hpp
diff -u carob/include/CarobException.hpp:1.50 
carob/include/CarobException.hpp:1.51
--- carob/include/CarobException.hpp:1.50       Mon Dec  4 22:24:07 2006
+++ carob/include/CarobException.hpp    Tue Dec  5 01:13:16 2006
@@ -22,9 +22,16 @@
 #ifndef _CAROBEXCEPTION_H_
 #define _CAROBEXCEPTION_H_
 
+#include <ostream>
+
 #include <string>
 #include <vector>
 
+#ifdef __GLIBC__ // probably not the right macro
+#include <cxxabi.h>
+#define CAROB_CXX_DEMANGLING 1
+#endif
+
 namespace {
   const int            DEFAULTVENDORCODE(-1);
   const std::wstring   DEFAULTSQLSTATE(L"HY000");
@@ -43,9 +50,14 @@
   BackTrace();
 };
 
+std::wostream&
+operator<< (std::wostream& outstream, const BackTrace& bt);
 
 typedef std::vector<StackTraceElement> StackTrace;
 
+std::wostream&
+operator<< (std::wostream& outstream, const StackTrace& st);
+
 /**
  * Mother class of all exceptions send by the driver defining common exception 
  * interface.
@@ -361,7 +373,8 @@
    * Provides a string representation of the element.
    * @return all members formated in a single string
    */
-  std::wstring toWString() const;
+  friend std::wostream&
+  operator<< (std::wostream& oss, const StackTraceElement& ste);
 
 private:
   // we would like to have const members (and initializer lists)
Index: carob/src/CarobException.cpp
diff -u carob/src/CarobException.cpp:1.20 carob/src/CarobException.cpp:1.21
--- carob/src/CarobException.cpp:1.20   Tue Feb 28 19:50:41 2006
+++ carob/src/CarobException.cpp        Tue Dec  5 01:13:16 2006
@@ -59,7 +59,7 @@
   }
 }
 
-/** Copy constructor */
+/** Copy constructor. NOT chaining! */
 CarobException::CarobException(const CarobException& ce) :
   message(ce.message),
   stackTrace(ce.stackTrace),
@@ -117,13 +117,64 @@
   ds>>lineNumber;
 }
 
-std::wstring
-StackTraceElement::toWString() const
-{ 
-    std::wostringstream oss;
-    oss << L"at " << declaringClass << L"." << methodName
-        << L"(" << fileName << L":" << lineNumber << L")";
-    return oss.str();
+
+std::wostream&
+CarobNS::operator<< (std::wostream& oss, const CarobNS::StackTraceElement& ste)
+{
+    oss << L"at " << ste.declaringClass << L"." << ste.methodName
+        << L"(" << ste.fileName << L":" << ste.lineNumber << L")";
+    return oss;
+}
+
+std::wostream&
+CarobNS::operator<< (std::wostream& outstream, const CarobNS::StackTrace& st)
+{
+    if (st.size() == 0)
+        outstream << L"  (empty/no remote stack trace)" << std::endl;
+    else {
+        for (CarobNS::StackTrace::const_iterator s = st.begin(); s != 
st.end(); s++)
+            outstream << *s << std::endl;
+    }
+
+  return outstream;
+}
+
+std::wostream&
+CarobNS::operator<< (std::wostream& outstream, const CarobNS::BackTrace& 
stvect)
+{  
+    int status;
+    char *demangled;
+
+    for (CarobNS::BackTrace::const_iterator btframe = stvect.begin();
+         btframe != stvect.end(); btframe++) {
+
+        std::string sframe(*btframe);
+
+#ifdef CAROB_CXX_DEMANGLING
+        typedef std::string::size_type ssize;
+
+        ssize start(sframe.find('('));
+        ssize end(sframe.find('+'));
+
+        if (std::string::npos != start && std::string::npos != end)
+        {   
+            start++;
+            ssize len(end-start);
+            
+            std::string mangled(sframe.substr(start, len));
+
+            demangled = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
+            if (demangled) {
+                sframe.replace(start, len, demangled);
+                free(demangled);
+            }
+        }
+#endif
+        // not demangled frames is better than nothing: always print
+        outstream << CarobNS::StaticCodecs::fromASCII(sframe) << std::endl;
+  }
+
+  return outstream;
 }
 
 
Index: carob/test/CarobProtector.hpp
diff -u carob/test/CarobProtector.hpp:1.16 carob/test/CarobProtector.hpp:1.17
--- carob/test/CarobProtector.hpp:1.16  Fri Dec  1 22:27:14 2006
+++ carob/test/CarobProtector.hpp       Tue Dec  5 01:13:16 2006
@@ -25,10 +25,6 @@
 #include <cppunit/Protector.h>
 #include <cppunit/Message.h>
 
-#ifdef __GLIBC__ // probably not the right macro
-#include <cxxabi.h>
-#define CAROB_CXX_DEMANGLING 1
-#endif
 
 static const std::wstring BEPREFIX(L"BackendException: ");
 static const std::wstring COPREFIX(L"ControllerException: ");
@@ -37,76 +33,47 @@
 static const std::wstring NEXTPREFIX(L"chained CarobException : ");
 
 
-static void cerrStackTrace(const std::wstring& prefix,
+static void cerrStackTrace(std::wostream& outstream, const std::wstring& 
prefix,
     const CarobNS::CarobException& cex)
 {
-  std::wcerr << prefix << "SQL state: " << cex.getSQLState() << std::endl;
-  std::wcerr << prefix << "remote stack: " << std::endl;
-  const CarobNS::StackTrace& st = cex.getStackTrace();
-
-  if (st.size() == 0) {
-      std::wcerr << L"  (empty/no remote stack trace)" << std::endl;
-      return;
-  }
-
-  for (CarobNS::StackTrace::const_iterator s = st.begin(); s != st.end(); s++)
-    std::wcerr << prefix << s->toWString() << std::endl;;
+  outstream << prefix;
+  outstream << " SQL state: " << cex.getSQLState() << ", remote stack: " << 
std::endl;
+  outstream <<  cex.getStackTrace();
 }
 
 
-static void cerrBackTrace(const std::wstring& prefix,
+static void cerrBackTrace(std::wostream& outstream, const std::wstring& prefix,
     const CarobNS::CarobException& cex)
 {
-    const CarobNS::BackTrace& stvect = cex.getBackTrace();
-  
-    int status;
-    char *demangled;
-
-
-    for (CarobNS::BackTrace::const_iterator btframe = stvect.begin(); btframe 
!= stvect.end(); btframe++) {
-
-        std::string sframe(*btframe);
+    outstream << prefix << std::endl;
+    outstream << cex.getBackTrace();
+}
 
-#ifdef CAROB_CXX_DEMANGLING
-        typedef std::string::size_type ssize;
 
-        ssize start(sframe.find('('));
-        ssize end(sframe.find('+'));
+static std::wstring carobExceptionChain(const std::wstring& prefix,
+    const CarobNS::CarobException& ce)
+{
+  std::wostringstream woss;
 
-        if (std::string::npos != start && std::string::npos != end)
-        {   
-            start++;
-            ssize len(end-start);
-            
-            std::string mangled(sframe.substr(start, len));
+  cerrBackTrace(woss, prefix, ce);
+  cerrStackTrace(woss, prefix, ce);
 
-            demangled = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
-            if (demangled) {
-                sframe.replace(start, len, demangled);
-                free(demangled);
-            }
-        }
-#endif
+  const CarobNS::CarobException* nextex_p = &ce;
+  
+  while ((nextex_p = nextex_p->getNext()) != NULL) {
+    woss << "   ---  chained CarobException  --- " << std::endl;
+    cerrBackTrace(woss, NEXTPREFIX, *nextex_p);
+    cerrStackTrace(woss, NEXTPREFIX, *nextex_p);
+  } 
 
-        std::wcerr << prefix;
-        std::cerr << sframe << std::endl;
+  return woss.str();
 
-  }
 }
 
 static void cerrCarobExceptionChain(const std::wstring& prefix,
     const CarobNS::CarobException& ce)
 {
-  cerrBackTrace(prefix, ce);
-  cerrStackTrace(prefix, ce);
-
-  const CarobNS::CarobException* nextex_p = &ce;
-  
-  while ((nextex_p = nextex_p->getNext()) != NULL) {
-    std::wcerr << "   ---  chained CarobException  --- " << std::endl;
-    cerrBackTrace(NEXTPREFIX, *nextex_p);
-    cerrStackTrace(NEXTPREFIX, *nextex_p);
-  } 
+    CarobNS::logError(L"cerrCarobExceptionChain", carobExceptionChain(prefix, 
ce));
 }
 
 

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to