• Classes
  • Modules
  • Namespaces
  • Files
  • Related Pages
  • File List
  • File Members

acserr.h

Go to the documentation of this file.
00001 #ifndef _ACSERR__H_
00002 #define _ACSERR__H_
00003 /*******************************************************************************
00004 *    ALMA - Atacama Large Millimiter Array
00005 *    (c) European Southern Observatory, 2002
00006 *    Copyright by ESO (in the framework of the ALMA collaboration)
00007 *    and Cosylab 2002, All rights reserved
00008 *
00009 *    This library is free software; you can redistribute it and/or
00010 *    modify it under the terms of the GNU Lesser General Public
00011 *    License as published by the Free Software Foundation; either
00012 *    version 2.1 of the License, or (at your option) any later version.
00013 *
00014 *    This library is distributed in the hope that it will be useful,
00015 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 *    Lesser General Public License for more details.
00018 *
00019 *    You should have received a copy of the GNU Lesser General Public
00020 *    License along with this library; if not, write to the Free Software
00021 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00022 *
00023 * "@(#) $Id: acserr.h,v 1.81 2010/05/31 09:36:51 bjeram Exp $"
00024 *
00025 * who       when      what
00026 * --------  --------  ----------------------------------------------
00027 * almamgr 2004-03-02 added last() to ErrorTraceHelper
00028 * almamgr 2004-03-02 added isErrorFree, log and getErrorTraceHelper to CompletionImpl
00029 * bjeram 2003-07-07 added std:: prefix (gcc 3.2.x)
00030 * bjeram 2003-03-06 added strstream #include
00031 1* bjeram 2003-03-06 added ACSErr prefix to types defined in the idl files
00032 * bjeram 2002-06-05 added setTimeStamp
00033 * bjeram 2002-06-05 added ACSError (const char* file, int line, ACSError &err, ACSErrType et, ACSErr::ErrorCode ec, const char *routine, ACSErr::Severity severity);
00034 * bjeram 2002-06-04 moved getDescription(type, code) to public and made it static
00035 * bjeram 2002-06-04 fixed ML in addData<T>
00036 * bjeram 2002-02-13 added ACSError() and ACS_ERROR() for creating no-error object w/o runtime and source info
00037 * bjeram 2001-10-18 overloaded ACS_EXCEPTION and (RE)THROW_ACS_EXCEPTIONS(_EX) macros
00038 * bjeram 2001-10-18 added RETHROW_ACS_EXCEPTION_EX
00039 * bjeram 2001-10-18 added  THROW_ACS_EXCEPTION_EX
00040 * bjeram 2001-10-18 overloaded ACS_ERROR macro
00041 * almamgr  20/06/01  created
00042 */
00043 
00044 #ifndef __cplusplus
00045 #error This is a C++ include file and cannot be used from plain C
00046 #endif
00047 
00048 
00049 #include "acserrLegacy.h"
00050 #include <sstream>
00051 
00052 // forward declaration
00053 class ACSLogImpl;
00054 
00055 namespace ACSErr
00056 {
00057 
00063 class ErrorTraceHelper
00064 {
00065   protected:
00066     friend class CompletionImpl;
00067     friend class ::ACSLogImpl;
00068     friend class ::ACSError;
00069 
00070     ErrorTraceHelper(ACSErr::ErrorTrace &et);
00071 
00072 // for OK cases and wrapping error completion
00073      void setErrorTrace(ACSErr::ErrorTrace &et, int depth);
00074 
00075 // default constructor (OK completion)
00076     ErrorTraceHelper()
00077         : m_errorTracePtr(0), m_current(0), m_depth(0)
00078         {}
00079 
00080 // create new error trace
00081     ErrorTraceHelper (ACSErr::ACSErrType et, ACSErr::ErrorCode ec,
00082                       const char* file, int line, const char* routine, const char* sd,
00083                       ACSErr::Severity severity,
00084                       ACSErr::ErrorTrace &errortrace);
00085 
00086 // adding previos error trace
00087     ErrorTraceHelper (const ACSErr::ErrorTrace &pet,
00088                       ACSErr::ACSErrType et, ACSErr::ErrorCode ec,
00089                       const char* file, int line, const char* routine, const char* sd, 
00090                       ACSErr::Severity severity,
00091                       ACSErr::ErrorTrace &errortrace);
00092 
00096     ErrorTraceHelper& operator=(ACSErr::ErrorTrace& eth);
00097 
00098   public:
00099 
00100 
00117     void log(ACE_Log_Priority priorty=LM_ERROR);
00118 
00122     std::string toString();
00123 
00130     void addData (const char* name, const char* value);
00131     
00136     void addData (const char* name, char* value)
00137         {
00138             addData (name, (const char*)value);
00139         }//addData
00140 
00148     template<typename T>
00149     void addData (const char* name, T value)
00150         {
00151             const char *s;
00152             std::ostringstream ostr;
00153             ostr << value << std::ends;
00154             std::string ts=ostr.str(); // we have to make a temporary string otherwise there is problem with memory:  s = ostr.str().c_str(); does not work
00155             s = ts.c_str();
00156             addData (name, s);
00157         }//addData
00158 
00165     void setMemberValue (const char* name, const char* value);
00166 
00173     void setMemberValue (const char* name, ACE_CString &value);
00174 
00181     template<typename T>
00182     void setMemberValue (const char* name, T value)
00183         { 
00184             const char *s;
00185             if (name==NULL) return;
00186 
00187             std::ostringstream ostr;
00188             ostr << value << std::ends;
00189             std::string ts=ostr.str(); // we have to make a temporary string otherwise there is problem with memory
00190             s = ts.c_str();
00191             setMemberValue (name, s);
00192         }//setMemberValue
00193 
00199     ACE_CString getData (const char* name);
00200 
00207     void getMemberValue(const char* name, char*& value)
00208         {
00209             value = CORBA::string_dup(getData(name).c_str());
00210         }
00211     
00219     template<typename T>
00220     void getMemberValue (const char* name, T &value)
00221         {
00222             std::istringstream istr(getData(name).c_str());
00223             istr >> value;
00224         }//getMemberValue
00225 
00232     template<typename T>
00233     T getMemberValue (const char* name);
00234 
00241     char* getDescription();
00242 
00247     char* getShortDescription();
00248 
00253   char* getFileName(){ return CORBA::string_dup(m_current->file); }
00254 
00259   CORBA::ULong getLineNumber(){ return m_current->lineNum; }
00260 
00265   char* getRoutine(){ return CORBA::string_dup (m_current->routine); }
00266 
00271   char* getHostName(){ return CORBA::string_dup (m_current->host); }
00272 
00277   char* getProcessName(){ return CORBA::string_dup (m_current->process); }
00278 
00283   char* getThread(){ return CORBA::string_dup (m_current->thread); }
00284 
00289   char* getSourceObject(){ return CORBA::string_dup (m_current->sourceObject); }
00294   CORBA::ULongLong getTimeStamp (){ return m_current->timeStamp; }
00295 
00300     ACSErr::ErrorCode getErrorCode(){ return m_current->errorCode; }
00301 
00306     ACSErr::ACSErrType getErrorType(){ return m_current->errorType; }
00307 
00311   ACSErr::Severity getSeverity() { return m_current->severity; }
00312 
00317   unsigned int getDepth(){ return m_depth; }
00318 
00323  void setTimeStamp (CORBA::ULongLong time){ m_current->timeStamp = time; }
00324 
00330   void setSourceObject(const char* so){ m_current->sourceObject = CORBA::string_dup (so); }
00331 
00337   void setFileName(const char* fn){ m_current->file = CORBA::string_dup (fn); }
00338 
00344   void setLineNumber (CORBA::ULong ln){ m_current->lineNum = ln; }//? should we delete previos one
00345 
00352   void setError (ACSErr::ACSErrType ty, ACSErr::ErrorCode ec) 
00353         { m_current->errorType=ty; m_current->errorCode=ec; }
00354 
00360   void setSeverity(ACSErr::Severity severity) {m_current->severity = severity; }
00361 
00366   static void setHostName (const char* hn);
00367   
00372   static void setProcessName (const char *pn); 
00373 
00379   ACSErr::ErrorTrace *getNext();
00380 
00385     bool last() { return (m_depth==0 || m_current->previousError.length()==0); }
00386   
00391   ACSErr::ErrorTrace *top(){ m_current = m_errorTracePtr; return m_current;}
00392 
00398     ACSErr::ErrorTrace& getErrorTrace(){ return *m_current; } // should check if stack is empty
00399     
00400     static ACS::Time getTime();
00401     
00407     ErrorTraceHelper*  getErrorTraceHelper() { return this; }
00408 
00409   protected:
00410 
00411     void fill (ACSErr::ACSErrType et, ACSErr::ErrorCode ec, ACSErr::Severity severity,
00412                const char* file, int line, const char* routine, const char* sd);
00413 
00422     void log (ACSErr::ErrorTrace * c,
00423               int level, char *stackId,
00424               ACE_Log_Priority priorty=LM_ERROR);
00425 
00426     void toString (ACSErr::ErrorTrace * c, int level, std::ostringstream& oss);
00427 
00428     ACSErr::ErrorTrace *m_errorTracePtr;
00429 //& m_errorTraceRef;
00430     ACSErr::ErrorTrace *m_current;
00431     unsigned int m_depth;
00432     
00433     static char m_hostName[64];
00434     static char m_processName[64];
00435     static const unsigned int m_maxDepth;
00436 };
00437 
00438 /*******************************************************************************************/
00443 class CompletionInit : public ACSErr::Completion
00444 {
00445   public:
00446     CompletionInit(const ACSErr::Completion &c);
00447 
00448     CompletionInit(ACSErr::CompletionType t, ACSErr::CompletionCode c, bool initTrace=true);
00449     
00454     ACSErr::CompletionCode getCode(){ return code; }
00455 
00460     ACSErr::CompletionType getType(){ return type; }
00461 
00466   CORBA::ULongLong getTimeStamp (){ return timeStamp; }
00467 };
00468 
00469 
00470 
00471 
00472 /**************************************************************************************/
00477 class CompletionImpl : public CompletionInit 
00478 {
00479   public:
00480 // default constructor
00481     CompletionImpl();
00482 
00483     CompletionImpl (ACSErr::ACSErrType t, ACSErr::ErrorCode c) :
00484         CompletionInit(t, c, false)/*,
00485         m_errorTraceHelper(previousError[0], previousError.length())
00486 */
00487         {
00488         }
00489 
00490     CompletionImpl (ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00491                       const char* file, int line, const char* routine, const char* sd,
00492                       ACSErr::Severity severity) : 
00493         CompletionInit(t, c),
00494         m_errorTraceHelper(t, c, file, line, routine, sd, severity, previousError[0])
00495         {}
00496 
00497 // adding previous (remote or local) with reference 
00498     CompletionImpl (const ACSErr::Completion &pc, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00499                       const char* file, int line, const char* routine, const char* sd,
00500                       ACSErr::Severity severity) :
00501         CompletionInit(t, c),
00502         m_errorTraceHelper(pc.previousError[0], t, c, file, line, routine, sd, severity, previousError[0])
00503         {}
00504 // adding previous remote completion as pointer
00505     CompletionImpl (ACSErr::Completion *pc, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00506                       const char* file, int line, const char* routine, const char* sd,
00507                       ACSErr::Severity severity) :
00508         CompletionInit(t, c),
00509         m_errorTraceHelper(pc->previousError[0], t, c, file, line, routine, sd, severity, previousError[0])
00510         { delete pc; }
00511 
00512 // adding previous completion as pointer
00513     CompletionImpl (CompletionImpl *pc, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00514                       const char* file, int line, const char* routine, const char* sd,
00515                       ACSErr::Severity severity) :
00516         CompletionInit(t, c),
00517         m_errorTraceHelper(pc->previousError[0], t, c, file, line, routine, sd, severity, previousError[0])
00518         { delete pc; }
00519 
00520 // adding error trace
00521     CompletionImpl (const ACSErr::ErrorTrace &et, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00522                       const char* file, int line, const char* routine, const char* sd,
00523                       ACSErr::Severity severity) :
00524         CompletionInit(t, c),
00525         m_errorTraceHelper(et, t, c, file, line, routine, sd, severity, previousError[0])
00526         {}
00527 
00528 //wrapping remote (CORBA) Completion
00534     CompletionImpl(ACSErr::Completion* c, bool del=true);
00535 
00541     CompletionImpl(ACSErr::Completion_var& c);
00542 
00543     CompletionImpl (const ACSErr::Completion &c);
00544 
00548     CompletionImpl (const CompletionImpl &c);
00549 
00553     virtual ~CompletionImpl(){}
00554     
00564     ACSErr::Completion* returnCompletion (bool deletion=true)
00565         {
00566             ACSErr::Completion *tmp = new ACSErr::Completion(*this);
00567             if (deletion) delete this;
00568             return tmp;
00569         }//returnCompletion
00570 
00581     ACSErr::Completion* outCompletion(bool del=false) { return this->returnCompletion(del); }
00582 
00588     bool isErrorFree(){ return (previousError.length() == 0); }
00589 
00594     ErrorTraceHelper*  getErrorTraceHelper(){ return (ErrorTraceHelper*)( (previousError.length() > 0) ? &m_errorTraceHelper : NULL); }
00595     
00601     void log(ACE_Log_Priority priorty=LM_ERROR);
00602 
00603     template<typename T>
00604     void addData (const char* name, T value)
00605         {
00606             if (!isErrorFree())
00607                 {
00608                 m_errorTraceHelper.addData(name, value);
00609                 }
00610         }
00611 
00612 
00613     CompletionImpl& operator=(CompletionImpl&);
00614 
00620     CompletionImpl& operator=(Completion* c);
00621 
00627     CompletionImpl& operator=(Completion_var& c);
00628 
00629 
00630 
00631   protected:
00632     ErrorTraceHelper m_errorTraceHelper;
00633 };//CompletionImpl
00634 
00635 /*************************************************************************************/
00636 //template implementation
00637 //
00638 
00639 // specialization for strings
00640 template<>
00641 char * ErrorTraceHelper::getMemberValue<char*> (const char* name);
00642 
00643 template<>
00644 ACE_CString ErrorTraceHelper::getMemberValue<ACE_CString> (const char* name);
00645 
00646 template<typename T>
00647 T ErrorTraceHelper::getMemberValue (const char* name)
00648 {
00649     T value;
00650     std::istringstream istr(getData(name).c_str());
00651     istr >> value;
00652     return value;
00653 }//getMemberValue
00654 
00655 }
00656 
00657 // these two lines are just for backward compatibility and should be removed
00658 // ... with next release
00659 typedef ACSErr::CompletionInit CompletionInit;
00660 typedef ACSErr::CompletionImpl CompletionImpl;
00661 typedef ACSErr::ErrorTraceHelper ErrorTraceHelper;
00662 
00663 #endif

Generated on Thu Jan 12 2012 23:13:50 for ACS-10.0 C++ API by  doxygen 1.7.0