Skip to content

Commit 61fc348

Browse files
committed
Add improved logging
1 parent 4fdd3e2 commit 61fc348

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

formulate/expression.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import print_function
55

66
from .identifiers import IDs
7+
from .logging import add_logging
78

89

910
__all__ = [
@@ -237,6 +238,7 @@ def id(self):
237238
def args(self):
238239
return self._args
239240

241+
@add_logging(ignore_args=[1, 2])
240242
def to_string(self, config, constants):
241243
if isinstance(self.args[0], SingleComponent):
242244
assert len(self.args) == 1, self.args
@@ -261,6 +263,7 @@ def __str__(self):
261263
def name(self):
262264
return self._name
263265

266+
@add_logging(ignore_args=[1, 2])
264267
def to_string(self, config, constants):
265268
return self.name
266269

@@ -280,5 +283,6 @@ def __str__(self):
280283
def id(self):
281284
return self._id
282285

286+
@add_logging(ignore_args=[1, 2])
283287
def to_string(self, config, constants):
284288
return str(constants[self.id].value)

formulate/logging.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,59 @@
33
from __future__ import division
44
from __future__ import print_function
55

6+
from functools import wraps
7+
import string
68
import logging
9+
import random
710

811

912
__all__ = [
10-
'logger'
13+
'logger',
14+
'add_logging',
1115
]
1216

1317

18+
def get_identifier():
19+
return''.join(random.choice(string.ascii_uppercase + string.digits)
20+
for i in range(5))
21+
22+
23+
def add_logging(func=None, *, ignore_args=None, ignore_kwargs=None):
24+
"""Decorator to add logging to a method
25+
26+
Parameters
27+
----------
28+
func : function
29+
Function to decorate
30+
ignore_args : list
31+
Indices of the arguments to ignore
32+
ignore_kwargs : list
33+
Names of the keyword arguments to ignore
34+
"""
35+
def real_decorator(func):
36+
@wraps(func)
37+
def new_func(*args, **kwargs):
38+
my_id = get_identifier()
39+
try:
40+
func_name = func.__qualname__
41+
except AttributeError:
42+
# Python 2 doesn't have __qualname__
43+
func_name = func.__name__
44+
# Don't log arguments which should be ignored
45+
_args = [a for i, a in enumerate(args) if ignore_args and i not in ignore_args]
46+
_kwargs = {k: v for k, v in kwargs.items() if ignore_kwargs and k not in ignore_kwargs}
47+
logger.debug(my_id+' Calling '+func_name+' with '+repr(_args)+' and '+repr(_kwargs))
48+
result = func(*args, **kwargs)
49+
logger.debug(my_id+' - Got result '+repr(result))
50+
return result
51+
return new_func
52+
53+
if func is None:
54+
return real_decorator
55+
else:
56+
return real_decorator(func)
57+
58+
1459
LOGGER_NAME = 'formulate'
1560

1661
try:

formulate/parser.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
from __future__ import print_function
66

77
from collections import defaultdict
8-
from functools import wraps
98

109
import pyparsing
1110
from pyparsing import Literal, Suppress, pyparsing_common, opAssoc, Word
1211

1312
from .expression import Expression, Variable, Constant, ExpressionComponent
1413
from .identifiers import order_of_operations
15-
from .logging import logger
14+
from .logging import logger, add_logging
1615

1716

1817
__all__ = [
@@ -24,21 +23,6 @@
2423
]
2524

2625

27-
def add_logging(func):
28-
@wraps(func)
29-
def new_func(*args, **kwargs):
30-
try:
31-
func_name = func.__qualname__
32-
except AttributeError:
33-
# Python 2 doesn't have __qualname__
34-
func_name = func.__name__
35-
logger.debug('Calling '+func_name+' with '+repr(args)+' and '+repr(kwargs))
36-
result = func(*args, **kwargs)
37-
logger.debug(' - Got result '+repr(result))
38-
return result
39-
return new_func
40-
41-
4226
class PConstant(object):
4327
def __init__(self, id, value):
4428
"""Represents a constant
@@ -81,6 +65,7 @@ def get_parser(self, EXPRESSION):
8165
# TODO Detect constants?
8266
return None
8367

68+
@add_logging
8469
def to_string(self):
8570
return str(self.value)
8671

@@ -146,6 +131,7 @@ def _parse_action(self, string, location, result):
146131
# TODO Replace with logging decorator
147132
return self(*result)
148133

134+
@add_logging(ignore_args=[2, 3])
149135
def to_string(self, expression, config, constants):
150136
args = []
151137
for arg in expression.args:
@@ -225,6 +211,7 @@ def _parse_action(self, string, location, result):
225211
assert len(result) >= 2 or self._rhs_only, result
226212
return self(*result)
227213

214+
@add_logging(ignore_args=[2, 3])
228215
def to_string(self, expression, config, constants):
229216
args = []
230217
for arg in expression.args:

0 commit comments

Comments
 (0)