ソースを参照

Reduce # of function calls in often-executed code.

Chris McDonough 18 年 前
コミット
79064e0f44

+ 9 - 4
src/supervisor/dispatchers.py

@@ -103,11 +103,14 @@ class POutputDispatcher(PDispatcher):
 
         self.childlog = self.mainlog
 
-        # this is purely for a minor speedup
+        # all code below is purely for minor speedups
         begintoken = self.event_type.BEGIN_TOKEN
         endtoken = self.event_type.END_TOKEN
         self.begintoken_data = (begintoken, len(begintoken))
         self.endtoken_data = (endtoken, len(endtoken))
+        self.mainlog_level = loggers.LevelsByName.DEBG
+        options_loglevel = self.process.config.options.loglevel 
+        self.log_to_mainlog = options_loglevel <= self.mainlog_level
 
     def removelogs(self):
         for log in (self.mainlog, self.capturelog):
@@ -129,9 +132,11 @@ class POutputDispatcher(PDispatcher):
                 data = config.options.stripEscapes(data)
             if self.childlog:
                 self.childlog.info(data)
-            msg = '%(name)r %(channel)s output:\n%(data)s'
-            self.process.config.options.logger.debug(
-                msg, name=config.name, channel=self.channel, data=data)
+            if self.log_to_mainlog:
+                msg = '%(name)r %(channel)s output:\n%(data)s'
+                config.options.logger.log(
+                    self.mainlog_level, msg, name=config.name,
+                    channel=self.channel, data=data)
 
     def record_output(self):
         if self.capturelog is None:

+ 6 - 9
src/supervisor/loggers.py

@@ -243,32 +243,29 @@ class Logger:
 
     def trace(self, msg, **kw):
         if LevelsByName.TRAC >= self.level:
-            self._log(LevelsByName.TRAC, msg, **kw)
+            self.log(LevelsByName.TRAC, msg, **kw)
     
     def debug(self, msg, **kw):
         if LevelsByName.DEBG >= self.level:
-            self._log(LevelsByName.DEBG, msg, **kw)
+            self.log(LevelsByName.DEBG, msg, **kw)
     
     def info(self, msg, **kw):
         if LevelsByName.INFO >= self.level:
-            self._log(LevelsByName.INFO, msg, **kw)
+            self.log(LevelsByName.INFO, msg, **kw)
 
     def warn(self, msg, **kw):
         if LevelsByName.WARN >= self.level:
-            self._log(LevelsByName.WARN, msg, **kw)
+            self.log(LevelsByName.WARN, msg, **kw)
 
     def error(self, msg, **kw):
         if LevelsByName.ERRO >= self.level:
-            self._log(LevelsByName.ERRO, msg, **kw)
+            self.log(LevelsByName.ERRO, msg, **kw)
 
     def critical(self, msg, **kw):
         if LevelsByName.CRIT >= self.level:
-            self._log(LevelsByName.CRIT, msg, **kw)
+            self.log(LevelsByName.CRIT, msg, **kw)
 
     def log(self, level, msg, **kw):
-        self._log(level, msg, **kw)
-
-    def _log(self, level, msg, **kw):
         record = LogRecord(level, msg, **kw)
         for handler in self.handlers:
             if level >= handler.level:

+ 8 - 1
src/supervisor/tests/base.py

@@ -8,6 +8,7 @@ class DummyOptions:
     execv_error = None
     kill_error = None
     minfds = 5
+    loglevel = 20
 
     def __init__(self):
         self.identifier = 'supervisor'
@@ -242,7 +243,13 @@ class DummyLogger:
         if kw:
             msg = msg % kw
         self.data.append(msg)
-    warn = log = debug = critical = trace = error = info
+    warn = debug = critical = trace = error = info
+
+    def log(self, level, msg, **kw):
+        if kw:
+            msg = msg % kw
+        self.data.append(msg)
+        
     def reopen(self):
         self.reopened = True
     def close(self):

+ 4 - 0
src/supervisor/tests/test_dispatchers.py

@@ -129,6 +129,8 @@ class POutputDispatcherTests(unittest.TestCase):
         # stdout/stderr goes to the process log and the main log,
         # in non-capturemode, the data length doesn't matter
         options = DummyOptions()
+        from supervisor import loggers
+        options.loglevel = loggers.LevelsByName.TRAC
         config = DummyPConfig(options, 'process1', '/bin/process1',
                               stdout_logfile='/tmp/foo')
         process = DummyProcess(config)
@@ -145,6 +147,8 @@ class POutputDispatcherTests(unittest.TestCase):
         # in capturemode, the length of the data needs to be longer
         # than the capture token to make it out.
         options = DummyOptions()
+        from supervisor import loggers
+        options.loglevel = loggers.LevelsByName.TRAC
         config = DummyPConfig(options, 'process1', '/bin/process1',
                               stdout_logfile='/tmp/foo',
                               stdout_capture_maxbytes=100)