Skip to content

Commit

Permalink
added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbion committed Oct 2, 2023
1 parent 05a0358 commit 1376c11
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pilot/helpers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import platform

from logger.logger import logger
from utils.style import yellow, green, red, yellow_bold, white_bold
from database.database import get_saved_command_run, save_command_run
from helpers.exceptions.TooDeepRecursionError import TooDeepRecursionError
Expand All @@ -15,13 +16,15 @@

interrupted = False


def enqueue_output(out, q):
for line in iter(out.readline, ''):
if interrupted: # Check if the flag is set
break
q.put(line)
out.close()


def run_command(command, root_path, q_stdout, q_stderr, pid_container):
"""
Execute a command in a subprocess.
Expand All @@ -36,6 +39,7 @@ def run_command(command, root_path, q_stdout, q_stderr, pid_container):
Returns:
subprocess.Popen: The subprocess object.
"""
logger.info(f'Running `{command}`')
if platform.system() == 'Windows': # Check the operating system
process = subprocess.Popen(
command,
Expand Down Expand Up @@ -65,19 +69,19 @@ def run_command(command, root_path, q_stdout, q_stderr, pid_container):
t_stderr.start()
return process


def terminate_process(pid):
if platform.system() == "Windows":
try:
subprocess.run(["taskkill", "/F", "/T", "/PID", str(pid)])
except subprocess.CalledProcessError:
# Handle any potential errors here
pass
except subprocess.CalledProcessError as e:
logger.error(f'Error while terminating process: {e}')
else: # Unix-like systems
try:
os.killpg(pid, signal.SIGKILL)
except OSError:
# Handle any potential errors here
pass
except OSError as e:
logger.error(f'Error while terminating process: {e}')


def execute_command(project, command, timeout=None, force=False):
"""
Expand Down Expand Up @@ -159,6 +163,7 @@ def execute_command(project, command, timeout=None, force=False):
output_line = q.get_nowait()
if output_line not in output:
print(green('CLI OUTPUT:') + output_line, end='')
logger.info('CLI OUTPUT: ' + output_line)
output += output_line
break

Expand All @@ -176,6 +181,7 @@ def execute_command(project, command, timeout=None, force=False):
if line:
output += line
print(green('CLI OUTPUT:') + line, end='')
logger.info('CLI OUTPUT: ' + line)

# Read stderr
try:
Expand All @@ -186,13 +192,16 @@ def execute_command(project, command, timeout=None, force=False):
if stderr_line:
stderr_output += stderr_line
print(red('CLI ERROR:') + stderr_line, end='') # Print with different color for distinction
logger.error('CLI ERROR: ' + stderr_line)

except (KeyboardInterrupt, TimeoutError) as e:
interrupted = True
if isinstance(e, KeyboardInterrupt):
print("\nCTRL+C detected. Stopping command execution...")
print('\nCTRL+C detected. Stopping command execution...')
logger.info('CTRL+C detected. Stopping command execution...')
else:
print("\nTimeout detected. Stopping command execution...")
print('\nTimeout detected. Stopping command execution...')
logger.warn('Timeout detected. Stopping command execution...')

terminate_process(pid_container[0])

Expand Down
3 changes: 3 additions & 0 deletions pilot/logger/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import logging


Expand Down Expand Up @@ -45,6 +46,8 @@ def filter_sensitive_fields(record):
args_list = ['*****' if arg in sensitive_fields else arg for arg in args_list]
record.args = tuple(args_list)

# Remove ANSI escape sequences - colours & bold
record.msg = re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', record.msg)
return record.levelno <= logging.INFO


Expand Down
2 changes: 2 additions & 0 deletions pilot/utils/llm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def create_gpt_chat_completion(messages: List[dict], req_type, min_tokens=MIN_TO
except TokenLimitError as e:
raise e
except Exception as e:
logger.error(f'The request to {os.getenv("ENDPOINT")} API failed: %s', e)
print(f'The request to {os.getenv("ENDPOINT")} API failed. Here is the error message:')
print(e)

Expand Down Expand Up @@ -193,6 +194,7 @@ def wrapper(*args, **kwargs):
])).ask()

# TODO: take user's input into consideration - send to LLM?
# https://github.com/Pythagora-io/gpt-pilot/issues/122
if user_message != '':
return {}

Expand Down

0 comments on commit 1376c11

Please sign in to comment.