Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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.75 2006/10/24 10:44:30 gchiozzi 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 namespace ACSErr
00053 {
00054 
00060 class ErrorTraceHelper
00061 {
00062   protected:
00063     friend class CompletionImpl;
00064     friend class ACSLogImpl;
00065     friend class ACSError;
00066 
00067     ErrorTraceHelper(ACSErr::ErrorTrace &et);
00068 
00069 // for OK cases and wrapping completion
00070     ErrorTraceHelper(ACSErr::ErrorTrace &et, int depth);
00071 
00072 // default constructor
00073     ErrorTraceHelper();
00074 
00075 // create new error trace
00076     ErrorTraceHelper (ACSErr::ACSErrType et, ACSErr::ErrorCode ec,
00077                       const char* file, int line, const char* routine, const char* sd,
00078                       ACSErr::Severity severity,
00079                       ACSErr::ErrorTrace &errortrace);
00080 
00081 // adding previos error trace
00082     ErrorTraceHelper (const ACSErr::ErrorTrace &pet,
00083                       ACSErr::ACSErrType et, ACSErr::ErrorCode ec,
00084                       const char* file, int line, const char* routine, const char* sd, 
00085                       ACSErr::Severity severity,
00086                       ACSErr::ErrorTrace &errortrace);
00087 
00091     ErrorTraceHelper& operator=(ACSErr::ErrorTrace& eth);
00092 
00093   public:
00094 
00095 
00112     void log(ACE_Log_Priority priorty=LM_ERROR);
00113 
00117     std::string toString();
00118 
00125     void addData (const char* name, const char* value);
00126     
00131     void addData (const char* name, char* value)
00132         {
00133             addData (name, (const char*)value);
00134         }//addData
00135 
00142     template<typename T>
00143     void addData (const char* name, T value)
00144         {
00145             const char *s;
00146             std::ostringstream ostr;
00147             ostr << value << std::ends;
00148             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
00149             s = ts.c_str();
00150             addData (name, s);
00151         }//addData
00152 
00153 
00161     void setMemberValue (const char* name, const char* value);
00162 
00171     void setMemberValue (const char* name, ACE_CString &value);
00172 
00179     template<typename T>
00180     void setMemberValue (const char* name, T value)
00181         { 
00182             const char *s;
00183             if (name==NULL) return;
00184 
00185             std::ostringstream ostr;
00186             ostr << value << std::ends;
00187             std::string ts=ostr.str(); // we have to make a temporary string otherwise there is problem with memory
00188             s = ts.c_str();
00189             setMemberValue (name, s);
00190         }//setMemberValue
00191 
00197     ACE_CString getData (const char* name);
00198 
00206     void getMemberValue(const char* name, char*& value)
00207         {
00208             value = CORBA::string_dup(getData(name).c_str());
00209         }
00210     
00218     template<typename T>
00219     void getMemberValue (const char* name, T &value)
00220         {
00221             std::istringstream istr(getData(name).c_str());
00222             istr >> value;
00223         }//getMemberValue
00224 
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_errorTraceRef; 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_errorTraceRef;
00429     ACSErr::ErrorTrace *m_current;
00430     unsigned int m_depth;
00431     
00432     static char m_hostName[64];
00433     static char m_processName[64];
00434     static const unsigned int m_maxDepth;
00435 };
00436 
00437 /*******************************************************************************************/
00442 class CompletionInit : public ACSErr::Completion
00443 {
00444   public:
00445     CompletionInit(const ACSErr::Completion &c);
00446 
00447     CompletionInit(ACSErr::CompletionType t, ACSErr::CompletionCode c, bool initTrace=true);
00448     
00453     ACSErr::CompletionCode getCode(){ return code; }
00454 
00459     ACSErr::CompletionType getType(){ return type; }
00460 
00465   CORBA::ULongLong getTimeStamp (){ return timeStamp; }
00466 };
00467 
00468 
00469 
00470 
00471 /**************************************************************************************/
00476 class CompletionImpl : public CompletionInit 
00477 {
00478   public:
00479 // default constructor
00480     CompletionImpl();
00481 
00482     CompletionImpl (ACSErr::ACSErrType t, ACSErr::ErrorCode c) :
00483         CompletionInit(t, c, false),
00484         m_errorTraceHelper(previousError[0], previousError.length())
00485         {
00486         }
00487 
00488     CompletionImpl (ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00489                       const char* file, int line, const char* routine, const char* sd,
00490                       ACSErr::Severity severity) : 
00491         CompletionInit(t, c),
00492         m_errorTraceHelper(t, c, file, line, routine, sd, severity, previousError[0])
00493         {}
00494 
00495 // adding previous (remote or local) with reference 
00496     CompletionImpl (const ACSErr::Completion &pc, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00497                       const char* file, int line, const char* routine, const char* sd,
00498                       ACSErr::Severity severity) :
00499         CompletionInit(t, c),
00500         m_errorTraceHelper(pc.previousError[0], t, c, file, line, routine, sd, severity, previousError[0])
00501         {}
00502 // adding previous remote completion as pointer
00503     CompletionImpl (ACSErr::Completion *pc, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00504                       const char* file, int line, const char* routine, const char* sd,
00505                       ACSErr::Severity severity) :
00506         CompletionInit(t, c),
00507         m_errorTraceHelper(pc->previousError[0], t, c, file, line, routine, sd, severity, previousError[0])
00508         { delete pc; }
00509 
00510 // adding previous completion as pointer
00511     CompletionImpl (CompletionImpl *pc, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00512                       const char* file, int line, const char* routine, const char* sd,
00513                       ACSErr::Severity severity) :
00514         CompletionInit(t, c),
00515         m_errorTraceHelper(pc->previousError[0], t, c, file, line, routine, sd, severity, previousError[0])
00516         { delete pc; }
00517 
00518 // adding error trace
00519     CompletionImpl (const ACSErr::ErrorTrace &et, ACSErr::ACSErrType t, ACSErr::ErrorCode c,
00520                       const char* file, int line, const char* routine, const char* sd,
00521                       ACSErr::Severity severity) :
00522         CompletionInit(t, c),
00523         m_errorTraceHelper(et, t, c, file, line, routine, sd, severity, previousError[0])
00524         {}
00525 
00526 //wrapping remote (CORBA) Completion
00532     CompletionImpl(ACSErr::Completion* c, bool del=true);
00533 
00534     CompletionImpl (const ACSErr::Completion &c);
00535 
00539     CompletionImpl (const CompletionImpl &c);
00540 
00544     virtual ~CompletionImpl(){}
00545     
00551   ACSErr::Completion* returnCompletion (bool deletion=true)
00552         {
00553             ACSErr::Completion *tmp = new ACSErr::Completion(*this);
00554             if (deletion) delete this;
00555             return tmp;
00556         }
00557 
00563     bool isErrorFree(){ return (previousError.length() == 0); }
00564 
00569     ErrorTraceHelper*  getErrorTraceHelper(){ return (ErrorTraceHelper*)( (previousError.length() > 0) ? &m_errorTraceHelper : NULL); }
00570     
00576     void log(ACE_Log_Priority priorty=LM_ERROR);
00577 
00578     template<typename T>
00579     void addData (const char* name, T value)
00580         {
00581             if (!isErrorFree())
00582                 {
00583                 m_errorTraceHelper.addData(name, value);
00584                 }
00585         }
00586 
00587     CompletionImpl& operator=(CompletionImpl&);
00588 
00593     CompletionImpl& operator=(Completion* c);
00594 
00595   protected:
00596     ErrorTraceHelper m_errorTraceHelper;
00597 };//CompletionImpl
00598 
00599 /*************************************************************************************/
00600 //template implementation
00601 //
00602 
00603 // specialization for strings
00604 template<>
00605 char * ErrorTraceHelper::getMemberValue<char*> (const char* name);
00606 
00607 template<>
00608 ACE_CString ErrorTraceHelper::getMemberValue<ACE_CString> (const char* name);
00609 
00610 template<typename T>
00611 T ErrorTraceHelper::getMemberValue (const char* name)
00612 {
00613     T value;
00614     std::istringstream istr(getData(name).c_str());
00615     istr >> value;
00616     return value;
00617 }//getMemberValue
00618 
00619 }
00620 
00621 // these two lines are just for backward compatibility and should be removed
00622 // ... with next release
00623 typedef ACSErr::CompletionInit CompletionInit;
00624 typedef ACSErr::CompletionImpl CompletionImpl;
00625 typedef ACSErr::ErrorTraceHelper ErrorTraceHelper;
00626 
00627 #endif

Generated on Sun Oct 29 02:26:25 2006 for ACS C++ API by doxygen 1.3.6