- 论坛徽章:
- 1
|
刚学python,写了一个输出日志的py
#!/usr/bin/python
import time
DEBUG = True
if DEBUG:
def debug(*msgs):
print time.strftime("%Y/%m/%d %H:%M:%S"),
for msg in msgs:
print msg,
print
else:
def debug(*msgs):
pass
|
这样在其他py脚本里 import 这个文件,就能用debug("......")输出日志。
而在分发时,可以修改“DEBUG=False”关掉日志的输出。
现在的问题是,关掉日志输出后,还会产生一个函数调用的开销。
在C里面,用宏的话:
#ifdef DEBUG
#define debug(msg) ....
#else
#define debug(msg)
#endif |
是不会有任何开销产生的。
有没有办法做到? |
|