def reinit_comlog( loggername, loglevel, logfile, logtype, maxlogsize, bprint_console ): # too many arg pylint: disable=R0913 """ 重新设置comlog, 参与意义同init_comlog. reinit_comlog会重新设置整个进程的参数, 但请注意loggername一定不能与 原来的loggername相同,否则可能出现打印两次的情况 """ loggerman = _LoggerMan() loggerman._reset_logger(logging.getLogger(loggername)) if os.path.exists(logfile) is False: if platforms.is_linux(): os.mknod(logfile) else: with open(logfile, 'w+') as fhandle: fhandle.write('----Windows File Creation ----\n') elif os.path.isfile(logfile) is False: raise err.LoggerException( 'The log file exists. But it\'s not regular file' ) loggerman._config_filelogger( loglevel, logfile, logtype, maxlogsize, bprint_console ) # too many arg pylint: disable=w0212 info('-' * 20 + 'Log Reinitialized Successfully' + '-' * 20) return
def reinit_comlog( loggername, loglevel, logfile, logtype, maxlogsize, bprint_console, gen_wf=False ): # too many arg pylint: disable=R0913 """ 重新设置comlog, 参与意义同init_comlog. reinit_comlog会重新设置整个进程的参数, 但请注意loggername一定不能与 原来的loggername相同,相同的loggername会raise ValueError """ global G_INITED_LOGGER if loggername in G_INITED_LOGGER: msg = 'loggername:%s has been already initalized!!!' % loggername raise ValueError(msg) G_INITED_LOGGER.append(loggername) loggerman = _LoggerMan() loggerman._reset_logger(logging.getLogger(loggername)) if os.path.exists(logfile) is False: if platforms.is_linux(): os.mknod(logfile) else: with open(logfile, 'w+') as fhandle: fhandle.write('----Windows File Creation ----\n') elif os.path.isfile(logfile) is False: raise err.LoggerException( 'The log file exists. But it\'s not regular file' ) loggerman._config_filelogger( loglevel, logfile, logtype, maxlogsize, bprint_console, gen_wf ) # too many arg pylint: disable=w0212 info('-' * 20 + 'Log Reinitialized Successfully' + '-' * 20) return
def getip_byinterface(iface='eth0'): """ @platform: Linux/Unix 获得某个网卡的ip地址 E.g. :: import cup print cup.net.getip_byinterface('eth0') print cup.net.getip_byinterface('eth1') print cup.net.getip_byinterface('xgbe0') # 万兆网卡 """ if platforms.is_linux(): ifreq = struct.pack('16sH14s', iface, socket.AF_INET, '\x00' * 14) try: res = fcntl.ioctl(_SOCKFD, _SIOCGIFADDR, ifreq) except Exception as error: # pylint: disable=W0703,W0612 return None ipaddr = struct.unpack('16sH2x4s8x', res)[2] return socket.inet_ntoa(ipaddr) else: raise NotImplementedError('Not implemented on this platform')
def init_comlog( loggername, loglevel=logging.INFO, logfile='cup.log', logtype=ROTATION, maxlogsize=1073741824, bprint_console=False, gen_wf=False ): # too many arg pylint: disable=R0913 """ 初始化日志函数。用法如下:: :param loggername: 这个logger的名字. :param loglevel: 一共四种 log.DEBUG log.INFO log.ERROR log.CRITICAL :param logfile: log文件的位置,如不存在,会尝试创建该文件 :param logtype: 支持两种leo.log.ROTATION cup.log.INFINITE ROTATION会在文件大小达到maxlogsize的时候进行switch. 一共会switch 30个文件, 然后在这30个文件里面ROTATION的写 INFINITE会一直写log文件 :param maxlogsize: logfile的最大文件大小(单位byte).超过会进行覆盖写或者switch. :param b_printcmd: 打印日志到logfile的同时,是否打印到stdout. :param gen_wf: 将级别大于等于WARNING的消息打印到${logfile}.wf中. 请注意,打印日志时要么打印unicode字符,要么打印Python默认的UTF8的字符 *E.g.* :: import logging from cup import log log.init_comlog( 'test', log.DEBUG, '/home/work/test/test.log', log.ROTATION, 1024, False ) log.info('test xxx') log.critical('test critical') """ loggerman = _LoggerMan() if loggerman.is_initalized() is False: loggerman._setlogger(logging.getLogger(loggername)) if os.path.exists(logfile) is False: if platforms.is_linux(): os.mknod(logfile) else: with open(logfile, 'w+') as fhandle: fhandle.write('----Windows File Creation ----\n') elif os.path.isfile(logfile) is False: raise err.LoggerException( 'The log file exists. But it\'s not regular file' ) loggerman._config_filelogger( loglevel, logfile, logtype, maxlogsize, bprint_console, gen_wf ) # too many arg pylint: disable=w0212 info('-' * 20 + 'Log Initialized Successfully' + '-' * 20) global G_INITED_LOGGER G_INITED_LOGGER.append(loggername) else: print '[%s:%s] init_comlog has been already initalized' % \ (_file(1), _line(1)) return
__all__ = [ 'get_local_hostname', 'get_hostip', 'getip_byinterface', 'set_sock_keepalive_linux', 'set_sock_reusable', 'set_sock_linger', 'set_sock_quickack', 'async', 'localport_free', 'port_listened' ] if platforms.is_linux(): _SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM) _SOCKFD = _SOCK.fileno() _SIOCGIFADDR = 0x8915 def getip_byinterface(iface='eth0'): """ @platform: Linux/Unix 获得某个网卡的ip地址 E.g. :: import cup print cup.net.getip_byinterface('eth0') print cup.net.getip_byinterface('eth1')
def init_comlog(loggername, loglevel=logging.INFO, logfile='cup.log', logtype=ROTATION, maxlogsize=1073741824, bprint_console=False, gen_wf=False): # too many arg pylint: disable=R0913 """ 初始化日志函数 :param loggername: 这个logger的名字 :param loglevel: 一共四种 log.DEBUG log.INFO log.ERROR log.CRITICAL :param logfile: log文件的位置,如不存在,会尝试创建该文件 :param logtype: 支持两种cup.log.ROTATION cup.log.INFINITE ROTATION会在文件大小达到maxlogsize的时候进行switch. 一共会switch 30个文件, 然后在这30个文件里面ROTATION的写 INFINITE会一直写log文件 :param maxlogsize: logfile的最大文件大小(单位byte).超过会进行覆盖写或者switch. :param b_printcmd: 打印日志到logfile的同时,是否打印到stdout. :param gen_wf: 将级别大于等于WARNING的消息打印到${logfile}.wf中. 请注意,打印日志时要么打印unicode字符,要么打印Python默认的UTF8的字符 *E.g.* :: import logging from cup import log log.init_comlog( 'test', log.DEBUG, '/home/work/test/test.log', log.ROTATION, 1024, False ) log.info('test xxx') log.critical('test critical') """ loggerman = _LoggerMan() if loggerman.is_initalized() is False: # loggerman._setlogger(logging.getLogger(loggername)) loggerman._setlogger(logging.getLogger()) if os.path.exists(logfile) is False: if platforms.is_linux(): os.mknod(logfile) else: with open(logfile, 'w+') as fhandle: fhandle.write('----Windows File Creation ----\n') elif os.path.isfile(logfile) is False: raise err.LoggerException( 'The log file exists. But it\'s not regular file') loggerman._config_filelogger(loglevel, logfile, logtype, maxlogsize, bprint_console, gen_wf) # too many arg pylint: disable=w0212 info('-' * 20 + 'Log Initialized Successfully' + '-' * 20) global G_INITED_LOGGER G_INITED_LOGGER.append(loggername) else: print '[%s:%s] init_comlog has been already initalized' % \ (_file(1), _line(1)) return
import copy import shutil from cup import err from cup import decorators from cup import platforms __all__ = [ 'LockFile', 'FILELOCK_SHARED', 'FILELOCK_EXCLUSIVE', 'FILELOCK_NONBLOCKING', 'FILELOCK_UNLOCK', 'mk_newnode', 'safe_rmtree', 'safe_delete' ] CANNOT_DEL_PATHLIST = ['/', '/proc', '/boot', '/sys'] if platforms.is_linux() or platforms.is_mac(): import fcntl FILELOCK_EXCLUSIVE = fcntl.LOCK_EX FILELOCK_SHARED = fcntl.LOCK_SH FILELOCK_NONBLOCKING = fcntl.LOCK_NB FILELOCK_UNLOCK = fcntl.LOCK_UN elif platforms.is_windows(): import msvcrt def file_size(fobj): """win file size""" return os.path.getsize(os.path.realpath(fobj.name)) def win_lockfile(fobj, blocking=True): """win lock file""" flags = msvcrt.LK_RLCK
import fcntl except ImportError as error: # 'Seems run on non-linux machine' pass from cup.net import async from cup import log from cup import platforms __all__ = [ 'get_local_hostname', 'get_hostip', 'getip_byinterface', 'set_sock_keepalive_linux', 'set_sock_reusable', 'set_sock_linger', 'set_sock_quickack', 'async', 'localport_free', 'port_listened' ] if platforms.is_linux(): _SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM) _SOCKFD = _SOCK.fileno() _SIOCGIFADDR = 0x8915 def getip_byinterface(iface='eth0'): """ get ipaddr of a network adapter :platform: Linux/Unix E.g. :: import cup
def init_comlog(loggername, loglevel=logging.INFO, logfile='cup.log', logtype=ROTATION, maxlogsize=1073741824, bprint_console=False, gen_wf=False): """ Initialize your default logger :param loggername: Unique logger name for default logging. :param loglevel: 4 default levels: log.DEBUG log.INFO log.ERROR log.CRITICAL :param logfile: log file. Will try to create it if no existence :param logtype: Two type candidiates: log.ROTATION and log.INFINITE log.ROTATION will let logfile switch to a new one (30 files at most). When logger reaches the 30th logfile, will overwrite from the oldest to the most recent. log.INFINITE will write on the logfile infinitely :param maxlogsize: maxmum log size with byte :param b_printcmd: print to stdout or not? :param gen_wf: print log msges with level >= WARNING to file (${logfile}.wf) *E.g.* :: import logging from cup import log log.init_comlog( 'test', log.DEBUG, '/home/work/test/test.log', log.ROTATION, 1024, False ) log.info('test xxx') log.critical('test critical') """ loggerman = _RootLogerMan() rootloger = logging.getLogger() if not loggerman.is_initalized(): loggerman.set_rootlogger(loggername, rootloger) if os.path.exists(logfile) is False: if platforms.is_linux(): os.mknod(logfile) else: with open(logfile, 'w+') as fhandle: fhandle.write('\n') elif os.path.isfile(logfile) is False: raise err.LoggerException( 'The log file exists. But it\'s not regular file') loggerparams = LoggerParams(loglevel, logfile, logtype, maxlogsize, bprint_console, gen_wf) LogInitializer.setup_filelogger(rootloger, loggerparams) info('-' * 20 + 'Log Initialized Successfully' + '-' * 20) global G_INITED_LOGGER G_INITED_LOGGER.append(loggername) else: print('[{0}:{1}] init_comlog has been already initalized'.format( LogInitializer.get_codefile(1), LogInitializer.get_codeline(1))) return