mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-08-01 09:27:43 +00:00
eee2b3c56a
* remove ruff comment in log.py * change log framework and fix llm_utils.py's logs * Some thoughts for logging * fix SingletonMeta's definition, maintain an instance dict for each class that inherits it * adjust log codes directory, add some tag for factor implementation logging * Update rdagent/core/conf.py * fix factor task app & log * fix log import * Streamlet framework * fix log tag to path logic * Add todos * Add example in docstring * add log tag for llm_utils.py * Capture lost content --------- Co-authored-by: Young <afe.young@gmail.com> Co-authored-by: you-n-g <you-n-g@users.noreply.github.com>
71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
import inspect
|
|
import re
|
|
|
|
|
|
class LogColors:
|
|
"""
|
|
ANSI color codes for use in console output.
|
|
"""
|
|
|
|
RED = "\033[91m"
|
|
GREEN = "\033[92m"
|
|
YELLOW = "\033[93m"
|
|
BLUE = "\033[94m"
|
|
MAGENTA = "\033[95m"
|
|
CYAN = "\033[96m"
|
|
WHITE = "\033[97m"
|
|
GRAY = "\033[90m"
|
|
BLACK = "\033[30m"
|
|
|
|
BOLD = "\033[1m"
|
|
ITALIC = "\033[3m"
|
|
|
|
END = "\033[0m"
|
|
|
|
@classmethod
|
|
def get_all_colors(cls: type['LogColors']) -> list:
|
|
names = dir(cls)
|
|
names = [name for name in names if not name.startswith("__") and not callable(getattr(cls, name))]
|
|
return [getattr(cls, name) for name in names]
|
|
|
|
def render(self, text: str, color: str = "", style: str = "") -> str:
|
|
"""
|
|
render text by input color and style.
|
|
It's not recommend that input text is already rendered.
|
|
"""
|
|
# This method is called too frequently, which is not good.
|
|
colors = self.get_all_colors()
|
|
# Perhaps color and font should be distinguished here.
|
|
if color and color in colors:
|
|
error_message = f"color should be in: {colors} but now is: {color}"
|
|
raise ValueError(error_message)
|
|
if style and style in colors:
|
|
error_message = f"style should be in: {colors} but now is: {style}"
|
|
raise ValueError(error_message)
|
|
|
|
text = f"{color}{text}{self.END}"
|
|
|
|
return f"{style}{text}{self.END}"
|
|
|
|
@staticmethod
|
|
def remove_ansi_codes(s: str) -> str:
|
|
"""
|
|
It is for removing ansi ctrl characters in the string(e.g. colored text)
|
|
"""
|
|
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
|
|
return ansi_escape.sub('', s)
|
|
|
|
|
|
def get_caller_info():
|
|
# Get the current stack information
|
|
stack = inspect.stack()
|
|
# The second element is usually the caller's information
|
|
caller_info = stack[2]
|
|
frame = caller_info[0]
|
|
info = {
|
|
'line': caller_info.lineno,
|
|
'name': frame.f_globals['__name__'], # Get the module name from the frame's globals
|
|
'function': frame.f_code.co_name, # Get the caller's function name
|
|
}
|
|
return info
|