1 /**
2  * @file scim_debug.h
3  * @brief Defines class scim::DebugOutput and related MACROS.
4  *
5  * All of the debug information should be output via scim::DebugOutput class.
6  * This class provides message filter and redirection ability.
7  */
8 
9 /*
10  * Smart Common Input Method
11  *
12  * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
13  *
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this program; if not, write to the
27  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
28  * Boston, MA  02111-1307  USA
29  *
30  * $Id: scim_debug.h,v 1.18 2005/08/05 01:54:24 suzhe Exp $
31  */
32 
33 #ifndef __SCIM_DEBUG_H
34 #define __SCIM_DEBUG_H
35 
36 #define SCIM_DEBUG_MAX_VERBOSE      7
37 
38 namespace scim {
39 
40 /**
41  * @name The mask for debug messages filtering.
42  * @{
43  */
44 #define SCIM_DEBUG_AllMask          ((uint32)~0) /**< Show all messages. */
45 #define SCIM_DEBUG_MainMask         1    /**< Show messages of main application. */
46 #define SCIM_DEBUG_ConfigMask       2    /**< Show messages of Config objects */
47 #define SCIM_DEBUG_IMEngineMask     4    /**< Show messages of IMEngine objects */
48 #define SCIM_DEBUG_BackEndMask      8    /**< Show messages of BackEnd objects */
49 #define SCIM_DEBUG_FrontEndMask     16   /**< Show messages of FrontEnd objects */
50 #define SCIM_DEBUG_ModuleMask       32   /**< Show messages of Module objects */
51 #define SCIM_DEBUG_UtilityMask      64   /**< Show messages of utility functions */
52 #define SCIM_DEBUG_IConvMask        128  /**< Show messages of IConvert objects */
53 #define SCIM_DEBUG_LookupTableMask  256  /**< Show messages of LookupTable objects */
54 #define SCIM_DEBUG_SocketMask       512  /**< Show messages of Socket objects */
55 /**
56  * @}
57  */
58 
59 /**
60  * @name The macros to simplify the debug message print method.
61  *
62  * You can output debug messages by this way:
63  *   SCIM_DEBUG_IMENGINE(1) << "Hello World!\n";
64  *
65  * @{
66  */
67 #define SCIM_DEBUG(mask,level)        (scim::DebugOutput(mask,level) << scim::DebugOutput::serial_number () << __FILE__ << ":" << __LINE__ << " > ")
68 #define SCIM_DEBUG_MAIN(level)        SCIM_DEBUG(SCIM_DEBUG_MainMask,level)
69 #define SCIM_DEBUG_CONFIG(level)      SCIM_DEBUG(SCIM_DEBUG_ConfigMask,level)
70 #define SCIM_DEBUG_IMENGINE(level)    SCIM_DEBUG(SCIM_DEBUG_IMEngineMask,level)
71 #define SCIM_DEBUG_BACKEND(level)     SCIM_DEBUG(SCIM_DEBUG_BackEndMask,level)
72 #define SCIM_DEBUG_FRONTEND(level)    SCIM_DEBUG(SCIM_DEBUG_FrontEndMask,level)
73 #define SCIM_DEBUG_MODULE(level)      SCIM_DEBUG(SCIM_DEBUG_ModuleMask,level)
74 #define SCIM_DEBUG_UTILITY(level)     SCIM_DEBUG(SCIM_DEBUG_UtilityMask,level)
75 #define SCIM_DEBUG_ICONV(level)       SCIM_DEBUG(SCIM_DEBUG_IConvMask,level)
76 #define SCIM_DEBUG_LOOKUPTABLE(level) SCIM_DEBUG(SCIM_DEBUG_LookupTableMask,level)
77 #define SCIM_DEBUG_SOCKET(level)      SCIM_DEBUG(SCIM_DEBUG_SocketMask,level)
78 /**
79  * @}
80  */
81 
82 /**
83  * @brief The class to filter and redirect the debug messages.
84  */
85 class DebugOutput
86 {
87 private:
88     static uint32          current_verbose;
89     static uint32          current_mask;
90 
91     static uint32          verbose_level;
92     static uint32          output_mask;
93     static std::ostream   *output_stream;
94 
95 public:
96     /**
97      * @brief Constructor.
98      * @param mask - the debug filter mask.
99      * @param verbose - the verbose level of the debug message.
100      */
101     DebugOutput (uint32 mask = SCIM_DEBUG_AllMask, uint32 verbose = 1);
102 
103     /**
104      * @brief A template stream output operator.
105      *
106      * All kinds of data and variables can be output via DebugOutput by
107      * this operator.
108      */
109 #if ENABLE_DEBUG
110     template <typename T>
111     const DebugOutput& operator << (const T& t) const {
112         if (output_stream && (current_mask & output_mask) && (current_verbose <= verbose_level))
113             (*output_stream) << t;
114         return *this;
115     }
116 #else
117     template <typename T>
118     const DebugOutput& operator << (const T&) const {
119         return *this;
120     }
121 #endif
122 
123 public:
124     /**
125      * @brief The global method to enable the debug output.
126      * @param debug - the mask to indicate which kind of
127      *                debug should be enabled.
128      */
129     static void enable_debug (uint32 debug);
130 
131     /**
132      * @brief The global method to enable the debug output by their names.
133      * @param debug - the name of the debug type to be enabled. The valid
134      *                names are: all, main, config, imengine, backend, frontend,
135      *                module, utility, iconv, lookuptable, socket.
136      */
137     static void enable_debug_by_name (const String &debug);
138 
139     /**
140      * @brief Disable the debug type indicated by the given mask.
141      * @param debug - the mask of the debug type to be disabled.
142      */
143     static void disable_debug (uint32 debug);
144 
145     /**
146      * @brief Disable the debug type indicated by the given name.
147      * @param debug - the name of the debug type to be disabled.
148      */
149     static void disable_debug_by_name (const String &debug);
150 
151     /**
152      * @brief Set the debug verbose level.
153      * @param verbose - the debug verbose level, 0 means no debug output.
154      */
155     static void set_verbose_level (uint32 verbose);
156 
157     /**
158      * @brief Set the debug output file.
159      *
160      * @param file - the file to store the debug output.
161      *               If equal to "stderr" or "cerr" then the debug
162      *               output will be set to std:cerr.
163      *               If equal to "stdout" or "cout" then the debug
164      *               output will be set to std::cout.
165      */
166     static void set_output (const String &file);
167 
168     static String serial_number ();
169 };
170 
171 } // namespace scim
172 
173 #endif //__SCIM_DEBUG_H
174 /*
175 vi:ts=4:nowrap:ai:expandtab
176 */
177