[PATCH 6 of 8 v2] blackbox: avoid creating multiple file handles for a single log

Yuya Nishihara yuya at tcha.org
Tue Feb 9 08:17:26 EST 2016


On Mon, 08 Feb 2016 00:15:08 -0600, timeless wrote:
> # HG changeset patch
> # User timeless <timeless at mozdev.org>
> # Date 1454514091 0
> #      Wed Feb 03 15:41:31 2016 +0000
> # Node ID c10b56801049483531e531f0f4d2533fb987f8b3
> # Parent  99aa1e5f3b7e24afe97954d95a5107858888240e
> blackbox: avoid creating multiple file handles for a single log
> 
> There are multiple ui objects in Mercurial that can relate to a repository,
> before this change, each one would have its own file pointer, which
> results in unfortunate logging behavior.
> 
> Also, any log rotation results would be bad because only the
> active blackboxui object's file pointer would be refreshed.

Ok, I got why ui.copy() wouldn't work.

A possible workaround is to share a cell across ui objects.

  def __init__(self, src=None):
      if src:
          self._blackboxcell = getattr(src, '_blackboxcell', [])
      else:
          self._blackboxcell = []

  def _openlogfile(self):
      if self._blackboxcell:
          return self._blackboxcell[0]
      else:
          fp = self._bbvfs('blackbox.log', 'a')
          self._blackboxcell.append(fp)
          return fp

I agree this is also tricky, but I heard you are going to override ui.copy()
anyway, so it might be worth trying. Please let me know if you hate it or not.

> diff --git a/hgext/blackbox.py b/hgext/blackbox.py
> --- a/hgext/blackbox.py
> +++ b/hgext/blackbox.py
> @@ -42,6 +42,22 @@
>  testedwith = 'internal'
>  lastblackbox = None
>  
> +filehandles = {}
> +
> +def _openlog(vfs):
> +    path = vfs.join('blackbox.log')
> +    if path in filehandles:
> +        return filehandles[path]
> +    filehandles[path] = fp = vfs('blackbox.log', 'a')
> +    filehandles[fp] = path
> +    return fp
> +
> +def _closelog(fp):
> +    path = filehandles[fp]
> +    del filehandles[fp]
> +    del filehandles[path]

Nitpick: you can pass vfs to generate path, which will avoid double uses of
filehandles dict.


More information about the Mercurial-devel mailing list