45 # Note for extension authors: ONLY specify testedwith = 'internal' for |
45 # Note for extension authors: ONLY specify testedwith = 'internal' for |
46 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
46 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
47 # be specifying the version(s) of Mercurial they are tested with, or |
47 # be specifying the version(s) of Mercurial they are tested with, or |
48 # leave the attribute unspecified. |
48 # leave the attribute unspecified. |
49 testedwith = 'internal' |
49 testedwith = 'internal' |
50 lastblackbox = None |
50 lastfp = None |
51 |
51 |
52 filehandles = {} |
52 filehandles = {} |
53 |
53 |
54 def _openlog(vfs): |
54 def _openlog(vfs): |
55 path = vfs.join('blackbox.log') |
55 path = vfs.join('blackbox.log') |
101 newpath=maxfiles > 0 and path + '.1') |
101 newpath=maxfiles > 0 and path + '.1') |
102 fp = _openlog(self._bbvfs) |
102 fp = _openlog(self._bbvfs) |
103 return fp |
103 return fp |
104 |
104 |
105 def log(self, event, *msg, **opts): |
105 def log(self, event, *msg, **opts): |
106 global lastblackbox |
106 global lastfp |
107 super(blackboxui, self).log(event, *msg, **opts) |
107 super(blackboxui, self).log(event, *msg, **opts) |
108 |
108 |
109 if not '*' in self.track and not event in self.track: |
109 if not '*' in self.track and not event in self.track: |
110 return |
110 return |
111 |
111 |
112 if util.safehasattr(self, '_blackbox'): |
112 if util.safehasattr(self, '_blackbox'): |
113 blackbox = self._blackbox |
113 fp = self._blackbox |
114 elif util.safehasattr(self, '_bbvfs'): |
114 elif util.safehasattr(self, '_bbvfs'): |
115 try: |
115 try: |
116 self._blackbox = self._openlogfile() |
116 self._bbfp = self._openlogfile() |
117 except (IOError, OSError) as err: |
117 except (IOError, OSError) as err: |
118 self.debug('warning: cannot write to blackbox.log: %s\n' % |
118 self.debug('warning: cannot write to blackbox.log: %s\n' % |
119 err.strerror) |
119 err.strerror) |
120 del self._bbvfs |
120 del self._bbvfs |
121 self._blackbox = None |
121 self._bbfp = None |
122 blackbox = self._blackbox |
122 fp = self._bbfp |
123 else: |
123 else: |
124 # certain ui instances exist outside the context of |
124 # certain ui instances exist outside the context of |
125 # a repo, so just default to the last blackbox that |
125 # a repo, so just default to the last blackbox that |
126 # was seen. |
126 # was seen. |
127 blackbox = lastblackbox |
127 fp = lastfp |
128 |
128 |
129 if blackbox: |
129 if fp: |
130 date = util.datestr(None, '%Y/%m/%d %H:%M:%S') |
130 date = util.datestr(None, '%Y/%m/%d %H:%M:%S') |
131 user = util.getuser() |
131 user = util.getuser() |
132 pid = str(util.getpid()) |
132 pid = str(util.getpid()) |
133 formattedmsg = msg[0] % msg[1:] |
133 formattedmsg = msg[0] % msg[1:] |
134 try: |
134 try: |
135 blackbox.write('%s %s (%s)> %s' % |
135 fp.write('%s %s (%s)> %s' % |
136 (date, user, pid, formattedmsg)) |
136 (date, user, pid, formattedmsg)) |
137 blackbox.flush() |
137 fp.flush() |
138 except IOError as err: |
138 except IOError as err: |
139 self.debug('warning: cannot write to blackbox.log: %s\n' % |
139 self.debug('warning: cannot write to blackbox.log: %s\n' % |
140 err.strerror) |
140 err.strerror) |
141 lastblackbox = blackbox |
141 lastfp = fp |
142 |
142 |
143 def setrepo(self, repo): |
143 def setrepo(self, repo): |
144 self._bbvfs = repo.vfs |
144 self._bbvfs = repo.vfs |
145 |
145 |
146 ui.__class__ = blackboxui |
146 ui.__class__ = blackboxui |