Skip to content

Commit

Permalink
File mapping logging. Set debug_mode=true in settings to see it.
Browse files Browse the repository at this point in the history
  • Loading branch information
danmoseley committed Sep 1, 2015
1 parent 32195b8 commit 8764e8c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
10 changes: 10 additions & 0 deletions projectsystem/DocumentMapping.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os
import logging
from projectsystem import Sourcemap

logger = logging.getLogger("SWI")

class Position:
""" All lines and columns in Sublime, in source maps,
and in the WebKit protocol are explicitly specified to be
Expand Down Expand Up @@ -89,6 +93,7 @@ class MappingInfo:
line_mappings = []

def __init__(self, generated_file):

source_map_file = Sourcemap.get_sourcemap_file(generated_file)
if not len(source_map_file):
return
Expand All @@ -97,8 +102,13 @@ def __init__(self, generated_file):
if not self.parsed_source_map.is_valid():
return

logger.info(' Source map is valid')

self.generated_file = generated_file
self.authored_sources = self.parsed_source_map.get_authored_sources_path()

[logger.info(' Found authored source %s which exists? %s' % (s, str(os.path.isfile(s)))) for s in self.authored_sources]

self.line_mappings = self.parsed_source_map.line_mappings

def is_valid(self):
Expand Down
29 changes: 22 additions & 7 deletions projectsystem/Sourcemap.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
import json
import os
import logging
from projectsystem import VLQDecoder

logger = logging.getLogger("SWI")

def get_sourcemap_file(file_name):
sourcemap_prefix = "//# sourceMappingURL="
map_file = ""

logger.info(' Looking for source map in %s' % (file_name))

try:
with open(file_name, "r") as f:
with open(file_name, "r", encoding="utf8") as f:
# Read the last line of the file containing sourcemap information
sourcemap_info = f.readlines()[-1]
if (sourcemap_info and len(sourcemap_info) > 0 and sourcemap_info.index(sourcemap_prefix) is 0):
map_file = sourcemap_info[len(sourcemap_prefix):].strip()
map_file = os.path.dirname(file_name) + os.path.sep + map_file
if sourcemap_info and len(sourcemap_info) > 0:
sourcemap_info.strip()
index = sourcemap_info.find(sourcemap_prefix)
if index == 0:
map_file = sourcemap_info[len(sourcemap_prefix):]
map_file = os.path.dirname(file_name) + os.path.sep + map_file
logger.info(' Found sourcemap comment %s' % (sourcemap_info))
f.close()
except Exception as e:
print('Could not read %s: %s' % (file_name, str(e)))
logger.info(' Could not read %s for source map info: %s' % (file_name, str(e)))
pass

return map_file


class ParsedSourceMap:
def __init__(self, file_name):
self.authored_sources = None
self.line_mappings = None

logger.info(' Reading source map %s' % (file_name))

try:
self.content = None
with open(file_name, "r") as f:
with open(file_name, "r", encoding="utf8") as f:
self.content = json.loads(f.read())
f.close()

Expand All @@ -34,7 +49,7 @@ def __init__(self, file_name):
self.authored_sources = self.content["sources"]
self.line_mappings = SourceMapParser.calculate_line_mappings(self.content)
except Exception as e:
print('Could not read %s: %s' % (file_name, str(e)))
logger.info(' Could not read source map %s: %s' % (file_name, str(e)))
pass

def is_valid(self):
Expand Down
4 changes: 2 additions & 2 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def send(self, command, callback=None, options=None):
command.options = options
self.commands[command.id] = command
self.next_id += 1
if utils.get_setting('debug_mode'):
if utils.get_setting('ws_debug_mode'):
print ('SWI: ->> ' + json.dumps(command.request, sort_keys=True, indent=4, separators=(',', ': ')))
self.socket.send(json.dumps(command.request))

Expand All @@ -65,7 +65,7 @@ def message_callback(self, ws, message):
Parse it and call matching callback.
"""
parsed = json.loads(message)
if utils.get_setting('debug_mode'):
if utils.get_setting('ws_debug_mode'):
print ('SWI: <<- ' + json.dumps(parsed, sort_keys=True, indent=4, separators=(',', ': ')))
if 'method' in parsed:
if parsed['method'] in self.notifications:
Expand Down
24 changes: 21 additions & 3 deletions swi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import imp
import re
import logging

swi_folder = os.path.dirname(os.path.realpath(__file__))
if not swi_folder in sys.path:
Expand Down Expand Up @@ -56,8 +57,15 @@
breakpoint_inactive_icon = 'Packages/Web Inspector/icons/breakpoint_inactive.png'
breakpoint_current_icon = 'Packages/Web Inspector/icons/breakpoint_current.png'

logger = logging.getLogger("SWI")
logger.propagate = False

def plugin_loaded():

if not logger.handlers and utils.get_setting('debug_mode'):
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)

close_all_our_windows()
clear_all_views()

Expand Down Expand Up @@ -281,6 +289,8 @@ def scriptParsed(self, data, notification):
file_name = ''
script = get_script(data['url'])

logger.info('====Notified of url %s====' % url)

if script:
if int(scriptId) > int(script['scriptId']):
script['scriptId'] = str(scriptId)
Expand All @@ -296,22 +306,30 @@ def scriptParsed(self, data, notification):
# eg., folder is c:\site and url is http://localhost/app.js
# glob for c:\site\app.js (primary) and c:\site\*\app.js (fallback only - there may be a c:\site\foo\app.js)
try:
files = glob.glob(folder + "\\" + "\\".join(url_parts)) + glob.glob(folder + "\\*\\" + "\\".join(url_parts))
glob1 = folder + "\\" + "\\".join(url_parts)
glob2 = folder + "\\*\\" + "\\".join(url_parts)
logger.info(' Glob for files at %s and %s' % (glob1, glob2))
files = glob.glob(glob1) + glob.glob(glob2)
except:
pass
else:
files = glob.glob(folder + "/" + "/".join(url_parts)) + glob.glob(folder + "/*/" + "/".join(url_parts))
glob1 = folder + "/" + "/".join(url_parts)
glob2 = folder + "/*/" + "/".join(url_parts)
logger.info(' Glob for files at %s and %s' % (glob1, glob2))
files = glob.glob(glob1) + glob.glob(glob2)

if len(files) > 0 and files[0] != '':
file_name = files[0]

if (file_name):
logger.info(' Matched %s to %s' % (url, file_name))
# Create a file mapping to look for mapped source code
projectsystem.DocumentMapping.MappingsManager.create_mapping(file_name)

file_to_scriptId.append({'file': file_name, 'scriptId': str(scriptId), 'url': data['url']})
file_to_scriptId.append({'file': file_name, 'scriptId': str(scriptId), 'url': data['url']})
# don't try to match shorter fragments, we already found a match
url_parts = []

if len(url_parts) > 0:
del url_parts[0]

Expand Down

0 comments on commit 8764e8c

Please sign in to comment.