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

scim_debug.h

Go to the documentation of this file.
00001 /**
00002  * @file scim_debug.h
00003  * @brief Defines class scim::DebugOutput and related MACROS.
00004  *
00005  * All of the debug information should be output via scim::DebugOutput class.
00006  * This class provides message filter and redirection ability.
00007  */
00008 
00009 /*
00010  * Smart Common Input Method
00011  * 
00012  * Copyright (c) 2004 James Su <suzhe@turbolinux.com.cn>
00013  * Copyright (c) 2003 James Su <suzhe@turbolinux.com.cn>
00014  * Copyright (c) 2002 James Su <suzhe@turbolinux.com.cn>
00015  *
00016  *
00017  * This library is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU Lesser General Public
00019  * License as published by the Free Software Foundation; either
00020  * version 2 of the License, or (at your option) any later version.
00021  *
00022  * This library is distributed in the hope that it will be useful,
00023  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  * GNU Lesser General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU Lesser General Public
00028  * License along with this program; if not, write to the
00029  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00030  * Boston, MA  02111-1307  USA
00031  *
00032  * $Id: scim_debug.h,v 1.12 2004/02/06 07:53:15 suzhe Exp $
00033  */
00034 
00035 #ifndef __SCIM_DEBUG_H
00036 #define __SCIM_DEBUG_H
00037 
00038 #define SCIM_DEBUG_MAX_VERBOSE      7
00039 
00040 namespace scim {
00041 
00042 /**
00043  * @name The mask for debug messages filtering.
00044  * @{
00045  */
00046 #define SCIM_DEBUG_AllMask          (~0) /**< Show all messages. */
00047 #define SCIM_DEBUG_MainMask         1    /**< Show messages of main application. */
00048 #define SCIM_DEBUG_ConfigMask       2    /**< Show messages of Config objects */
00049 #define SCIM_DEBUG_ServerMask       4    /**< Show messages of Server objects */
00050 #define SCIM_DEBUG_BackEndMask      8    /**< Show messages of BackEnd objects */
00051 #define SCIM_DEBUG_FrontEndMask     16   /**< Show messages of FrontEnd objects */
00052 #define SCIM_DEBUG_ModuleMask       32   /**< Show messages of Module objects */
00053 #define SCIM_DEBUG_UtilityMask      64   /**< Show messages of utility functions */
00054 #define SCIM_DEBUG_IConvMask        128  /**< Show messages of IConvert objects */
00055 #define SCIM_DEBUG_LookupTableMask  256  /**< Show messages of LookupTable objects */
00056 #define SCIM_DEBUG_SocketMask       512  /**< Show messages of Socket objects */
00057 /**
00058  * @}
00059  */
00060 
00061 /**
00062  * @name The macros to simplify the debug message print method.
00063  *
00064  * You can output debug messages by this way:
00065  *   SCIM_DEBUG_SERVER(1) << "Hello World!\n";
00066  *
00067  * @{
00068  */
00069 #define SCIM_DEBUG(mask,level)        (scim::DebugOutput(mask,level) << __FILE__ << ":" << __LINE__ << " > ")
00070 #define SCIM_DEBUG_MAIN(level)        SCIM_DEBUG(SCIM_DEBUG_MainMask,level)
00071 #define SCIM_DEBUG_CONFIG(level)      SCIM_DEBUG(SCIM_DEBUG_ConfigMask,level)
00072 #define SCIM_DEBUG_SERVER(level)      SCIM_DEBUG(SCIM_DEBUG_ServerMask,level)
00073 #define SCIM_DEBUG_BACKEND(level)     SCIM_DEBUG(SCIM_DEBUG_BackEndMask,level)
00074 #define SCIM_DEBUG_FRONTEND(level)    SCIM_DEBUG(SCIM_DEBUG_FrontEndMask,level)
00075 #define SCIM_DEBUG_MODULE(level)      SCIM_DEBUG(SCIM_DEBUG_ModuleMask,level)
00076 #define SCIM_DEBUG_UTILITY(level)     SCIM_DEBUG(SCIM_DEBUG_UtilityMask,level)
00077 #define SCIM_DEBUG_ICONV(level)       SCIM_DEBUG(SCIM_DEBUG_IConvMask,level)
00078 #define SCIM_DEBUG_LOOKUPTABLE(level) SCIM_DEBUG(SCIM_DEBUG_LookupTableMask,level)
00079 #define SCIM_DEBUG_SOCKET(level)      SCIM_DEBUG(SCIM_DEBUG_SocketMask,level)
00080 /**
00081  * @}
00082  */
00083 
00084 /**
00085  * @brief The class to filter and redirect the debug messages.
00086  */
00087 #if ENABLE_DEBUG
00088 class DebugOutput
00089 {
00090     uint32 m_mask;
00091     uint32 m_verbose;
00092 
00093 private:
00094     static uint32          verbose_level;
00095     static uint32          output_mask;
00096     static std::ostream   *output_stream;
00097 
00098 public:
00099     /**
00100      * @brief Constructor.
00101      * @param mask - the debug filter mask.
00102      * @param verbose - the verbose level of the debug message.
00103      */
00104     DebugOutput (uint32 mask = SCIM_DEBUG_AllMask, uint32 verbose = 1)
00105         : m_mask (mask), m_verbose (verbose) { }
00106 
00107     /**
00108      * @brief A template stream output operator.
00109      *
00110      * All kinds of data and variables can be output via DebugOutput by
00111      * this operator.
00112      */
00113     template <typename T>
00114     const DebugOutput& operator << (const T& t) const {
00115         if (output_stream && (m_mask & output_mask)
00116             && (m_verbose <= verbose_level))
00117             (*output_stream) << t;
00118         return *this;
00119     }
00120 
00121 public:
00122     /** 
00123      * @brief The global method to enable the debug output.
00124      * @param debug - the mask to indicate which kind of
00125      *                debug should be enabled.
00126      */
00127     static void enable_debug (uint32 debug);
00128 
00129     /**
00130      * @brief The global method to enable the debug output by their names.
00131      * @param debug - the name of the debug type to be enabled. The valid
00132      *                names are: all, main, config, server, backend, frontend,
00133      *                module, utility, iconv, lookuptable, socket.
00134      */
00135     static void enable_debug_by_name (const String &debug);
00136 
00137     /**
00138      * @brief Disable the debug type indicated by the given mask.
00139      * @param debug - the mask of the debug type to be disabled.
00140      */
00141     static void disable_debug (uint32 debug);
00142 
00143     /**
00144      * @brief Disable the debug type indicated by the given name.
00145      * @param debug - the name of the debug type to be disabled.
00146      */
00147     static void disable_debug_by_name (const String &debug);
00148 
00149     /**
00150      * @brief Set the debug verbose level.
00151      * @param verbose - the debug verbose level, 0 means no debug output.
00152      */
00153     static void set_verbose_level (uint32 verbose);
00154 
00155     /**
00156      * @brief Set the debug output file.
00157      *
00158      * @param file - the file to store the debug output.
00159      *               If equal to "stderr" or "cerr" then the debug
00160      *               output will be set to std:cerr.
00161      *               If equal to "stdout" or "cout" then the debug
00162      *               output will be set to std::cout.
00163      */
00164     static void set_output (const String &file);
00165 };
00166 #else
00167 class DebugOutput
00168 {
00169 private:
00170     static uint32          verbose_level;
00171     static uint32          output_mask;
00172     static std::ostream   *output_stream;
00173 
00174 public:
00175     DebugOutput (uint32 mask = SCIM_DEBUG_AllMask, uint32 verbose = 1){ }
00176 
00177     template <typename T>
00178     const DebugOutput& operator << (const T& t) const {
00179         return *this;
00180     }
00181 
00182 public:
00183     static void enable_debug (uint32 debug);
00184     static void enable_debug_by_name (const String &debug);
00185 
00186     static void disable_debug (uint32 debug);
00187     static void disable_debug_by_name (const String &debug);
00188 
00189     static void set_verbose_level (uint32 verbose);
00190     static void set_output (const String &file);
00191 };
00192 #endif
00193 
00194 } // namespace scim
00195 
00196 #endif //__SCIM_DEBUG_H
00197 /*
00198 vi:ts=4:nowrap:ai:expandtab
00199 */

Generated on Fri May 7 17:27:25 2004 for scim by doxygen 1.3.6