Skip to content

Commit

Permalink
Added FileHandler to revised logging system
Browse files Browse the repository at this point in the history
The `FileHandler` reintroduces functionality, allowing the application
to log to remote files in the `log` folder.
  • Loading branch information
jamestaylr committed Jul 4, 2015
1 parent 72ff6e4 commit 71a43f1
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
logger = logging.getLogger('log')

def setup_logging():
logger.setLevel(logging.DEBUG)

log_path = r'logs/'
if not os.path.exists(log_path): os.makedirs(log_path)

file_handler = logging.FileHandler('logs/' + time.strftime("%Y-%m-%d %H-%M-%S") + '.log')
file_handler.setLevel(logging.DEBUG)

formatterDisplay = logging.Formatter('[%(asctime)s] %(levelname)-0s: %(message)s', '%H:%M:%S')
file_handler.setFormatter(formatterDisplay)

logger.addHandler(file_handler)

def setup_terminal_logging():
screen = curses.initscr()
screen.nodelay(1)
screen.border(0)
Expand All @@ -14,29 +28,27 @@ def setup_logging():

height = 20
# height, width, begin_y, begin_x
win = curses.newwin(height, maxx-4, maxy-(height + 1), 2)

window = curses.newwin(height, maxx-4, maxy-(height + 1), 2)

curses.setsyx(-1, -1)
screen.addstr(1,2, "SailBOT: the most advanced collegiate sailing operating system")
screen.addstr(1,2, "SailBOT")
screen.refresh()
win.refresh()
win.scrollok(True)
win.idlok(True)
win.leaveok(True)
window.refresh()
window.scrollok(True)
window.idlok(True)
window.leaveok(True)

mh = modules.log.CursesHandler(win)
terminal_handler = modules.log.CursesHandler(window)
formatterDisplay = logging.Formatter('[%(asctime)s] %(levelname)-0s: %(message)s', '%H:%M:%S')
mh.setFormatter(formatterDisplay)
logger.addHandler(mh)
logger.setLevel(logging.DEBUG)
terminal_handler.setFormatter(formatterDisplay)
logger.addHandler(terminal_handler)

def shutdown_terminal():
curses.curs_set(1)
curses.nocbreak()
curses.echo()
curses.endwin()

def getJSON(obj):
return json.dumps(obj, default=lambda o: o.__dict__, sort_keys=True, indent=4)

Expand All @@ -45,15 +57,15 @@ def setup_locations(target_locations, boundary_locations):
with open('locations.json', 'r') as myfile:
json_data = json.loads(myfile.read().replace('\n', ''))


for location in json_data['target_locations']:
target_locations.append({"latitude": location["latitude"], "longitude": location["longitude"]})
for location in json_data['boundary_locations']:
boundary_locations.append({"latitude": location["latitude"], "longitude": location["longitude"]})

logger.info("Loaded the following target locations: %s" % target_locations)
logger.info("Loaded the following boundary locations: %s" % boundary_locations)

except IOError:
logger.error('The locations JSON file could not be found!')
sys.exit()
Expand All @@ -62,10 +74,6 @@ def setup_locations(target_locations, boundary_locations):
sys.exit()

def setup_config(values):

# Logging in this method must stay as print statements because the logger
# has not been defined yet

try:
config = configparser.ConfigParser()
config.read('config.ini')
Expand Down

0 comments on commit 71a43f1

Please sign in to comment.