Skip to content

Commit

Permalink
Using logging instead of plain print() (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
LuKks and sindresorhus committed Nov 20, 2020
1 parent 30ebf8d commit 92aa8b3
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sublime
import sublime_plugin
import subprocess
import logging
from SublimeLinter.lint import (
NodeLinter,
linter as linter_module
Expand All @@ -12,6 +13,7 @@
from SublimeLinter.lint.linter import PermanentError
from SublimeLinter.lint.base_linter.node_linter import read_json_file

logger = logging.getLogger('SublimeLinter.plugin.xo')

STANDARD_SELECTOR = 'source.js'
PLUGINS = {
Expand Down Expand Up @@ -103,10 +105,10 @@ def xo_fix(self, view, content):
if encoding == 'Undefined':
encoding = 'utf-8'

print('xo_fix -> content length:', len(content))
print('xo_fix -> encoding:', encoding)
print('xo_fix -> xo_env.PWD:', self.xo_env['PWD'])
print('xo_fix -> xo_project_root:', self.xo_project_root)
logger.info('xo_fix content length: %d', len(content))
logger.info('xo_fix encoding: %s', encoding)
logger.info('xo_fix xo_env.PWD: %s', self.xo_env['PWD'])
logger.info('xo_fix xo_project_root: %s', self.xo_project_root)

# TODO: Change to use `subprocess.run()` when Sublime updates Python to 3.5 or later.
proc = subprocess.Popen(
Expand All @@ -119,43 +121,43 @@ def xo_fix(self, view, content):
startupinfo=startup_info,
)
stdout, stderr = proc.communicate(content)
print('xo_fix -> stdout len:', len(stdout))
print('xo_fix -> stderr len:', len(stderr))
print('xo_fix -> stderr content:', stderr.decode(encoding))
print('xo_fix -> returncode:', proc.returncode)
logger.info('xo_fix stdout len: %d', len(stdout))
logger.info('xo_fix stderr len: %d', len(stderr))
logger.info('xo_fix stderr content: %s', stderr.decode(encoding))
logger.info('xo_fix returncode: %d', proc.returncode)

if proc.returncode != 0:
sublime.message_dialog("[xo_fix " + str(proc.returncode) + "] " + stderr.decode(encoding))
sublime.message_dialog('[xo_fix ' + str(proc.returncode) + '] ' + stderr.decode(encoding))
return None

return stdout.decode(encoding)

class XoFixCommand(sublime_plugin.TextCommand):
def is_enabled(self):
print('XoFixCommand -> is_enabled?')
logger.info('XoFixCommand is_enabled?')
linter = make_fake_linter(self.view)
print('XoFixCommand -> project_root ->', linter.context.get('project_root'))
logger.info('XoFixCommand project_root → %s', linter.context.get('project_root'))

self.xo_start_dir = linter.get_start_dir()
print('XoFixCommand -> xo_start_dir', self.xo_start_dir)
logger.info('XoFixCommand xo_start_dir %s', self.xo_start_dir)
if not self.xo_start_dir:
print('XoFixCommand -> xo_start_dir -> False')
logger.info('XoFixCommand xo_start_dir False')
return False

self.xo_path = linter.find_local_executable(self.xo_start_dir, 'xo')
print('XoFixCommand -> xo_path', self.xo_path)
logger.info('XoFixCommand xo_path %s', self.xo_path)
if not self.xo_path:
print('XoFixCommand -> xo_path -> False')
logger.info('XoFixCommand xo_path False')
return False

self.xo_project_root = linter.context.get('project_root')
self.xo_env = os.environ.copy()
self.xo_env['PWD'] = self.xo_project_root
self.xo_env['PATH'] += os.pathsep + '/usr/local/bin' # Ensure correct PATH for Node.js on macOS

print('XoFixCommand -> environ.path ->', self.xo_env['PATH'])
print('XoFixCommand -> project_root ->', self.xo_project_root)
print('XoFixCommand -> return -> True')
logger.info('XoFixCommand environ.path → %s', self.xo_env['PATH'])
logger.info('XoFixCommand project_root → %s', self.xo_project_root)
logger.info('XoFixCommand return True')
return True

def run(self, edit):
Expand Down

0 comments on commit 92aa8b3

Please sign in to comment.