From 2e02cd74a507c3f1efe25e968892c01a9bbf5231 Mon Sep 17 00:00:00 2001 From: rawgni Date: Tue, 6 Sep 2016 10:23:54 +0700 Subject: [PATCH] one log handler --- pokemongo_bot/__init__.py | 10 +- pokemongo_bot/event_handlers/__init__.py | 1 - .../event_handlers/colored_logging_handler.py | 138 ------------------ .../event_handlers/logging_handler.py | 127 +++++++++++++++- 4 files changed, 127 insertions(+), 149 deletions(-) delete mode 100644 pokemongo_bot/event_handlers/colored_logging_handler.py diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 8d6f49a705..082644a47d 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -29,7 +29,7 @@ from item_list import Item from metrics import Metrics from sleep_schedule import SleepSchedule -from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler, ColoredLoggingHandler, SocialHandler +from pokemongo_bot.event_handlers import SocketIoHandler, LoggingHandler, SocialHandler from pokemongo_bot.socketio_server.runner import SocketIoRunner from pokemongo_bot.websocket_remote_control import WebsocketRemoteControl from pokemongo_bot.base_dir import _base_dir @@ -145,10 +145,10 @@ def start(self): def _setup_event_system(self): handlers = [] - if self.config.logging and 'color' in self.config.logging and self.config.logging['color']: - handlers.append(ColoredLoggingHandler(self)) - else: - handlers.append(LoggingHandler(self)) + color = self.config.logging and 'color' in self.config.logging and self.config.logging['color'] + debug = self.config.debug + + handlers.append(LoggingHandler(color, debug)) if self.config.enable_social: handlers.append(SocialHandler(self)) diff --git a/pokemongo_bot/event_handlers/__init__.py b/pokemongo_bot/event_handlers/__init__.py index c4c03cc37f..f2fb40d7a4 100644 --- a/pokemongo_bot/event_handlers/__init__.py +++ b/pokemongo_bot/event_handlers/__init__.py @@ -1,5 +1,4 @@ from logging_handler import LoggingHandler from socketio_handler import SocketIoHandler -from colored_logging_handler import ColoredLoggingHandler from social_handler import SocialHandler from telegram_handler import TelegramHandler diff --git a/pokemongo_bot/event_handlers/colored_logging_handler.py b/pokemongo_bot/event_handlers/colored_logging_handler.py deleted file mode 100644 index 0b0e8f116d..0000000000 --- a/pokemongo_bot/event_handlers/colored_logging_handler.py +++ /dev/null @@ -1,138 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -import logging - -from pokemongo_bot.event_manager import EventHandler - - -class ColoredLoggingHandler(EventHandler): - EVENT_COLOR_MAP = { - 'api_error': 'red', - 'badges': 'blue', - 'bot_exit': 'red', - 'bot_start': 'green', - 'catch_limit': 'red', - 'catch_log': 'magenta', - 'config_error': 'red', - 'egg_already_incubating': 'yellow', - 'egg_hatched': 'green', - 'egg_hatched_fail': 'red', - 'eggs_hatched_log': 'magenta', - 'evolve_log': 'magenta', - 'future_pokemon_release': 'yellow', - 'incubate': 'green', - 'incubator_already_used': 'yellow', - 'inventory_full': 'yellow', - 'item_discard_fail': 'red', - 'item_discarded': 'green', - 'next_force_recycle': 'green', - 'force_recycle': 'green', - 'keep_best_release': 'green', - 'level_up': 'green', - 'level_up_reward': 'green', - 'location_cache_error': 'yellow', - 'location_cache_ignored': 'yellow', - 'login_failed': 'red', - 'login_log': 'magenta', - 'login_successful': 'green', - 'log_stats': 'magenta', - 'lucky_egg_error': 'red', - 'move_to_map_pokemon_encounter': 'green', - 'move_to_map_pokemon_fail': 'red', - 'next_egg_incubates': 'yellow', - 'next_sleep': 'green', - 'next_random_pause': 'green', - 'next_random_alive_pause': 'green', - 'no_pokeballs': 'red', - 'path_lap_end': 'green', - 'pokemon_appeared': 'yellow', - 'pokemon_capture_failed': 'red', - 'pokemon_caught': 'blue', - 'pokemon_evolved': 'green', - 'pokemon_fled': 'red', - 'pokemon_inventory_full': 'red', - 'pokemon_nickname_invalid': 'red', - 'pokemon_not_in_range': 'yellow', - 'pokemon_release': 'green', - 'pokemon_upgraded': 'green', - 'pokemon_vanished': 'red', - 'pokestop_empty': 'yellow', - 'pokestop_log': 'magenta', - 'pokestop_searching_too_often': 'yellow', - 'rename_pokemon': 'green', - 'show_best_pokemon': 'magenta', - 'show_inventory': 'magenta', - 'skip_evolve': 'yellow', - 'softban': 'red', - 'softban_log': 'magenta', - 'spin_limit': 'red', - 'spun_pokestop': 'cyan', - 'threw_berry_failed': 'red', - 'transfer_log': 'magenta', - 'unknown_spin_result': 'red', - 'unset_pokemon_nickname': 'red', - 'vip_pokemon': 'red', - 'use_incense': 'blue', - 'vanish_limit_reached': 'red', - - 'arrived_at_cluster': 'none', - 'arrived_at_fort': 'none', - 'bot_sleep': 'none', - 'bot_random_pause': 'none', - 'bot_random_alive_pause': 'none', - 'catchable_pokemon': 'none', - 'found_cluster': 'none', - 'incubate_try': 'none', - 'load_cached_location': 'none', - 'location_found': 'none', - 'login_started': 'none', - 'lured_pokemon_found': 'none', - 'move_to_map_pokemon_move_towards': 'none', - 'move_to_map_pokemon_teleport_back': 'none', - 'move_to_map_pokemon_updated_map': 'none', - 'moving_to_fort': 'none', - 'moving_to_lured_fort': 'none', - 'pokemon_catch_rate': 'none', - 'pokemon_evolve_fail': 'none', - 'pokestop_on_cooldown': 'none', - 'pokestop_out_of_range': 'none', - 'polyline_request': 'none', - 'position_update': 'none', - 'path_lap_update': 'none', - 'set_start_location': 'none', - 'softban_fix': 'none', - 'softban_fix_done': 'none', - 'spun_fort': 'none', - 'threw_berry': 'none', - 'threw_pokeball': 'none', - 'used_lucky_egg': 'none' - } - COLOR_CODE = { - 'gray': '\033[90m', - 'red': '\033[91m', - 'green': '\033[92m', - 'yellow': '\033[93m', - 'blue': '\033[94m', - 'magenta': '\033[95m', - 'cyan': '\033[96m', - 'white': '\033[97m', - 'none': '\033[0m' - } - - def __init__(self, bot): - self.bot = bot - - def handle_event(self, event, sender, level, formatted_msg, data): - if not formatted_msg: - formatted_msg = str(data) - - if event in self.EVENT_COLOR_MAP: - color = self.COLOR_CODE[self.EVENT_COLOR_MAP[event]] - formatted_msg = '{}{}{}'.format(color, formatted_msg, self.COLOR_CODE['none']) - - if self.bot.config.debug: - formatted_msg = '[{}] {}'.format(event, formatted_msg) - - logger = logging.getLogger(type(sender).__name__) - getattr(logger, level)(formatted_msg) diff --git a/pokemongo_bot/event_handlers/logging_handler.py b/pokemongo_bot/event_handlers/logging_handler.py index 46e50b311f..c4c2a8f664 100644 --- a/pokemongo_bot/event_handlers/logging_handler.py +++ b/pokemongo_bot/event_handlers/logging_handler.py @@ -1,22 +1,139 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals - import logging from pokemongo_bot.event_manager import EventHandler class LoggingHandler(EventHandler): + EVENT_COLOR_MAP = { + 'api_error': 'red', + 'badges': 'blue', + 'bot_exit': 'red', + 'bot_start': 'green', + 'catch_limit': 'red', + 'catch_log': 'magenta', + 'config_error': 'red', + 'egg_already_incubating': 'yellow', + 'egg_hatched': 'green', + 'egg_hatched_fail': 'red', + 'eggs_hatched_log': 'magenta', + 'evolve_log': 'magenta', + 'future_pokemon_release': 'yellow', + 'incubate': 'green', + 'incubator_already_used': 'yellow', + 'inventory_full': 'yellow', + 'item_discard_fail': 'red', + 'item_discarded': 'green', + 'next_force_recycle': 'green', + 'force_recycle': 'green', + 'keep_best_release': 'green', + 'level_up': 'green', + 'level_up_reward': 'green', + 'location_cache_error': 'yellow', + 'location_cache_ignored': 'yellow', + 'login_failed': 'red', + 'login_log': 'magenta', + 'login_successful': 'green', + 'log_stats': 'magenta', + 'lucky_egg_error': 'red', + 'move_to_map_pokemon_encounter': 'green', + 'move_to_map_pokemon_fail': 'red', + 'next_egg_incubates': 'yellow', + 'next_sleep': 'green', + 'next_random_pause': 'green', + 'next_random_alive_pause': 'green', + 'no_pokeballs': 'red', + 'path_lap_end': 'green', + 'pokemon_appeared': 'yellow', + 'pokemon_capture_failed': 'red', + 'pokemon_caught': 'blue', + 'pokemon_evolved': 'green', + 'pokemon_fled': 'red', + 'pokemon_inventory_full': 'red', + 'pokemon_nickname_invalid': 'red', + 'pokemon_not_in_range': 'yellow', + 'pokemon_release': 'green', + 'pokemon_upgraded': 'green', + 'pokemon_vanished': 'red', + 'pokestop_empty': 'yellow', + 'pokestop_log': 'magenta', + 'pokestop_searching_too_often': 'yellow', + 'rename_pokemon': 'green', + 'show_best_pokemon': 'magenta', + 'show_inventory': 'magenta', + 'skip_evolve': 'yellow', + 'softban': 'red', + 'softban_log': 'magenta', + 'spin_limit': 'red', + 'spun_pokestop': 'cyan', + 'threw_berry_failed': 'red', + 'transfer_log': 'magenta', + 'unknown_spin_result': 'red', + 'unset_pokemon_nickname': 'red', + 'vip_pokemon': 'red', + 'use_incense': 'blue', + 'vanish_limit_reached': 'red', + + 'arrived_at_cluster': 'none', + 'arrived_at_fort': 'none', + 'bot_sleep': 'none', + 'bot_random_pause': 'none', + 'bot_random_alive_pause': 'none', + 'catchable_pokemon': 'none', + 'found_cluster': 'none', + 'incubate_try': 'none', + 'load_cached_location': 'none', + 'location_found': 'none', + 'login_started': 'none', + 'lured_pokemon_found': 'none', + 'move_to_map_pokemon_move_towards': 'none', + 'move_to_map_pokemon_teleport_back': 'none', + 'move_to_map_pokemon_updated_map': 'none', + 'moving_to_fort': 'none', + 'moving_to_lured_fort': 'none', + 'pokemon_catch_rate': 'none', + 'pokemon_evolve_fail': 'none', + 'pokestop_on_cooldown': 'none', + 'pokestop_out_of_range': 'none', + 'polyline_request': 'none', + 'position_update': 'none', + 'path_lap_update': 'none', + 'set_start_location': 'none', + 'softban_fix': 'none', + 'softban_fix_done': 'none', + 'spun_fort': 'none', + 'threw_berry': 'none', + 'threw_pokeball': 'none', + 'used_lucky_egg': 'none' + } + COLOR_CODE = { + 'gray': '\033[90m', + 'red': '\033[91m', + 'green': '\033[92m', + 'yellow': '\033[93m', + 'blue': '\033[94m', + 'magenta': '\033[95m', + 'cyan': '\033[96m', + 'white': '\033[97m', + 'none': '\033[0m' + } - def __init__(self, bot): - self.bot = bot + def __init__(self, color=True, debug=False): + self.color = color + self.debug = debug def handle_event(self, event, sender, level, formatted_msg, data): if not formatted_msg: formatted_msg = str(data) - if self.bot.config.debug: + + if self.color and event in self.EVENT_COLOR_MAP: + color = self.COLOR_CODE[self.EVENT_COLOR_MAP[event]] + formatted_msg = '{}{}{}'.format(color, formatted_msg, self.COLOR_CODE['none']) + + if self.debug: formatted_msg = '[{}] {}'.format(event, formatted_msg) logger = logging.getLogger(type(sender).__name__) - getattr(logger, level)(formatted_msg) + getattr(logger, level)(formatted_msg.encode(sys.stdout.encoding, "replace").decode("utf-8"))