Create a way to log to the list in Vim

This commit is contained in:
ZyX 2015-01-05 23:33:06 +03:00
parent d51a50223e
commit 1c1d33dbbf
4 changed files with 50 additions and 2 deletions

View File

@ -31,6 +31,12 @@ Vim configuration can be overridden using the following options:
If this variable is set to a true value it will prevent Powerline from reporting
an error when loaded in a copy of vim without the necessary Python support.
``g:powerline_use_var_handler``
This variable may be set to either 0 or 1. If it is set to 1 then Vim will
save log in ``g:powerline_log_messages`` variable in addition to whatever
was configured in :ref:`log_* options <config-common-log>`. Level is always
:ref:`log_level <config-common-log_level>`, same for format.
Powerline script overrides
==========================

View File

@ -98,6 +98,8 @@ Common configuration is a subdictionary that is a value of ``common`` key in
Defines path which will hold powerline logs. If not present, logging will be
done to stderr.
.. _config-common-log_level:
``log_level``
String, determines logging level. Defaults to ``WARNING``.

View File

@ -424,6 +424,14 @@ class Powerline(object):
Usually returns encoding of the current locale.
'''
def create_logger(self):
'''Create logger
This function is used to create logger unless it was already specified
at initialization.
'''
return create_logger(self.common_config, self.default_log_stream)
def create_renderer(self, load_main=False, load_colors=False, load_colorscheme=False, load_theme=False):
'''(Re)create renderer object. Can be used after Powerline object was
successfully initialized. If any of the below parameters except
@ -461,7 +469,7 @@ class Powerline(object):
self.import_paths = self.common_config['paths']
if not self.logger:
self.logger = create_logger(self.common_config, self.default_log_stream)
self.logger = self.create_logger()
if not self.pl:
self.pl = PowerlineLogger(self.use_daemon_threads, self.logger, self.ext)

View File

@ -3,14 +3,16 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
import sys
import json
import logging
from itertools import count
import vim
from powerline.bindings.vim import vim_get_func, vim_getvar, get_vim_encoding
from powerline.bindings.vim import vim_get_func, vim_getvar, get_vim_encoding, python_to_vim
from powerline import Powerline, FailedUnicode
from powerline.lib.dict import mergedicts
from powerline.lib.unicode import u
def _override_from(config, override_varname):
@ -22,6 +24,24 @@ def _override_from(config, override_varname):
return config
class VimVarHandler(logging.Handler):
'''Vim-specific handler which emits messages to Vim global variables
Used variable: ``g:powerline_log_messages``.
'''
def __init__(self, *args, **kwargs):
super(VimVarHandler, self).__init__(*args, **kwargs)
vim.command('unlet! g:powerline_log_messages')
vim.command('let g:powerline_log_messages = []')
@staticmethod
def emit(record):
message = u(record.message)
if record.exc_text:
message += '\n' + u(record.exc_text)
vim.eval(b'add(g:powerline_log_messages, ' + python_to_vim(message) + b')')
class VimPowerline(Powerline):
def init(self, pyeval='PowerlinePyeval', **kwargs):
super(VimPowerline, self).init('vim', **kwargs)
@ -31,6 +51,18 @@ class VimPowerline(Powerline):
default_log_stream = sys.stdout
def create_logger(self):
logger = super(VimPowerline, self).create_logger()
try:
if int(vim_getvar('powerline_use_var_handler')):
formatter = logging.Formatter(self.common_config['log_format'])
handler = VimVarHandler(getattr(logging, self.common_config['log_level']))
handler.setFormatter(formatter)
logger.addHandler(handler)
except KeyError:
pass
return logger
def add_local_theme(self, key, config):
'''Add local themes at runtime (during vim session).