<s> import os <EOL> import time <EOL> from sys import stdout , stderr <EOL> from zope . interface import implements <EOL> from twisted . python . log import startLoggingWithObserver , textFromEventDict , msg , err , ILogObserver <EOL> from twisted . python . syslog import SyslogObserver <EOL> from twisted . python . logfile import DailyLogFile <EOL> class CarbonLogFile ( DailyLogFile ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> DailyLogFile . __init__ ( self , * args , ** kwargs ) <EOL> from carbon . conf import settings <EOL> self . enableRotation = settings . ENABLE_LOGROTATION <EOL> def _openFile ( self ) : <EOL> """<STR_LIT>""" <EOL> openMode = self . defaultMode or <NUM_LIT:0> <NUM_LIT> <EOL> self . _file = os . fdopen ( os . open ( <EOL> self . path , os . O_CREAT | os . O_RDWR , openMode ) , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . closed = False <EOL> if self . defaultMode : <EOL> try : <EOL> os . chmod ( self . path , self . defaultMode ) <EOL> except OSError : <EOL> pass <EOL> self . _file . seek ( <NUM_LIT:0> , os . SEEK_END ) <EOL> self . lastDate = self . toDate ( os . stat ( self . path ) [ <NUM_LIT:8> ] ) <EOL> def shouldRotate ( self ) : <EOL> if self . enableRotation : <EOL> return DailyLogFile . shouldRotate ( self ) <EOL> else : <EOL> return False <EOL> def write ( self , data ) : <EOL> if not self . enableRotation : <EOL> if not os . path . exists ( self . path ) : <EOL> self . reopen ( ) <EOL> else : <EOL> path_stat = os . stat ( self . path ) <EOL> fd_stat = os . fstat ( self . _file . fileno ( ) ) <EOL> if not ( path_stat . st_ino == fd_stat . st_ino <EOL> and path_stat . st_dev == fd_stat . st_dev ) : <EOL> self . reopen ( ) <EOL> DailyLogFile . write ( self , data ) <EOL> def reopen ( self ) : <EOL> self . close ( ) <EOL> self . _openFile ( ) <EOL> class CarbonLogObserver ( object ) : <EOL> implements ( ILogObserver ) <EOL> def log_to_dir ( self , logdir ) : <EOL> self . logdir = logdir <EOL> self . console_logfile = CarbonLogFile ( '<STR_LIT>' , logdir ) <EOL> self . custom_logs = { } <EOL> self . observer = self . logdir_observer <EOL> def log_to_syslog ( self , prefix ) : <EOL> observer = SyslogObserver ( prefix ) . emit <EOL> def syslog_observer ( event ) : <EOL> event [ "<STR_LIT>" ] = event . get ( "<STR_LIT:type>" , "<STR_LIT>" ) <EOL> observer ( event ) <EOL> self . observer = syslog_observer <EOL> def __call__ ( self , event ) : <EOL> return self . observer ( event ) <EOL> def stdout_observer ( self , event ) : <EOL> stdout . write ( formatEvent ( event , includeType = True ) + '<STR_LIT:\n>' ) <EOL> stdout . flush ( ) <EOL> def logdir_observer ( self , event ) : <EOL> message = formatEvent ( event ) <EOL> log_type = event . get ( '<STR_LIT:type>' ) <EOL> if log_type is not None and log_type not in self . custom_logs : <EOL> self . custom_logs [ log_type ] = CarbonLogFile ( log_type + '<STR_LIT>' , self . logdir ) <EOL> logfile = self . custom_logs . get ( log_type , self . console_logfile ) <EOL> logfile . write ( message + '<STR_LIT:\n>' ) <EOL> logfile . flush ( ) <EOL> observer = stdout_observer <EOL> carbonLogObserver = CarbonLogObserver ( ) <EOL> def formatEvent ( event , includeType = False ) : <EOL> event [ '<STR_LIT>' ] = '<STR_LIT>' in event <EOL> message = textFromEventDict ( event ) <EOL> if includeType : <EOL> typeTag = '<STR_LIT>' % event . get ( '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> else : <EOL> typeTag = '<STR_LIT>' <EOL> timestamp = time . strftime ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" % ( timestamp , typeTag , message ) <EOL> logToDir = carbonLogObserver . log_to_dir <EOL> logToSyslog = carbonLogObserver . log_to_syslog <EOL> def logToStdout ( ) : <EOL> startLoggingWithObserver ( carbonLogObserver ) <EOL> def cache ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def clients ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def creates ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def updates ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def listener ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def relay ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def aggregator ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def query ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def debug ( message , ** context ) : <EOL> if debugEnabled : <EOL> msg ( message , ** context ) <EOL> debugEnabled = False <EOL> def setDebugEnabled ( enabled ) : <EOL> global debugEnabled <EOL> debugEnabled = enabled </s>
