<commit_before>import datetime
import inspect
from contextlib import contextmanager
from typing import Iterator, Callable, Any

from approvaltests.utilities.string_wrapper import StringWrapper
from approvaltests.namer import StackFrameNamer


class LoggingInstance:

    def __init__(self):
        self.previous_timestamp = None
        self.logger = lambda t: print(t, end="")
        self.tabbing = 0
        self.counter = 0
        self.log_with_timestamps = True
        self.timer: Callable[[], datetime.datetime] = datetime.datetime.now

    def log_to_string(self) -> StringWrapper:
        buffer = StringWrapper()
        self.logger = buffer.append
        self.log_with_timestamps = False
        return buffer

    @contextmanager
    def use_markers(self, additional_stack: int = 0) -> Iterator[None]:
        stack_position = 1 + additional_stack
        stack = inspect.stack(stack_position)[2]
        method_name = stack.function
        filename = StackFrameNamer.get_class_name_for_frame(stack)
        expected = f"-> in: {method_name}(){filename}"
        self.log(expected)
        self.tabbing = self.tabbing + 1
        yield
        self.tabbing = self.tabbing - 1
        expected = f"<- out: {method_name}(){filename}"
        self.log(expected)
        pass

    def log(self, text: str) -> None:
        if self.counter != 0:
            self.logger("\n")
            self.counter = 0
        timestamp = self.get_timestamp()
        self.logger(f"{timestamp}{self.get_tabs()}{text}\n")

    def get_timestamp(self) -> str:
        timestamp = ""
        if self.log_with_timestamps:
            time1: datetime.datetime = self.timer()
            time = time1.strftime("%Y-%m-%dT%H:%M:%SZ")
            diff_millseconds = 0
            if self.previous_timestamp != None:
                delta = time1 - self.previous_timestamp
                diff_millseconds = int((delta).total_seconds() * 1000)
            diff = diff_millseconds
            diff_display = f" ~{diff:06}ms"
            timestamp = f"[{time} {diff_display}] "
            self.previous_timestamp = time1
        return timestamp

    def variable(self, name: str, value: Any) -> None:
        display_variable = f"variable: {name} = {value}"
        self.log(display_variable)

    def hour_glass(self) -> None:
        self.increment_hour_glass_counter()
        if self.counter == 1:
            self.logger(f"{self.get_tabs()}.")
        elif self.counter == 100:
            self.logger("10\n")
            self.counter = 0
        elif self.counter % 10 == 0:
            digit = int(self.counter / 10)
            self.logger(f"{digit}")
        else:
            self.logger(".")

    def get_tabs(self) -> str:
        return "  " * self.tabbing

    def increment_hour_glass_counter(self) -> None:
        self.counter = self.counter + 1

    def event(self, event_name: str) -> None:
        self.log(f"event: {event_name}")

    def query(self, query_text:str) -> None:
        self.log(f"Sql: {query_text}")

    def message(self, message):
        self.log(f"message: {message}")


    def warning(self, exception: Exception) -> None:
        t = self.log_with_timestamps
        self.log_with_timestamps = False
        warning_text = "*" * 91
        self.log(warning_text)
        if t:
            self.log_with_timestamps = True
            self.log("")
            self.log_with_timestamps = False
        self.log(str(exception))
        self.log(warning_text)
        self.log_with_timestamps = t<commit_msg>R   Cleaned up logging with timestamps<commit_after>import datetime
import inspect
from contextlib import contextmanager
from typing import Iterator, Callable, Any

from approvaltests.utilities.string_wrapper import StringWrapper
from approvaltests.namer import StackFrameNamer


class LoggingInstance:

    def __init__(self):
        self.previous_timestamp = None
        self.logger = lambda t: print(t, end="")
        self.tabbing = 0
        self.counter = 0
        self.log_with_timestamps = True
        self.timer: Callable[[], datetime.datetime] = datetime.datetime.now

    def log_to_string(self) -> StringWrapper:
        buffer = StringWrapper()
        self.logger = buffer.append
        self.log_with_timestamps = False
        return buffer

    @contextmanager
    def use_markers(self, additional_stack: int = 0) -> Iterator[None]:
        stack_position = 1 + additional_stack
        stack = inspect.stack(stack_position)[2]
        method_name = stack.function
        filename = StackFrameNamer.get_class_name_for_frame(stack)
        expected = f"-> in: {method_name}(){filename}"
        self.log_and_add_timestamps_if_needed(expected)
        self.tabbing = self.tabbing + 1
        yield
        self.tabbing = self.tabbing - 1
        expected = f"<- out: {method_name}(){filename}"
        self.log_and_add_timestamps_if_needed(expected)
        pass

    def log_and_add_timestamps_if_needed(self, text: str, use_timestamps=True) -> None:
        if self.counter != 0:
            self.logger("\n")
            self.counter = 0
        timestamp = self.get_timestamp() if use_timestamps else ""
        output_message = f"{timestamp}{self.get_tabs()}{text}\n"
        self.logger(output_message)

    def get_timestamp(self) -> str:
        timestamp = ""
        if self.log_with_timestamps:
            time1: datetime.datetime = self.timer()
            time = time1.strftime("%Y-%m-%dT%H:%M:%SZ")
            diff_millseconds = 0
            if self.previous_timestamp != None:
                delta = time1 - self.previous_timestamp
                diff_millseconds = int((delta).total_seconds() * 1000)
            diff = diff_millseconds
            diff_display = f" ~{diff:06}ms"
            timestamp = f"[{time} {diff_display}] "
            self.previous_timestamp = time1
        return timestamp

    def variable(self, name: str, value: Any) -> None:
        display_variable = f"variable: {name} = {value}"
        self.log_and_add_timestamps_if_needed(display_variable)

    def hour_glass(self) -> None:
        self.increment_hour_glass_counter()
        if self.counter == 1:
            self.logger(f"{self.get_tabs()}.")
        elif self.counter == 100:
            self.logger("10\n")
            self.counter = 0
        elif self.counter % 10 == 0:
            digit = int(self.counter / 10)
            self.logger(f"{digit}")
        else:
            self.logger(".")

    def get_tabs(self) -> str:
        return "  " * self.tabbing

    def increment_hour_glass_counter(self) -> None:
        self.counter = self.counter + 1

    def event(self, event_name: str) -> None:
        self.log_and_add_timestamps_if_needed(f"event: {event_name}")

    def query(self, query_text:str) -> None:
        self.log_and_add_timestamps_if_needed(f"Sql: {query_text}")

    def message(self, message):
        self.log_and_add_timestamps_if_needed(f"message: {message}")


    def warning(self, exception: Exception) -> None:
        warning_text = "*" * 91
        self.log_and_add_timestamps_if_needed(warning_text, use_timestamps=False)
        if self.log_with_timestamps:
            self.log_and_add_timestamps_if_needed("")
        self.log_and_add_timestamps_if_needed(str(exception), use_timestamps=False)
        self.log_and_add_timestamps_if_needed(warning_text, use_timestamps=False)
{'fraction_non_alphanumeric': 0.10894308943089431, 'fraction_numerical': 0.009485094850948509, 'mean_word_length': 2.413968547641073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2135057', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2202, 'n_words': 451}