Skip to content

Commit da5bd02

Browse files
committed
Fixed a potential crash if you try to enable logging while running the .exe file
1 parent 382adc9 commit da5bd02

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

NSE_Option_Chain_Analyzer.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__(self, window: Tk) -> None:
6060
self.config_parser: configparser.ConfigParser = configparser.ConfigParser()
6161
self.create_config(new=True) if not os.path.isfile('NSE-OCA.ini') else None
6262
self.get_config()
63+
self.log_file: Optional[TextIO] = None
6364
self.log() if self.logging else None
6465
self.units_str: str = 'in K' if self.option_mode == 'Index' else 'in 10s'
6566
self.output_columns: Tuple[str, str, str, str, str, str, str, str, str] = (
@@ -733,8 +734,15 @@ def toggle_updates(self, event: Optional[Event] = None) -> None:
733734
# noinspection PyUnusedLocal
734735
def log(self, event: Optional[Event] = None) -> None:
735736
if self.first_run and self.logging or not self.logging:
736-
streamtologger.redirect(target="NSE-OCA.log",
737-
header_format="[{timestamp:%Y-%m-%d %H:%M:%S} - {level:5}] ")
737+
try:
738+
# noinspection PyProtectedMember,PyUnresolvedReferences
739+
base_path: str = sys._MEIPASS
740+
self.log_file = open('NSE-OCA.log', 'a', buffering=1)
741+
sys.stdout = self.log_file
742+
sys.stderr = self.log_file
743+
except AttributeError:
744+
streamtologger.redirect(target="NSE-OCA.log",
745+
header_format="[{timestamp:%Y-%m-%d %H:%M:%S} - {level:5}] ")
738746
self.logging = True
739747
print('----------Logging Started----------')
740748

@@ -757,7 +765,12 @@ def log(self, event: Optional[Event] = None) -> None:
757765
print('----------Logging Stopped----------')
758766
sys.stdout = self.stdout
759767
sys.stderr = self.stderr
760-
streamtologger._is_redirected = False
768+
try:
769+
# noinspection PyProtectedMember,PyUnresolvedReferences
770+
base_path: str = sys._MEIPASS
771+
self.log_file.close()
772+
except AttributeError:
773+
streamtologger._is_redirected = False
761774
self.logging = False
762775
self.options.entryconfig(self.options.index(9), label="Debug Logging: Off")
763776
messagebox.showinfo(title="Debug Logging Disabled", message="Errors will not be logged.")

0 commit comments

Comments
 (0)