-
Notifications
You must be signed in to change notification settings - Fork 108
/
logger.py
147 lines (124 loc) · 5.42 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""A note-taker module to write experiment logs and result files. It attaches
extra key info to logs and results (such as trial ID, function signature,
project) to help identify log during debugging and result tracking."""
import json
import logging
import os
from typing import Mapping
from results import Result
FINAL_RESULT_JSON = 'result.json'
_trial_logger = None
class CustomLoggerAdapter(logging.LoggerAdapter):
"""A note-taker to log and record experiment status, key info, and final
results."""
def process(self, msg, kwargs):
# Combine 'extra' dictionaries and modify the message
kwargs['extra'] = {**(self.extra or {}), **(kwargs.get('extra') or {})}
return msg, kwargs
def write_to_file(self, file_path: str, file_content: str) -> None:
"""Writes the |file_content| into a local |file_path|."""
with open(file_path, 'w') as file:
file.writelines(file_content)
def write_fuzz_target(self, result: Result) -> None:
"""Writes fuzz target."""
fuzz_target_path = os.path.join(result.work_dirs.fuzz_targets,
f'{result.trial:02d}.fuzz_target')
self.write_to_file(fuzz_target_path, result.fuzz_target_source)
def write_build_script(self, result: Result) -> None:
"""Writes build script."""
build_script_path = os.path.join(result.work_dirs.fuzz_targets,
f'{result.trial:02d}.build_script')
self.write_to_file(build_script_path, result.build_script_source)
def write_result(self, result_status_dir: str, result: Result) -> None:
"""Writes the final result into JSON for report generation."""
trial_result_dir = os.path.join(result_status_dir, f'{result.trial:02d}')
os.makedirs(trial_result_dir, exist_ok=True)
with open(os.path.join(trial_result_dir, FINAL_RESULT_JSON), 'w') as f:
json.dump(result.to_dict(), f)
def write_chat_history(self, result: Result) -> None:
"""Writes fuzz target."""
# TODO(dongge): Find a proper way to write this.
trial_result_dir = os.path.join(result.work_dirs.status,
f'{result.trial:02d}')
os.makedirs(trial_result_dir, exist_ok=True)
chat_history_path = os.path.join(trial_result_dir, 'log.txt')
chat_history = '\n'.join(
f'{agent_name}\n{chat_history}\n'
for agent_name, chat_history in result.chat_history.items())
self.write_to_file(chat_history_path, chat_history)
def debug(msg: object,
*args: object,
exc_info=None,
stack_info: bool = False,
stacklevel: int = 1,
extra: Mapping[str, object] | None = None,
**kwargs: object) -> None:
return get_trial_logger().debug(msg,
*args,
exc_info=exc_info,
stack_info=stack_info,
stacklevel=stacklevel,
extra=extra,
**kwargs)
def info(msg: object,
*args: object,
exc_info=None,
stack_info: bool = False,
stacklevel: int = 1,
extra: Mapping[str, object] | None = None,
**kwargs: object) -> None:
return get_trial_logger().info(msg,
*args,
exc_info=exc_info,
stack_info=stack_info,
stacklevel=stacklevel,
extra=extra,
**kwargs)
def warning(msg: object,
*args: object,
exc_info=None,
stack_info: bool = False,
stacklevel: int = 1,
extra: Mapping[str, object] | None = None,
**kwargs: object) -> None:
return get_trial_logger().warning(msg,
*args,
exc_info=exc_info,
stack_info=stack_info,
stacklevel=stacklevel,
extra=extra,
**kwargs)
def error(msg: object,
*args: object,
exc_info=None,
stack_info: bool = False,
stacklevel: int = 1,
extra: Mapping[str, object] | None = None,
**kwargs: object) -> None:
return get_trial_logger().error(msg,
*args,
exc_info=exc_info,
stack_info=stack_info,
stacklevel=stacklevel,
extra=extra,
**kwargs)
def get_trial_logger(name: str = __name__,
trial: int = 0,
level=logging.DEBUG) -> CustomLoggerAdapter:
"""Sets up or retrieves the singleton instance of CustomLoggerAdapter."""
global _trial_logger
if _trial_logger:
return _trial_logger
logger = logging.getLogger(name)
if not logger.handlers:
formatter = logging.Formatter(
fmt=('%(asctime)s [Trial ID: %(trial)02d] %(levelname)s '
'[%(module)s.%(funcName)s]: %(message)s'),
datefmt='%Y-%m-%d %H:%M:%S')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(level)
logger.propagate = False
_trial_logger = CustomLoggerAdapter(logger, {'trial': trial})
return _trial_logger