@@ -15,25 +15,37 @@ from typing import Any
15
15
class Logger:
16
16
def __init__(self) -> None:
17
17
self.logs = ""
18
+ self.max_log_length = 3750
18
19
19
20
def print(self, *objects: Any, sep: str = " ", end: str = "\\n") -> None:
20
21
self.logs += sep.join(map(str, objects)) + end
21
22
22
23
def flush(self, state: TradingState, orders: dict[Symbol, list[Order]], conversions: int, trader_data: str) -> None:
23
- print(json.dumps ([
24
- self.compress_state(state),
24
+ base_length = len(self.to_json ([
25
+ self.compress_state(state, "" ),
25
26
self.compress_orders(orders),
26
27
conversions,
27
- trader_data,
28
- self.logs,
29
- ], cls=ProsperityEncoder, separators=(",", ":")))
28
+ "",
29
+ "",
30
+ ]))
31
+
32
+ # We truncate state.traderData, trader_data, and self.logs to the same max. length to fit the log limit
33
+ max_item_length = (self.max_log_length - base_length) // 3
34
+
35
+ print(self.to_json([
36
+ self.compress_state(state, self.truncate(state.traderData, max_item_length)),
37
+ self.compress_orders(orders),
38
+ conversions,
39
+ self.truncate(trader_data, max_item_length),
40
+ self.truncate(self.logs, max_item_length),
41
+ ]))
30
42
31
43
self.logs = ""
32
44
33
- def compress_state(self, state: TradingState) -> list[Any]:
45
+ def compress_state(self, state: TradingState, trader_data: str ) -> list[Any]:
34
46
return [
35
47
state.timestamp,
36
- state.traderData ,
48
+ trader_data ,
37
49
self.compress_listings(state.listings),
38
50
self.compress_order_depths(state.order_depths),
39
51
self.compress_trades(state.own_trades),
@@ -94,6 +106,15 @@ class Logger:
94
106
95
107
return compressed
96
108
109
+ def to_json(self, value: Any) -> str:
110
+ return json.dumps(value, cls=ProsperityEncoder, separators=(",", ":"))
111
+
112
+ def truncate(self, value: str, max_length: int) -> str:
113
+ if len(value) <= max_length:
114
+ return value
115
+
116
+ return value[:max_length - 3] + "..."
117
+
97
118
logger = Logger()
98
119
99
120
class Trader:
0 commit comments