1 /*
2  * Smart Common Input Method
3  *
4  * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
5  *
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA  02111-1307  USA
21  *
22  * $Id: scim_debug.cpp,v 1.10 2005/08/05 01:54:24 suzhe Exp $
23  *
24  */
25 
26 #define Uses_SCIM_DEBUG
27 #include "scim_private.h"
28 #include "scim.h"
29 #include <cstdio>
30 
31 namespace scim {
32 
33 struct _DebugMaskName
34 {
35     uint32 mask;
36     const char  *name;
37 };
38 
39 static _DebugMaskName _debug_mask_names [] =
40 {
41     {SCIM_DEBUG_AllMask,        "all"},
42     {SCIM_DEBUG_MainMask,       "main"},
43     {SCIM_DEBUG_ConfigMask,     "config"},
44     {SCIM_DEBUG_IMEngineMask,   "imengine"},
45     {SCIM_DEBUG_BackEndMask,    "backend"},
46     {SCIM_DEBUG_FrontEndMask,   "frontend"},
47     {SCIM_DEBUG_ModuleMask,     "module"},
48     {SCIM_DEBUG_UtilityMask,    "utility"},
49     {SCIM_DEBUG_IConvMask,      "iconv"},
50     {SCIM_DEBUG_LookupTableMask,"lookuptable"},
51     {SCIM_DEBUG_SocketMask,     "socket"},
52     {0, 0}
53 };
54 
55 uint32         DebugOutput::current_verbose = 0;
56 uint32         DebugOutput::current_mask = 0;
57 uint32         DebugOutput::verbose_level = 0;
58 uint32         DebugOutput::output_mask = ~0;
59 std::ostream * DebugOutput::output_stream = &std::cerr;
60 
61 static std::ofstream __debug_output_file;
62 
63 #if ENABLE_DEBUG
DebugOutput(uint32 mask,uint32 verbose)64 DebugOutput::DebugOutput (uint32 mask, uint32 verbose)
65 {
66     current_mask    = mask;
67     current_verbose = verbose;
68 }
69 #else
DebugOutput(uint32 mask,uint32 verbose)70 DebugOutput::DebugOutput (uint32 mask, uint32 verbose)
71 {
72 }
73 #endif
74 
75 void
set_verbose_level(uint32 verbose)76 DebugOutput::set_verbose_level (uint32 verbose)
77 {
78     verbose_level =
79         (verbose > SCIM_DEBUG_MAX_VERBOSE) ? SCIM_DEBUG_MAX_VERBOSE : verbose;
80 }
81 
82 void
enable_debug(uint32 debug)83 DebugOutput::enable_debug (uint32 debug)
84 {
85     output_mask |= debug;
86 }
87 
88 void
enable_debug_by_name(const String & debug)89 DebugOutput::enable_debug_by_name (const String &debug)
90 {
91     _DebugMaskName *p = _debug_mask_names;
92     while (p->mask && p->name) {
93         if (String (p->name) == debug) {
94             output_mask |= p->mask;
95             return;
96         }
97         ++ p;
98     }
99 }
100 
101 void
disable_debug(uint32 debug)102 DebugOutput::disable_debug (uint32 debug)
103 {
104     output_mask &= (~debug);
105 }
106 
107 void
disable_debug_by_name(const String & debug)108 DebugOutput::disable_debug_by_name (const String &debug)
109 {
110     _DebugMaskName *p = _debug_mask_names;
111     while (p->mask && p->name) {
112         if (String (p->name) == debug) {
113             output_mask &= (~(p->mask));
114             return;
115         }
116         ++ p;
117     }
118 }
119 
120 void
set_output(const String & file)121 DebugOutput::set_output (const String &file)
122 {
123     DebugOutput::output_stream = &std::cerr;
124 
125     if (file.length ()) {
126         if (file == String ("stderr") || file == String ("cerr"))
127             DebugOutput::output_stream = &std::cerr;
128         else if (file == String ("stdout") || file == String ("cout"))
129             DebugOutput::output_stream = &std::cout;
130         else if (file == String ("none") || file == String ("off"))
131             DebugOutput::output_stream = 0;
132         else {
133             __debug_output_file.open (file.c_str ());
134             if (__debug_output_file.is_open ())
135                 DebugOutput::output_stream = &__debug_output_file;
136         }
137     }
138 }
139 
140 String
serial_number()141 DebugOutput::serial_number ()
142 {
143     static unsigned int serial = 0;
144     char buf [40];
145     snprintf (buf, 40, "<%08u>:", serial ++);
146     return String (buf);
147 }
148 
149 } // namespace scim
150 
151 /*
152 vi:ts=4:nowrap:ai:expandtab
153 */
154