Skip to content

Instantly share code, notes, and snippets.

@ilaif
Created April 16, 2019 21:01
Show Gist options
  • Select an option

  • Save ilaif/bedfc74336fadbb2a8cc6f9f4f8a8714 to your computer and use it in GitHub Desktop.

Select an option

Save ilaif/bedfc74336fadbb2a8cc6f9f4f8a8714 to your computer and use it in GitHub Desktop.
instrument-everything-blog-inject-instrumentation
def add_instrumentation(name, content):
lines = content.split('\n')
original_line_count = 1
instrumented_lines = []
subroutine_parenthesis_stack = []
sub_tested_line_numbers = []
tested_line_numbers = []
cur_subroutine_decl_line_number = 0
in_subroutine = False
in_synthetic = False
for raw_line in lines:
stripped_line = strip_line(raw_line)
is_tested = do_test_line(stripped_line)
l_ws_cou = count_leading_whitespaces(raw_line)
def add_log_line(subroutine_decl_line_number=cur_subroutine_decl_line_number, my_l_ws_cou=l_ws_cou):
instrumented_lines.append(get_var_line(original_line_count, tested_line_numbers, sub_tested_line_numbers,
instrumented_lines, subroutine_decl_line_number, my_l_ws_cou))
if is_line_sub_header(stripped_line):
# if we reached a sub header, we add the log for it *after*
# the line since we can't log outside subroutines
in_subroutine = True
instrumented_lines.append(raw_line)
cur_subroutine_decl_line_number = len(instrumented_lines)
subroutine_parenthesis_stack.append(original_line_count)
elif not in_subroutine:
instrumented_lines.append(raw_line)
elif in_subroutine:
if stripped_line[:11] == 'synthetic {':
add_log_line(subroutine_decl_line_number=cur_subroutine_decl_line_number,
my_l_ws_cou=2)
in_synthetic = True
if stripped_line[-2:] == '};':
in_synthetic = False
elif in_synthetic:
if stripped_line[-2:] == '};':
in_synthetic = False
elif has_close_params(is_tested, stripped_line): # is_close_parens
subroutine_parenthesis_stack.pop()
if end_of_subroutine(in_subroutine, subroutine_parenthesis_stack):
instrumented_lines.append(get_syslog_line(name, sub_tested_line_numbers, 2))
sub_tested_line_numbers = []
in_subroutine = False
elif has_open_params(is_tested, stripped_line):
add_log_line()
subroutine_parenthesis_stack.append(original_line_count)
elif has_open_params(is_tested, stripped_line): # is_open_parens
add_log_line()
subroutine_parenthesis_stack.append(original_line_count)
elif is_return_line(stripped_line):
add_log_line()
instrumented_lines.append(get_syslog_line(name, sub_tested_line_numbers, l_ws_cou))
elif is_tested:
add_log_line()
instrumented_lines.append(raw_line)
original_line_count += 1
instrumented_content = '\n'.join(instrumented_lines)
return instrumented_content, original_line_count, tested_line_numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment