Skip to content

Commit

Permalink
logging: use predictable log file names
Browse files Browse the repository at this point in the history
  • Loading branch information
bfredl committed Sep 23, 2016
1 parent b8bb7c2 commit cb23953
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ You can run the plugin host in nvim with logging enabled to debug errors:
NVIM_PYTHON_LOG_FILE=logfile NVIM_PYTHON_LOG_LEVEL=DEBUG nvim
```
As more than one python host process might be started, the log filenames take
the pattern `logfile_PID` where `PID` is the process id.
the pattern `logfile_pyX_KIND` where `X` is the major python version (2 or 3)
and `KIND` is either "rplugin" or "script" (for the `:python[3]`
script interface).

If the host cannot start at all, the error could be found in `~/.nvimlog` if
`nvim` was compiled with logging.
Expand Down
16 changes: 12 additions & 4 deletions neovim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ def start_host(session=None):
if os.path.isdir(path) and dup in plugins:
plugins.remove(dup)

setup_logging()
# Special case: the legacy scripthost receives a single relative filename
# while the rplugin host will receive absolute paths.
if plugins == ["script_host.py"]:
name = "script"
else:
name = "rplugin"

setup_logging(name)

if not session:
session = stdio_session()
Expand Down Expand Up @@ -94,12 +101,13 @@ def attach(session_type, address=None, port=None,
return Nvim.from_session(session).with_decode(decode)


def setup_logging():
def setup_logging(name):
"""Setup logging according to environment variables."""
logger = logging.getLogger(__name__)
if 'NVIM_PYTHON_LOG_FILE' in os.environ:
logfile = (os.environ['NVIM_PYTHON_LOG_FILE'].strip() +
'_' + str(os.getpid()))
prefix = os.environ['NVIM_PYTHON_LOG_FILE'].strip()
major_version = sys.version_info[0]
logfile = '{0}_py{1}_{2}'.format(prefix, major_version, name)
handler = logging.FileHandler(logfile, 'w')
handler.formatter = logging.Formatter(
'%(asctime)s [%(levelname)s @ '
Expand Down
2 changes: 1 addition & 1 deletion test/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from nose.tools import eq_ as eq

neovim.setup_logging()
neovim.setup_logging("test")

child_argv = os.environ.get('NVIM_CHILD_ARGV')
listen_address = os.environ.get('NVIM_LISTEN_ADDRESS')
Expand Down

0 comments on commit cb23953

Please sign in to comment.