Skip to content

Commit

Permalink
Merge pull request #162 from Leviaria/main
Browse files Browse the repository at this point in the history
Add Configurable Logging Level to Bot Configuration
  • Loading branch information
Leviaria authored Jun 20, 2024
2 parents 54e308c + e68df79 commit 09de14f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
14 changes: 13 additions & 1 deletion clashroyalebuildabot/bot/example/custom_bot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import random
import subprocess
import time
import os
import yaml
import sys

from loguru import logger

from clashroyalebuildabot.bot import Bot
from clashroyalebuildabot.bot.bot import Action
from clashroyalebuildabot.bot.example.custom_action import CustomAction
Expand All @@ -23,6 +25,16 @@ class CustomBot(Bot):
]

def __init__(self, cards=None, debug=False):
config_path = os.path.join(os.path.dirname(__file__), "..", "..", "config.yaml")
with open(config_path, encoding="utf-8") as file:
config = yaml.safe_load(file)

log_level = config.get("bot", {}).get("log_level", "INFO").upper()

logger.remove()
logger.add(sys.stdout, level=log_level)
logger.add(os.path.join(os.path.dirname(__file__), "..", "bot.log"), rotation="500 MB", level=log_level)

if cards is None:
cards = self.PRESET_DECK
if set(cards) != set(self.PRESET_DECK):
Expand Down
6 changes: 6 additions & 0 deletions clashroyalebuildabot/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
bot:
# The logging level for the bot.
# Use "DEBUG" for detailed debugging information, "INFO" for general information,
# "WARNING" for warning messages, "ERROR" for error messages, and "CRITICAL" for critical issues.
log_level: "INFO"

adb:
# The IP address of your device or emulator.
# Use 127.0.0.1 for a local emulator. If you're connecting to a physical device,
Expand Down
13 changes: 7 additions & 6 deletions clashroyalebuildabot/emulator.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import os

from adb_shell.adb_device import AdbDeviceTcp
from loguru import logger
from PIL import Image
import yaml

from clashroyalebuildabot.constants import SCREENSHOT_HEIGHT
from clashroyalebuildabot.constants import SCREENSHOT_WIDTH
from clashroyalebuildabot.constants import SRC_DIR

import sys
from clashroyalebuildabot.constants import SCREENSHOT_HEIGHT, SCREENSHOT_WIDTH, SRC_DIR

class Emulator:
def __init__(self):
config_path = os.path.join(SRC_DIR, "config.yaml")
with open(config_path, encoding="utf-8") as file:
config = yaml.safe_load(file)

log_level = config.get("bot", {}).get("log_level", "INFO").upper()
logger.remove()
logger.add(sys.stdout, level=log_level)
logger.add(os.path.join(SRC_DIR, "bot.log"), rotation="500 MB", level=log_level)

adb_config = config["adb"]
device_ip = adb_config["ip"]
device_port = adb_config["port"]
Expand Down
13 changes: 12 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import threading
import time
import yaml
from loguru import logger
from clashroyalebuildabot.bot.example.custom_bot import CustomBot
from clashroyalebuildabot.constants import DEBUG_DIR
Expand All @@ -25,8 +26,18 @@ def main():
bot.run()

if __name__ == "__main__":
config_path = os.path.join(os.path.dirname(__file__), "clashroyalebuildabot/config.yaml")
with open(config_path, encoding="utf-8") as file:
config = yaml.safe_load(file)

log_level = config.get("bot", {}).get("log_level", "INFO").upper()

logger.remove()
logger.add(sys.stdout, level=log_level)
logger.add(os.path.join(DEBUG_DIR, "bot.log"), rotation="500 MB", level=log_level)

# Update-Prüfung und Hauptprogramm starten
Updater().check_for_update()
logger.add(os.path.join(DEBUG_DIR, "bot.log"), rotation="500 MB")
title_thread = threading.Thread(target=update_terminal_title, daemon=True)
title_thread.start()
main()

0 comments on commit 09de14f

Please sign in to comment.