Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorized logging #2604

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
beea535
Initial logging changes
gurupras Aug 4, 2016
8fc7dc6
Converted some more logger.log()s
gurupras Aug 4, 2016
f2b3aad
Merge branch 'logging' into colorized-logging
gurupras Aug 4, 2016
2a4c95b
Tiny spacing fix
gurupras Aug 4, 2016
53312b7
Fixed bug where old logging format was still in use
gurupras Aug 4, 2016
17d23a6
Merge branch 'dev' of github.com:PokemonGoF/PokemonGo-Bot into colori…
gurupras Aug 5, 2016
4dd21ba
Removed extraneous print as pointed out by @douglascamata
gurupras Aug 7, 2016
bdbabf2
Removed all lcd reference
gurupras Aug 7, 2016
4ba146d
Addded some comments
gurupras Aug 7, 2016
043801c
Removed logger.verbose()
gurupras Aug 7, 2016
79050bb
Removed additional prints from event_manager.py
gurupras Aug 7, 2016
9a550d3
Revert "Removed additional prints from event_manager.py"
gurupras Aug 7, 2016
17ae46b
Removed some more code related to LCD that somehow got through the fi…
gurupras Aug 7, 2016
79bf1b2
Renamed LogFormatter to ColorizedLogFormatter
gurupras Aug 7, 2016
64ef6c4
Merge branch 'dev' into colorized-logging
gurupras Aug 7, 2016
4176d7b
Merge branch 'dev' of github.com:PokemonGoF/PokemonGo-Bot into colori…
gurupras Aug 7, 2016
cbba16a
Merge branch 'dev' into colorized-logging
gurupras Aug 8, 2016
6cb0192
Fixed logging system initialization
gurupras Aug 8, 2016
babffeb
Updated EventManager() function so it more closely reflects previous …
gurupras Aug 8, 2016
681a7d1
Fixed whitespace that crept in while refactoring
gurupras Aug 8, 2016
6baa70d
Removed some of the commas-without-colors
gurupras Aug 8, 2016
ba5ab09
Removed additional initialize() method that was added to EventHandler…
gurupras Aug 8, 2016
e724c06
Refactored comments and order of initialization of LoggingHandler's _…
gurupras Aug 8, 2016
6c14602
Merge branch 'dev' of github.com:PokemonGoF/PokemonGo-Bot into colori…
gurupras Aug 9, 2016
3fa43b9
Added import of unicode_literals from future to logger.py
gurupras Aug 9, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 71 additions & 47 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,34 +121,35 @@ def _register_events(self):
'location_found',
parameters=('position', 'location')
)
self.event_manager.register_event('api_error')
self.event_manager.register_event('config_error')
self.event_manager.register_event('api_error', color='red')
self.event_manager.register_event('config_error', color='red')

self.event_manager.register_event('login_started')
self.event_manager.register_event('login_failed')
self.event_manager.register_event('login_successful')
self.event_manager.register_event('login_started', color='green')
self.event_manager.register_event('login_failed', color='red')
self.event_manager.register_event('login_successful', color='green')

self.event_manager.register_event('set_start_location')
self.event_manager.register_event('load_cached_location')
self.event_manager.register_event('location_cache_ignored')
self.event_manager.register_event('set_start_location', color='yellow')
self.event_manager.register_event('load_cached_location', color='yellow')
self.event_manager.register_event('location_cache_ignored', color='yellow')
self.event_manager.register_event(
'position_update',
parameters=(
'current_position',
'last_position',
'distance', # optional
'distance_unit' # optional
)
),
color='cyan'
)
self.event_manager.register_event('location_cache_error')
self.event_manager.register_event('location_cache_error', color='red')

self.event_manager.register_event('bot_start')
self.event_manager.register_event('bot_exit')
self.event_manager.register_event('bot_start', color='green')
self.event_manager.register_event('bot_exit', color='red')

# sleep stuff
self.event_manager.register_event(
'next_sleep',
parameters=('time',)
parameters=('time',),
)
self.event_manager.register_event(
'bot_sleep',
Expand All @@ -162,7 +163,8 @@ def _register_events(self):
'fort_id',
'latitude',
'longitude'
)
),
color='cyan'
)
self.event_manager.register_event(
'lured_pokemon_found',
Expand All @@ -172,7 +174,8 @@ def _register_events(self):
'encounter_id',
'latitude',
'longitude'
)
),
color='blue'
)
self.event_manager.register_event(
'moving_to_fort',
Expand All @@ -193,26 +196,31 @@ def _register_events(self):
'spun_pokestop',
parameters=(
'pokestop', 'exp', 'items'
)
),
color='cyan'
)
self.event_manager.register_event(
'pokestop_empty',
parameters=('pokestop',)
parameters=('pokestop',),
color='yellow'
)
self.event_manager.register_event(
'pokestop_out_of_range',
parameters=('pokestop',)
parameters=('pokestop',),
color='yellow'
)
self.event_manager.register_event(
'pokestop_on_cooldown',
parameters=('pokestop', 'minutes_left')
parameters=('pokestop', 'minutes_left'),
color='red'
)
self.event_manager.register_event(
'unknown_spin_result',
parameters=('status_code',)
parameters=('status_code',),
color='red'
)
self.event_manager.register_event('pokestop_searching_too_often')
self.event_manager.register_event('arrived_at_fort')
self.event_manager.register_event('pokestop_searching_too_often', color='red')
self.event_manager.register_event('arrived_at_fort', color='green')

# pokemon stuff
self.event_manager.register_event(
Expand All @@ -224,7 +232,8 @@ def _register_events(self):
'latitude',
'longitude',
'expiration_timestamp_ms'
)
),
color='green'
)
self.event_manager.register_event(
'pokemon_appeared',
Expand All @@ -233,7 +242,8 @@ def _register_events(self):
'cp',
'iv',
'iv_display',
)
),
color='blue'
)
self.event_manager.register_event('no_pokeballs')
self.event_manager.register_event(
Expand All @@ -251,23 +261,27 @@ def _register_events(self):
'berry_name',
'ball_name',
'new_catch_rate'
)
),
color='cyan'
)
self.event_manager.register_event(
'threw_pokeball',
parameters=(
'ball_name',
'success_percentage',
'count_left'
)
),
color='cyan'
)
self.event_manager.register_event(
'pokemon_capture_failed',
parameters=('pokemon',)
parameters=('pokemon',),
color='red'
)
self.event_manager.register_event(
'pokemon_vanished',
parameters=('pokemon',)
parameters=('pokemon',),
color='red'
)
self.event_manager.register_event('pokemon_not_in_range')
self.event_manager.register_event('pokemon_inventory_full')
Expand All @@ -276,45 +290,51 @@ def _register_events(self):
parameters=(
'pokemon',
'cp', 'iv', 'iv_display', 'exp'
)
),
color='green'
)
self.event_manager.register_event(
'pokemon_evolved',
parameters=('pokemon', 'iv', 'cp')
parameters=('pokemon', 'iv', 'cp'),
color='green'
)
self.event_manager.register_event(
'pokemon_evolve_fail',
parameters=('pokemon',)
parameters=('pokemon',),
color='red'
)
self.event_manager.register_event('skip_evolve')
self.event_manager.register_event('threw_berry_failed', parameters=('status_code',))
self.event_manager.register_event('vip_pokemon')
self.event_manager.register_event('gained_candy', parameters=('quantity', 'type'))
self.event_manager.register_event('threw_berry_failed', parameters=('status_code',), color='red')
self.event_manager.register_event('vip_pokemon', color='red')
self.event_manager.register_event('gained_candy', parameters=('quantity', 'type'), color='green')

# level up stuff
self.event_manager.register_event(
'level_up',
parameters=(
'previous_level',
'current_level'
)
),
color='cyan'
)
self.event_manager.register_event(
'level_up_reward',
parameters=('items',)
parameters=('items',),
color='cyan'
)

# lucky egg
self.event_manager.register_event(
'used_lucky_egg',
parameters=('amount_left',)
parameters=('amount_left',),
color='cyan'
)
self.event_manager.register_event('lucky_egg_error')
self.event_manager.register_event('lucky_egg_error', color='red')

# softban
self.event_manager.register_event('softban')
self.event_manager.register_event('softban_fix')
self.event_manager.register_event('softban_fix_done')
self.event_manager.register_event('softban', color='red')
self.event_manager.register_event('softban_fix', color='yellow')
self.event_manager.register_event('softban_fix_done', color='green')

# egg incubating
self.event_manager.register_event(
Expand All @@ -332,34 +352,37 @@ def _register_events(self):
'next_egg_incubates',
parameters=('distance_in_km',)
)
self.event_manager.register_event('incubator_already_used')
self.event_manager.register_event('egg_already_incubating')
self.event_manager.register_event('incubator_already_used', color='yellow')
self.event_manager.register_event('egg_already_incubating', color='yellow')
self.event_manager.register_event(
'egg_hatched',
parameters=(
'pokemon',
'cp', 'iv', 'exp', 'stardust', 'candy'
)
),
color='blue'
)

# discard item
self.event_manager.register_event(
'item_discarded',
parameters=(
'amount', 'item', 'maximum'
)
),
color='yellow'
)
self.event_manager.register_event(
'item_discard_skipped',
parameters=('space',)
)
self.event_manager.register_event(
'item_discard_fail',
parameters=('item',)
parameters=('item',),
color='red'
)

# inventory
self.event_manager.register_event('inventory_full')
self.event_manager.register_event('inventory_full', color='yellow')

# release
self.event_manager.register_event(
Expand Down Expand Up @@ -575,6 +598,7 @@ def _setup_logging(self):
level=log_level,
format='%(asctime)s [%(name)10s] [%(levelname)s] %(message)s'
)

def check_session(self, position):
# Check session expiry
if self.api._auth_provider and self.api._auth_provider._ticket_expire:
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/cell_workers/sleep_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SleepSchedule(BaseTask):
SCHEDULING_MARGIN = timedelta(minutes=10) # Skip if next sleep is RESCHEDULING_MARGIN from now

def initialize(self):
# self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))
# self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',), color='cyan')
self._process_config()
self._schedule_next_sleep()

Expand Down
26 changes: 22 additions & 4 deletions pokemongo_bot/event_handlers/logging_handler.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


import sys
import logging

from pokemongo_bot import logger
from pokemongo_bot.event_manager import EventHandler


class LoggingHandler(EventHandler):

def handle_event(self, event, sender, level, formatted_msg, data):
def __init__(self):
# We call super constructor just to play it safe
EventHandler.__init__(self)

root_logger = logging.getLogger()
# We need to now essentially override basicConfig
root_logger.handlers = []

# We now need to set up the stream/file handler
#XXX: Should this be hard-coded to stdout?
stream_handler = logging.StreamHandler(sys.stdout)

# We're done with the basic **** and now turn on
# the oh-so-pretty color logging
stream_handler.setFormatter(logger.ColorizedLogFormatter())
root_logger.addHandler(stream_handler)

def handle_event(self, event, sender, level, formatted_msg, data, **kwargs):
logger = logging.getLogger(type(sender).__name__)
if formatted_msg:
message = "[{}] {}".format(event, formatted_msg)
else:
message = '{}: {}'.format(event, str(data))
getattr(logger, level)(message)
color = kwargs.get('color', 'white')
logger.colorized(level, message, color)
15 changes: 9 additions & 6 deletions pokemongo_bot/event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class EventManager(object):

def __init__(self, *handlers):
self._registered_events = dict()
self._handlers = handlers or []
self._handlers = []
if handlers:
for handler in handlers:
self.add_handler(handler)

def event_report(self):
for event, parameters in self._registered_events.iteritems():
for event, (color, parameters) in self._registered_events.iteritems():
print '-'*80
print 'Event: {}'.format(event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still this print and the print above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think those were added by me. But sure, I'll remove them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh shit, my bad... don't remove those, here it's the event's report... they should be here

if parameters:
Expand All @@ -37,8 +40,8 @@ def event_report(self):
def add_handler(self, event_handler):
self._handlers.append(event_handler)

def register_event(self, name, parameters=[]):
self._registered_events[name] = parameters
def register_event(self, name, color='white', parameters=[]):
self._registered_events[name] = (color, parameters)

def emit(self, event, sender=None, level='info', formatted='', data={}):
if not sender:
Expand All @@ -52,7 +55,7 @@ def emit(self, event, sender=None, level='info', formatted='', data={}):
raise EventNotRegisteredException("Event %s not registered..." % event)

# verify params match event
parameters = self._registered_events[event]
color, parameters = self._registered_events[event]
if parameters:
for k, v in data.iteritems():
if k not in parameters:
Expand All @@ -62,4 +65,4 @@ def emit(self, event, sender=None, level='info', formatted='', data={}):

# send off to the handlers
for handler in self._handlers:
handler.handle_event(event, sender, level, formatted_msg, data)
handler.handle_event(event, sender, level, formatted_msg, data, color=color)
Loading