Sublime Forum

Monkeypatch your sublime.log_* functions to toggle

#1

I was annoyed by this and just thought about how easy it would be to just monkeypatch it, so I did.

Create a new file named bootstrap_logging.py in your User folder and fill it with “the code”.

I got the updated code in a gist on github, but following is the current snippet in case I might delete it at one point:

import sublime
from functools import partial

function_names = ('input', 'commands', 'result_regex', 'indexing')

# Cleanup
if hasattr(sublime, 'logging_unbootstrap'):
    sublime.logging_unbootstrap()

# Assure this exists
if not hasattr(sublime, 'logging_cache'):
    setattr(sublime, 'logging_cache', {})

# Collect original functions
original_functions = {name: getattr(sublime, "log_" + name)
                      for name in function_names}


# Override functions with our new function
def toogle_logging(name, value=None):
    if value is None:
        value = not sublime.logging_cache.get(name, False)
    if value in (True, False):
        sublime.logging_cache[name] = value
    original_functions[name](value)

for name in function_names:
    setattr(sublime, "log_" + name, partial(toogle_logging, name))


# Register cleanup
def unbootstrap():
    for name, func in original_functions.items():
        setattr(sublime, "log_" + name, func)
    delattr(sublime, 'logging_unbootstrap')

setattr(sublime, 'logging_unbootstrap', unbootstrap)

# Done
print("Note: sublime.log* methods have been overridden and now toggle when "
      "called without a parameter")


def plugin_unloaded():
    unbootstrap()
0 Likes