1
2
3
4
5
6
7
8
9
10
11
12
13
14 """
15 IO functions.
16 """
17
18
19
20
21 import sys
22 import codecs
23 import locale
24 import traceback
25 import os
26 import threading
27
28 import cfg
29
30
31
32
33
34
35 -def z_u(a, enc=None):
36 """
37 Convert L{a} to unicode if C{a} is not unicode.
38 Use default locale encoding if not given L{enc}.
39 """
40 if not enc:
41 enc = cfg.z_epdl
42 if type(a) == str:
43 a = unicode(a, enc, u'replace')
44 elif type(a) != unicode:
45 try:
46 a = unicode(a)
47 except Exception,exc:
48 try:
49 a = unicode(str(a), enc, u'replace')
50 except:
51 a = u'ERROR then convert to unicode in z_u().'
52 return a
53
55 """
56 Print all concatenated L{z_u}(arg) if L{cfg.PYBAG_LOG} True.
57
58 @param arg: Object to output. All objects convert to string I{str()} function.
59 @type arg: *object
60 @return: None.
61 @rtype: None
62 """
63 if cfg.PYBAG_LOG or cfg.PYBAG_DEBUG:
64 log_writeln(u'# ', *arg)
65
67 """
68 Print all concatenated L{z_u}(arg) if L{cfg.PYBAG_DEBUG} True.
69
70 @param arg: Object to output. All objects convert to string I{str()} function.
71 @type arg: *object
72 @return: None.
73 @rtype: None
74 """
75 if cfg.PYBAG_DEBUG:
76 log_writeln(u'-->', *arg)
77
79 """
80 Used only in first call, and replace themself by L{log_writeln_1} if
81 L{log_init} pass, or L{log_writeln_0} otherwise.
82 """
83 _module = sys.modules[__name__]
84 try:
85 log_init()
86 _module.log_writeln = log_writeln_1
87 except:
88 _module.log_writeln = log_writeln_0
89 _module.log_writeln(*arg)
90
91 _loglock = threading.Lock()
92 """
93 Lock object for threading output.
94 """
95
97 """
98 Main writeln function for logging.
99 Write concatenated arguments into outstream and append new line ('\\n').
100
101 @param arg: Object to output. All objects convert to string L{z_u()} function.
102 @type arg: *object
103 @return: None.
104 @rtype: None
105 """
106 _u_ = z_u
107 def red(a,b):
108 return a + _u_(b)
109 try:
110
111 _loglock.acquire()
112 cfg.fstream.write( reduce(red, arg, u'') )
113 cfg.fstream.write(u'\n' )
114 except Exception, exc:
115
116 pass
117 finally:
118 _loglock.release()
119
121 """
122 Used if L{log_init} was fail in L{log_writeln} and do nothing.
123 """
124 pass
125
127 """
128 Initialize file output stream.
129
130 Default is "pybag.log".
131
132 """
133 from cli import dir_root
134
135 if not cfg.fstream:
136 try:
137 p=os.path.join(dir_root, u'pybag.log')
138 cfg.fstream=codecs.open(p,'w', 'utf_8', 'strict', 0)
139
140 except Exception, e:
141 cfg.fstream=sys.stderr
142 err=u'Can\'t open file for debug logging.\nerror:' + \
143 unicode(e) + u'\n\n'
144
145 cfg.fstream.write(err)
146
154
155 -def prt(*arg, **kwd):
156 """
157 Print all C{arg} consecutively with C{print} builtin function.
158 Also L{log} all output.
159
160 @keyword verbose: Verbosity level. {default VERBOSE == 3}.
161 @keyword place: Function name from called print.
162 @keyword error: Error description or exception.
163 """
164 _u_ = z_u
165 def red(a,b):
166 return a + _u_(b)
167 try:
168 verb = cfg.VERBOSE
169 if kwd.has_key('verbose'):
170 verb = kwd['verbose']
171 s = reduce(red, arg, u'')
172 sl = None
173 if kwd.has_key('error'):
174 tbs = traceback.format_exc()
175 s += u'\n\tERROR: %s' % (_u_(kwd['error']),)
176 sl = u'\n\tTRACEBACK: %s\n' % (_u_(tbs),)
177 se = s.encode(cfg.z_outep, u'strict')
178 if kwd.has_key('place'):
179 log(u'%d>%s: ' % (verb,kwd['place']), s)
180 else:
181 log(u'%d>' % (verb,), s)
182 if sl:
183 log(sl)
184 if verb <= cfg.VERBOSE:
185 print se
186 except:
187 pass
188
189 -def prt0(*arg, **kwd):
190 """
191 Print arguments L{arg} with L{prt} with C{verbose = 0}.
192 """
193 kwd.update({'verbose':0})
194 prt(*arg, **kwd)
195
196 -def prt1(*arg, **kwd):
197 """
198 Print arguments L{arg} with L{prt} with C{verbose = 1}.
199 """
200 kwd.update({'verbose':1})
201 prt(*arg, **kwd)
202
203 -def prt2(*arg, **kwd):
204 """
205 Print arguments L{arg} with L{prt} with C{verbose = 2}.
206 """
207 kwd.update({'verbose':2})
208 prt(*arg, **kwd)
209
210 -def prt3(*arg, **kwd):
211 """
212 Print arguments L{arg} with L{prt} with C{verbose = 3}.
213 """
214 kwd.update({'verbose':3})
215 prt(*arg, **kwd)
216
217 -def prt4(*arg, **kwd):
218 """
219 Print arguments L{arg} with L{prt} with C{verbose = 4}.
220 """
221 kwd.update({'verbose':4})
222 prt(*arg, **kwd)
223
224 -def prt5(*arg, **kwd):
225 """
226 Print arguments L{arg} with L{prt} with C{verbose = 5}.
227 """
228 kwd.update({'verbose':5})
229 prt(*arg, **kwd)
230
231
268
269
270
271
272
273
274
275
276