Skip to content

Commit cc8c990

Browse files
committed
Workon Issue #22 - Use logger instead of prints
1 parent 2ca5eed commit cc8c990

7 files changed

Lines changed: 25 additions & 10 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,6 @@ dmypy.json
132132

133133
# Pyre type checker
134134
.pyre/
135+
136+
# Logs
137+
logs/*

main.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import src.automated_planner as parser
22
import argparse
3-
3+
import logging
4+
import os
45

56
def main():
67
args_parser = argparse.ArgumentParser(
@@ -11,10 +12,18 @@ def main():
1112
args_parser.add_argument("problem", type=str, help="PDDL problem file")
1213
args_parser.add_argument("-v", "--verbose", help="Increases the output's verbosity")
1314
args = args_parser.parse_args()
15+
logging.basicConfig(filename='logs/main.log', format='%(levelname)s:%(message)s', filemode='w', level=logging.INFO) # Creates the log file
1416
apla_tbx = parser.AutomatedPlanner(args.domain, args.problem)
17+
logging.info("Starting the tool")
1518
path, time = apla_tbx.depth_first_search(time_it=True)
16-
print(apla_tbx.get_actions_from_path(path))
17-
print("Computation time: %.2f" % time)
19+
logging.info(apla_tbx.get_actions_from_path(path))
20+
logging.info("Computation time: %.2f seconds" % time)
21+
logging.info("Tool finished")
22+
#Output the log (to show something in the output screen)
23+
logfile = open("logs/main.log", "r")
24+
print(logfile.read())
25+
logfile.close()
26+
1827

1928

2029
if __name__ == "__main__":

src/automated_planner.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .bfs import BreadthFirstSearch
33
from .dfs import DepthFirstSearch
44
from .dijkstra import DijkstraBestFirstSearch
5+
import logging
56

67
UI = False
78

@@ -49,7 +50,7 @@ def __retrace_path(self, node):
4950

5051
def get_actions_from_path(self, path):
5152
if not path:
52-
print("Path is empty, can't operate...")
53+
logging.warning("Path is empty, can't operate...")
5354
return []
5455
actions = []
5556
for node in path:
@@ -63,7 +64,7 @@ def get_actions_from_path(self, path):
6364

6465
def get_state_def_from_path(self, path):
6566
if not path:
66-
print("Path is empty, can't operate...")
67+
logging.warning("Path is empty, can't operate...")
6768
return []
6869
trimmed_path = []
6970
for node in path:

src/bfs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .node import Node
2-
2+
import logging
33

44
class BreadthFirstSearch:
55
def __init__(self, automated_planner):
@@ -32,5 +32,5 @@ def search(self):
3232
if child in self.visited:
3333
continue
3434
self.queue.append(child)
35-
print("!!! No path found !!!")
35+
logging.warning("!!! No path found !!!")
3636
return None

src/dfs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .node import Node
2+
import logging
23

34
class DepthFirstSearch:
45
def __init__(self, automated_planner):
@@ -31,5 +32,5 @@ def search(self):
3132
if child in self.visited:
3233
continue
3334
self.stack.append(child)
34-
print("!!! No path found !!!")
35+
logging.warning("!!! No path found !!!")
3536
return None

src/dijkstra.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from .heuristics import zero_heuristic
12
from .node import Node
3+
import logging
24
import math
3-
from .heuristics import zero_heuristic
45

56

67
class DijkstraBestFirstSearch:
@@ -64,5 +65,5 @@ def search(self):
6465
else:
6566
self.nodes[child_hash] = child
6667
self.open_nodes_n += 1
67-
print("!!! No path found !!!")
68+
logging.warning("!!! No path found !!!")
6869
return None

tests/test_dfs.py

Whitespace-only changes.

0 commit comments

Comments
 (0)