Skip to content

Commit 0c62393

Browse files
committed
Improve the logging system
This creates a `get_logger` function which sets up a standard, better-looking logging format without fuss. To log from an entity, we now do self.log() (or self.logger.debug()).
1 parent 05f990f commit 0c62393

11 files changed

Lines changed: 105 additions & 84 deletions

File tree

‎fireplace/actions.py‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import logging
21
from enum import IntEnum
32
from .dsl import LazyNum, Picker, Selector
43
from .enums import CardType, Mulligan, PowSubType, Zone
54
from .entity import Entity
5+
from .utils import get_logger
6+
7+
8+
logger = get_logger("fireplace.Action")
69

710

811
def _eval_card(source, card):
@@ -76,7 +79,7 @@ def _broadcast(self, entity, source, at, *args):
7679
if event.at != at:
7780
continue
7881
if isinstance(event.trigger, self.__class__) and event.trigger.matches(entity, args):
79-
logging.info("%r triggers off %r from %r", entity, self, source)
82+
logger.info("%r triggers off %r from %r", entity, self, source)
8083
entity.trigger_event(source, event, args)
8184

8285
def broadcast(self, source, at, *args):
@@ -129,7 +132,7 @@ def do(self, source, attacker, defender):
129132
defender.defending = True
130133
source.game.proposed_attacker = attacker
131134
source.game.proposed_defender = defender
132-
logging.info("%r attacks %r", attacker, defender)
135+
logger.info("%r attacks %r", attacker, defender)
133136
self.broadcast(source, EventListener.ON, attacker, defender)
134137
source.game._attack()
135138

@@ -165,7 +168,7 @@ class Args(Action.Args):
165168
ENTITY = 0
166169

167170
def do(self, source, target):
168-
logging.info("Processing Death for %r", target)
171+
logger.info("Processing Death for %r", target)
169172
self.broadcast(source, EventListener.ON, target)
170173
if target.deathrattles:
171174
source.game.queue_actions(source, [Deathrattle(target)])
@@ -238,7 +241,7 @@ def do(self, source, player, card, target=None, choose=None):
238241
if choose is not None:
239242
# Choose One cards replace the action on the played card
240243
chosen = player.card(choose)
241-
logging.info("Choose One from %r: %r", card, chosen)
244+
logger.info("Choose One from %r: %r", card, chosen)
242245
if chosen.has_target():
243246
chosen.target = target
244247
card.chosen = chosen
@@ -332,7 +335,7 @@ def trigger(self, source):
332335
targets = self.get_targets(source, args[0])
333336
args = args[1:]
334337
source.game.manager.action(self.type, source, targets, *args)
335-
logging.info("%r triggering %r targeting %r", source, self, targets)
338+
logger.info("%r triggering %r targeting %r", source, self, targets)
336339
for target in targets:
337340
target_args = self.get_target_args(source, target)
338341
ret.append(self.do(source, target, *target_args))
@@ -396,7 +399,7 @@ def do(self, source, target):
396399
source.game.queue_actions(target, actions)
397400

398401
if target.controller.extra_deathrattles:
399-
logging.info("Triggering deathrattles for %r again", target)
402+
logger.info("Triggering deathrattles for %r again", target)
400403
source.game.queue_actions(target, actions)
401404

402405

@@ -499,11 +502,11 @@ class Args(Action.Args):
499502
CARDS = 1
500503

501504
def do(self, source, target, cards):
502-
logging.debug("Giving %r to %s", cards, target)
505+
logger.info("Giving %r to %s", cards, target)
503506
ret = []
504507
for card in cards:
505508
if len(target.hand) >= target.max_hand_size:
506-
logging.info("Give(%r) fails because %r's hand is full", card, target)
509+
logger.info("Give(%r) fails because %r's hand is full", card, target)
507510
continue
508511
card.controller = target
509512
card.zone = Zone.HAND
@@ -544,7 +547,7 @@ def do(self, source, target, amount):
544547
amount = min(amount, target.damage)
545548
if amount:
546549
# Undamaged targets do not receive heals
547-
logging.info("%r heals %r for %i", source, target, amount)
550+
logger.info("%r heals %r for %i", source, target, amount)
548551
target.damage -= amount
549552
self.broadcast(source, EventListener.ON, target, amount)
550553

@@ -589,7 +592,7 @@ def get_target_args(self, source, target):
589592
return (card, )
590593

591594
def do(self, source, target, card):
592-
logging.info("Morphing %r into %r", target, card)
595+
logger.info("Morphing %r into %r", target, card)
593596
target.clear_buffs()
594597
target.zone = Zone.SETASIDE
595598
card.zone = Zone.PLAY
@@ -621,7 +624,7 @@ class Reveal(TargetedAction):
621624
Reveal secret targets.
622625
"""
623626
def do(self, source, target):
624-
logging.info("Revealing secret %r", target)
627+
logger.info("Revealing secret %r", target)
625628
self.broadcast(source, EventListener.ON, target)
626629
target.zone = Zone.GRAVEYARD
627630

@@ -635,7 +638,7 @@ class Args(Action.Args):
635638
AMOUNT = 1
636639

637640
def do(self, source, target, amount):
638-
logging.info("Setting current health on %r to %i", target, amount)
641+
logger.info("Setting current health on %r to %i", target, amount)
639642
maxhp = target.max_health
640643
target.damage = max(0, maxhp - amount)
641644

@@ -678,7 +681,7 @@ def _broadcast(self, entity, source, at, *args):
678681
return super()._broadcast(entity, source, at, *args)
679682

680683
def do(self, source, target, cards):
681-
logging.info("%s summons %r", target, cards)
684+
logger.info("%s summons %r", target, cards)
682685
if not isinstance(cards, list):
683686
cards = [cards]
684687

@@ -704,7 +707,7 @@ class Args(Action.Args):
704707
CARDS = 1
705708

706709
def do(self, source, target, cards):
707-
logging.info("%r shuffles into %s's deck", cards, target)
710+
logger.info("%r shuffles into %s's deck", cards, target)
708711
if not isinstance(cards, list):
709712
cards = [cards]
710713

@@ -744,7 +747,7 @@ class Steal(TargetedAction):
744747
The controller is the controller of the source of the action.
745748
"""
746749
def do(self, source, target):
747-
logging.info("%s takes control of %r", self, target)
750+
logger.info("%s takes control of %r", self, target)
748751
zone = target.zone
749752
target.zone = Zone.SETASIDE
750753
target.controller = source.controller
@@ -756,6 +759,6 @@ class UnlockOverload(TargetedAction):
756759
Unlock the target player's overload, both current and owed.
757760
"""
758761
def do(self, source, target):
759-
logging.info("%s overload gets cleared", target)
762+
logger.info("%s overload gets cleared", target)
760763
target.overloaded = 0
761764
target.overload_locked = 0

‎fireplace/aura.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import logging
2-
from .utils import CardList
1+
from .utils import CardList, get_logger
2+
3+
4+
logger = get_logger("fireplace.Aura")
35

46

57
class Aura:
@@ -30,7 +32,7 @@ def targets(self):
3032
return CardList(self.selector.eval(self.source.game, self.source))
3133

3234
def summon(self):
33-
logging.info("Summoning Aura %r", self)
35+
logger.info("Summoning Aura %r", self)
3436
self.source.auras.append(self)
3537
self.source.game.auras.append(self)
3638
self.source.game.refresh_auras()
@@ -65,7 +67,7 @@ def update(self):
6567
self._buffed.remove(target)
6668

6769
def destroy(self):
68-
logging.info("Removing %r affecting %r", self, self._buffed)
70+
logger.info("Removing %r affecting %r", self, self._buffed)
6971
self.source.game.auras.remove(self)
7072
for buff in self._buffs[:]:
7173
buff.destroy()

‎fireplace/card.py‎

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
from itertools import chain
32
from . import cards as CardDB, rules
43
from .actions import Damage, Deaths, Destroy, Heal, Morph, Play, Shuffle, SetCurrentHealth
@@ -79,7 +78,7 @@ def zone(self, value):
7978
def _set_zone(self, value):
8079
old = self.zone
8180
assert old != value
82-
logging.debug("%r moves from %r to %r" % (self, old, value))
81+
self.logger.debug("%r moves from %r to %r" % (self, old, value))
8382
caches = {
8483
Zone.HAND: self.controller.hand,
8584
Zone.DECK: self.controller.deck,
@@ -183,24 +182,24 @@ def _set_zone(self, zone):
183182

184183
def action(self):
185184
if self.cant_play:
186-
logging.info("%r play action cannot continue", self)
185+
self.log("%r play action cannot continue", self)
187186
return
188187

189188
kwargs = {}
190189
if self.target:
191190
kwargs["target"] = self.target
192191
elif self.has_target():
193-
logging.info("%r has no target, action exits early" % (self))
192+
self.log("%r has no target, action exits early", self)
194193
return
195194

196195
if self.has_combo and self.controller.combo:
197-
logging.info("Activating %r combo targeting %r" % (self, self.target))
196+
self.log("Activating %r combo targeting %r", self, self.target)
198197
actions = self.data.scripts.combo
199198
elif hasattr(self.data.scripts, "play"):
200-
logging.info("Activating %r action targeting %r" % (self, self.target))
199+
self.log("Activating %r action targeting %r", self, self.target)
201200
actions = self.data.scripts.play
202201
elif self.choose:
203-
logging.info("Activating %r Choose One: %r", self, self.chosen)
202+
self.log("Activating %r Choose One: %r", self, self.chosen)
204203
actions = self.chosen.data.scripts.play
205204
else:
206205
actions = []
@@ -215,12 +214,12 @@ def action(self):
215214
self.game.process_deaths()
216215

217216
if self.overload:
218-
logging.info("%r overloads %s for %i", self, self.controller, self.overload)
217+
self.log("%r overloads %s for %i", self, self.controller, self.overload)
219218
self.controller.overloaded += self.overload
220219

221220
def clear_buffs(self):
222221
if self.buffs:
223-
logging.info("Clearing buffs from %r" % (self))
222+
self.log("Clearing buffs from %r", self)
224223
for buff in self.buffs[:]:
225224
buff.destroy()
226225

@@ -234,21 +233,21 @@ def _destroy(self):
234233
be moved to the GRAVEYARD on the next Death event.
235234
"""
236235
if self.zone == Zone.PLAY:
237-
logging.info("Marking %r for imminent death", self)
236+
self.log("Marking %r for imminent death", self)
238237
self.to_be_destroyed = True
239238
else:
240239
self.zone = Zone.GRAVEYARD
241240

242241
def discard(self):
243-
logging.info("Discarding %r" % (self))
242+
self.log("Discarding %r" % (self))
244243
self.zone = Zone.DISCARD
245244

246245
def draw(self):
247246
if len(self.controller.hand) >= self.controller.max_hand_size:
248-
logging.info("%s overdraws and loses %r!", self.controller, self)
247+
self.log("%s overdraws and loses %r!", self.controller, self)
249248
self.discard()
250249
else:
251-
logging.info("%s draws %r", self.controller, self)
250+
self.log("%s draws %r", self.controller, self)
252251
self.zone = Zone.HAND
253252
self.controller.cards_drawn_this_turn += 1
254253

@@ -430,15 +429,9 @@ def damage(self):
430429
def damage(self, amount):
431430
amount = max(0, amount)
432431
dmg = self.damage
433-
if amount < dmg:
434-
logging.info("%r healed for %i health" % (self, dmg - amount))
435-
elif amount == dmg:
436-
logging.info("%r receives a no-op health change" % (self))
437-
else:
438-
logging.info("%r damaged for %i health" % (self, amount - dmg))
439432

440433
if self.min_health:
441-
logging.info("%r has HEALTH_MINIMUM of %i", self, self.min_health)
434+
self.log("%r has HEALTH_MINIMUM of %i", self, self.min_health)
442435
amount = min(amount, self.max_health - self.min_health)
443436

444437
self._damage = amount
@@ -449,7 +442,7 @@ def health(self):
449442

450443
def _hit(self, source, amount):
451444
if self.immune:
452-
logging.info("%r is immune to %i damage from %r", self, amount, source)
445+
self.log("%r is immune to %i damage from %r", self, amount, source)
453446
return 0
454447
self.damage += amount
455448
return amount
@@ -576,17 +569,17 @@ def _set_zone(self, value):
576569
self.controller.field.append(self)
577570

578571
if self.zone == Zone.PLAY:
579-
logging.info("%r is removed from the field" % (self))
572+
self.log("%r is removed from the field", self)
580573
self.controller.field.remove(self)
581574
if self.damage:
582575
self.damage = 0
583576

584577
super()._set_zone(value)
585578

586579
def bounce(self):
587-
logging.info("%r is bounced back to %s's hand" % (self, self.controller))
580+
self.log("%r is bounced back to %s's hand", self, self.controller)
588581
if len(self.controller.hand) == self.controller.max_hand_size:
589-
logging.info("%s's hand is full and bounce fails" % (self.controller))
582+
self.log("%s's hand is full and bounce fails", self.controller)
590583
self.destroy()
591584
else:
592585
self.zone = Zone.HAND
@@ -599,7 +592,7 @@ def hit(self, target, amount):
599592
def _hit(self, source, amount):
600593
if self.divine_shield:
601594
self.divine_shield = False
602-
logging.info("%r's divine shield prevents %i damage.", self, amount)
595+
self.log("%r's divine shield prevents %i damage.", self, amount)
603596
return
604597

605598
return super()._hit(source, amount)
@@ -614,7 +607,7 @@ def is_playable(self):
614607
return playable
615608

616609
def silence(self):
617-
logging.info("%r has been silenced" % (self))
610+
self.log("Silencing %r", self)
618611
for aura in self.auras:
619612
aura.to_be_destroyed = True
620613
self.clear_buffs()
@@ -692,20 +685,20 @@ def _set_zone(self, zone):
692685
super()._set_zone(zone)
693686

694687
def apply(self, target):
695-
logging.info("Applying %r to %r" % (self, target))
688+
self.log("Applying %r to %r", self, target)
696689
self.owner = target
697690
if self.attack_health_swap:
698691
self._swapped_atk = target.health
699692
self._swapped_health = target.atk
700693
if hasattr(self.data.scripts, "apply"):
701694
self.data.scripts.apply(self, target)
702695
if hasattr(self.data.scripts, "max_health") or self.attack_health_swap:
703-
logging.info("%r removes all damage from %r", self, target)
696+
self.log("%r removes all damage from %r", self, target)
704697
target.damage = 0
705698
self.zone = Zone.PLAY
706699

707700
def destroy(self):
708-
logging.info("Destroying buff %r from %r" % (self, self.owner))
701+
self.log("Destroying buff %r from %r", self, self.owner)
709702
if hasattr(self.data.scripts, "destroy"):
710703
self.data.scripts.destroy(self)
711704
self.zone = Zone.REMOVEDFROMGAME
@@ -810,7 +803,7 @@ def play(self, target=None):
810803

811804
def use(self, target=None):
812805
assert self.is_usable()
813-
logging.info("%s uses hero power %r on %r", self.controller, self, target)
806+
self.log("%s uses hero power %r on %r", self.controller, self, target)
814807

815808
if self.has_target():
816809
assert target

‎fireplace/dsl/evaluator.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import copy
2-
import logging
2+
from ..utils import get_logger
3+
4+
5+
logger = get_logger("fireplace.DSL")
36

47

58
class Evaluator:
@@ -81,5 +84,5 @@ def evaluate(self, source):
8184
t1 = self.selector1.eval(source.game, source)
8285
t2 = self.selector2.eval(source.game, source)
8386
diff = sum(t.cost for t in t1) - sum(t.cost for t in t2)
84-
logging.info("Jousting %r vs %r -> %i difference", t1, t2, diff)
87+
logger.info("Jousting %r vs %r -> %i difference", t1, t2, diff)
8588
return diff > 0

0 commit comments

Comments
 (0)