From: "Karl Hasselström" <kha@treskal.com>
To: Catalin Marinas <catalin.marinas@gmail.com>
Cc: git@vger.kernel.org
Subject: [StGit PATCH 2/5] Log subproces activity to a file
Date: Thu, 17 Jul 2008 22:42:33 +0200 [thread overview]
Message-ID: <20080717204233.23407.59842.stgit@yoghurt> (raw)
In-Reply-To: <20080717204133.23407.34264.stgit@yoghurt>
If the user sets $STGIT_SUBPROCESS_LOG to a log mode followed by a
colon and a file name, append the log to that file instead of writing
it to stdout.
Signed-off-by: Karl Hasselström <kha@treskal.com>
---
stgit/out.py | 11 +++++++----
stgit/run.py | 35 +++++++++++++++++++++++------------
2 files changed, 30 insertions(+), 16 deletions(-)
diff --git a/stgit/out.py b/stgit/out.py
index 485b830..753c176 100644
--- a/stgit/out.py
+++ b/stgit/out.py
@@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import sys, textwrap
class MessagePrinter(object):
- def __init__(self):
+ def __init__(self, file = None):
class Output(object):
def __init__(self, write, flush):
self.write = write
@@ -68,9 +68,12 @@ class MessagePrinter(object):
self.new_line()
self.write(string)
self.at_start_of_line = string.endswith('\n')
- self.__stderr = Output(sys.stderr.write, sys.stderr.flush)
- self.__stdout = Output(sys.stdout.write, sys.stdout.flush)
- if sys.stdout.isatty():
+ if file:
+ self.__stdout = self.__stderr = Output(file.write, file.flush)
+ else:
+ self.__stdout = Output(sys.stdout.write, sys.stdout.flush)
+ self.__stderr = Output(sys.stdout.write, sys.stdout.flush)
+ if file or sys.stdout.isatty():
self.__out = self.__stdout
self.__err = self.__stdout
else:
diff --git a/stgit/run.py b/stgit/run.py
index 0b79729..9d50e43 100644
--- a/stgit/run.py
+++ b/stgit/run.py
@@ -27,12 +27,22 @@ class RunException(StgException):
subprocess."""
pass
-_all_log_modes = ['debug', 'profile']
-_log_mode = os.environ.get('STGIT_SUBPROCESS_LOG', '')
-if _log_mode and not _log_mode in _all_log_modes:
- out.warn(('Unknown log mode "%s" specified in $STGIT_SUBPROCESS_LOG.'
- % _log_mode),
- 'Valid values are: %s' % ', '.join(_all_log_modes))
+def get_log_mode(spec):
+ if not ':' in spec:
+ spec += ':'
+ (log_mode, outfile) = spec.split(':', 1)
+ all_log_modes = ['debug', 'profile']
+ if log_mode and not log_mode in all_log_modes:
+ out.warn(('Unknown log mode "%s" specified in $STGIT_SUBPROCESS_LOG.'
+ % log_mode),
+ 'Valid values are: %s' % ', '.join(all_log_modes))
+ if outfile:
+ f = MessagePrinter(open(outfile, 'a'))
+ else:
+ f = out
+ return (log_mode, f)
+
+(_log_mode, _logfile) = get_log_mode(os.environ.get('STGIT_SUBPROCESS_LOG', ''))
class Run:
exc = RunException
@@ -47,22 +57,23 @@ class Run:
self.__discard_stderr = False
def __log_start(self):
if _log_mode == 'debug':
- out.start('Running subprocess %s' % self.__cmd)
+ _logfile.start('Running subprocess %s' % self.__cmd)
if self.__cwd != None:
- out.info('cwd: %s' % self.__cwd)
+ _logfile.info('cwd: %s' % self.__cwd)
if self.__env != None:
for k in sorted(self.__env.iterkeys()):
if k not in os.environ or os.environ[k] != self.__env[k]:
- out.info('%s: %s' % (k, self.__env[k]))
+ _logfile.info('%s: %s' % (k, self.__env[k]))
elif _log_mode == 'profile':
- out.start('Running subprocess %s' % self.__cmd[0])
+ _logfile.start('Running subprocess %s' % self.__cmd[0])
self.__starttime = datetime.datetime.now()
def __log_end(self, retcode):
if _log_mode == 'debug':
- out.done('return code: %d' % retcode)
+ _logfile.done('return code: %d' % retcode)
elif _log_mode == 'profile':
duration = datetime.datetime.now() - self.__starttime
- out.done('%1.3f s' % (duration.microseconds/1e6 + duration.seconds))
+ _logfile.done('%1.3f s' % (duration.microseconds/1e6
+ + duration.seconds))
def __check_exitcode(self):
if self.__good_retvals == None:
return
next prev parent reply other threads:[~2008-07-17 20:44 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-07-17 20:42 [StGit PATCH 0/5] Performance testing tools Karl Hasselström
2008-07-17 20:42 ` [StGit PATCH 1/5] Add some performance testing scripts Karl Hasselström
2008-07-17 20:42 ` Karl Hasselström [this message]
2008-07-18 21:45 ` [StGit PATCH 2/5] Log subproces activity to a file Catalin Marinas
2008-07-17 20:42 ` [StGit PATCH 3/5] Show full command in subprocess profiling Karl Hasselström
2008-07-18 21:47 ` Catalin Marinas
2008-07-17 20:42 ` [StGit PATCH 4/5] Log subprocess calls during performance testing Karl Hasselström
2008-07-17 20:42 ` [StGit PATCH 5/5] Global performance logging Karl Hasselström
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080717204233.23407.59842.stgit@yoghurt \
--to=kha@treskal.com \
--cc=catalin.marinas@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).