LogBlock.hpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. #ifndef __LOG_BLOCK_HPP__
  23. #define __LOG_BLOCK_HPP__
  24. //
  25. // Logging class to aid for simple log machanism.
  26. // The use of this class is to create a static object in the local scope,
  27. // and to be destroyed when exiting the scope.  It is not designed
  28. // to be dynamically created and passed.
  29. //
  30. class LogBlock
  31. {
  32.   /**
  33.    * Constructor printing a message that we are entering a block.  The return
  34.    * code of the block is initialized to the value of <code>rc</code> which
  35.    * defaults to <code>0</code>.
  36.    **/
  37.  public:
  38.   LogBlock
  39.   (
  40.    VXIlogInterface *log, 
  41.    unsigned int tag, 
  42.    const wchar_t* subtag,
  43.    const wchar_t* module,
  44.    int rc = 0
  45.   )
  46.   : _logHandle(log), _tag(tag), _subtag(subtag), _module(module), _rc(rc), 
  47.    _tagIsEnabled(2), _isEnableFlag(false)
  48.   {
  49.     if (isEnabled())
  50.       logDiagnostic(L"entered.");
  51.   }
  52.   /**
  53.    * Destructor printing a message that we are exiting a block.
  54.    **/
  55.   ~LogBlock()
  56.   {
  57.     if (isEnabled())
  58.       logDiagnostic(L"return: rc = %d", _rc);
  59.   }
  60.  public:
  61.   /**
  62.    * Sets the return code for this block.
  63.    *
  64.    * @param _rc The new value of the return code for the block.
  65.    * @return <code>this</code>.
  66.    **/
  67.   LogBlock& operator=(int rc)
  68.   {
  69.     _rc = rc;
  70.     return *this;
  71.   }
  72.  public:
  73.   /**
  74.    * Converts a LogBlock into an int.
  75.    * @return the current return code for this LogBlock.
  76.    **/
  77.   operator int()
  78.   {
  79.     return _rc;
  80.   }
  81.  public:
  82.   bool isEnabled()
  83.   {
  84.     if(_tagIsEnabled ==2)
  85.     {
  86.       _isEnableFlag = isEnabled(_tag);
  87.       _tagIsEnabled = 1;
  88.     }
  89.     return _isEnableFlag;
  90.   }
  91.  public:
  92.   bool isEnabled(unsigned int tag)
  93.   {
  94.     return (_logHandle != NULL &&
  95.             _logHandle->DiagnosticIsEnabled != NULL &&
  96.             _logHandle->DiagnosticIsEnabled(_logHandle, tag));
  97.   }
  98.  public:
  99.   // Log diagnostic messages based on the tag number passed at construction time
  100.   void logDiagnostic(const wchar_t *format, ...)
  101.   {
  102.     va_list vargs;
  103.     va_start(vargs, format);
  104.     if (_logHandle != NULL && isEnabled() && _logHandle->VDiagnostic != NULL)
  105.     {
  106.       (_logHandle->VDiagnostic)(_logHandle, _tag, _subtag, format, vargs);
  107.     }
  108.     va_end(vargs);
  109.   }
  110.   void logDiagnostic(const wchar_t *format, va_list vargs)
  111.   {
  112.    if (_logHandle != NULL && isEnabled() && _logHandle->VDiagnostic != NULL)
  113.    {
  114.      (_logHandle->VDiagnostic)(_logHandle, _tag, _subtag, format, vargs);
  115.    }
  116. }
  117.   // Log diagnostic messages based on the tag number passed at construction time 
  118.   // plus the offset argument
  119.   void logDiag(int offset, const wchar_t *format, ...)
  120.   {
  121.     va_list vargs;
  122.     va_start(vargs, format);
  123.     if (_logHandle != NULL && _logHandle->VDiagnostic != NULL)
  124.     {
  125.       (_logHandle->VDiagnostic)(_logHandle, _tag + offset, _subtag, format, vargs);
  126.     }
  127.     va_end(vargs);
  128.   }
  129.   void logDiag(int offset, const wchar_t *format, va_list vargs)
  130.   {
  131.    if (_logHandle != NULL && _logHandle->VDiagnostic != NULL)
  132.    {
  133.      (_logHandle->VDiagnostic)(_logHandle, _tag + offset, _subtag, format, vargs);
  134.    }
  135. }
  136.  public:
  137.   void logError(VXIunsigned errorID, const wchar_t *format, ...)
  138.   {
  139.     va_list vargs;
  140.     va_start(vargs, format);
  141.     if (_logHandle != NULL && _logHandle->VError != NULL)
  142.     {
  143.      if( format )
  144.        (_logHandle->VError)(_logHandle, _module, errorID, format, vargs);
  145.      else
  146.        (_logHandle->Error)(_logHandle, _module, errorID, NULL);    
  147.     }
  148.     va_end(vargs);
  149.   }
  150.   void logError(VXIunsigned errorID, const wchar_t *format, va_list vargs)
  151.   {
  152.     if (_logHandle != NULL && _logHandle->VError != NULL)
  153.     {
  154.       (_logHandle->VError)(_logHandle, _module, errorID, format, vargs);
  155.     }
  156.   }
  157.   
  158.   /* Just to ensure that LogBlock cannot be copied. */
  159.  private:
  160.   LogBlock(const LogBlock&);
  161.   LogBlock& operator=(const LogBlock&);
  162.  private:
  163.   // members
  164.   VXIlogInterface *_logHandle;
  165.   int _tagIsEnabled;
  166.   bool _isEnableFlag;
  167.   unsigned int _tag;
  168.   const wchar_t* _subtag;
  169.   const wchar_t* _module;
  170.   int _rc;
  171. };
  172. #endif