Index: DialityCoreCanProtocol.py =================================================================== diff -u -rf1a95e42324e7f6da895039e4863db6af49bf0d2 -rb93c9ea734481269f9c7423d15d42d6bf8e2f85a --- DialityCoreCanProtocol.py (.../DialityCoreCanProtocol.py) (revision f1a95e42324e7f6da895039e4863db6af49bf0d2) +++ DialityCoreCanProtocol.py (.../DialityCoreCanProtocol.py) (revision b93c9ea734481269f9c7423d15d42d6bf8e2f85a) @@ -22,6 +22,7 @@ from time import sleep import sys import argparse +from argparse import RawTextHelpFormatter import logging @@ -341,7 +342,7 @@ START_BYTE = DenaliMessage.START_BYTE DIALIN_MSG_RESP_TO = 0.1 # number of seconds to wait for a response to a sent command - def __init__(self, can_interface='can0', log_level="debug"): + def __init__(self, can_interface='can0', log_level="error"): """ DenaliCanMessenger constructor @@ -368,7 +369,7 @@ self.run = False self.sync_response_dictionary = {} - numeric_level = getattr(logging, log_level.upper(), None) + numeric_level = getattr(logging, log_level.upper(), logging.ERROR) logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', filename="DialIn.log", datefmt='%m-%d-%Y:%H:%M:%S', @@ -424,7 +425,8 @@ channel_id = message.arbitration_id message_length = can_data[DenaliMessage.PAYLOAD_LENGTH_INDEX] - print_and_log(str(time.time()) + " " + str(channel_id) + " " + str(can_data), log_level="info") + print_and_log(str(time.time()) + " " + str(channel_id) + " " + str(can_data), + log_level=logging.INFO) # if we are building a long message, then proceed to push it to the channel dictionary if channel_id in self.long_msg_channel_id_set: @@ -548,8 +550,7 @@ data=packet, is_extended_id=False) - self.logger.debug(packet) - # input("Press enter to send...") + print_and_log(packet) self.bus.send(packet, 0) # 0.1) # Sending @@ -576,16 +577,19 @@ return self.command_response_message -def print_and_log(message, log_level="debug"): +def print_and_log(message, log_level=logging.DEBUG): """ - Prints and logs a message + Prints a message if its severity is >= the current log level. + Also logs the message. \param message: The message to print and log \param log_level: The logging level, indicates the severity of the message \returns: None """ - print(message) + if logging.getLogger().getEffectiveLevel() <= log_level: + print(message) + if log_level == "debug": logging.debug(message) elif log_level == "info": @@ -597,53 +601,52 @@ elif log_level == "critical": logging.critical(message) - -def tst_print_received_packet(message, sync=False): +def test_print_received_packet(self, message, sync=False): channel_id = message[0] message[0] = DenaliMessage.START_BYTE introduction = "Received: " if sync: introduction = "Sync " + introduction - print_and_log(introduction, message, " in channel: ", channel_id) + print_and_log("{0} {1} in channel: {3}".format(introduction, message, channel_id)) -def tst_print_to_screen(message): +def test_print_to_screen(message): if message is None: print_and_log("Timeout!!!") else: - tst_print_received_packet(message) + test_print_received_packet(message) -def tst_function_for_sync(message): - tst_print_received_packet(message, sync=True) +def test_function_for_sync(message): + test_print_received_packet(message, sync=True) -def tst_print_sending_dg_board(message): - print_and_log("Sending to board: ", message['message'], " in channel: ", message['channel_id']) +def test_print_sending_dg_board(message): + print_and_log("Sending to board: {0} on channel: {1}".format(message['message'], message['channel_id'])) -def tst_print_sending_dg_sim(message): - print_and_log("Sending to DG simulator: ", message['message'], " in channel", message['channel_id'], end=" ---> ") +def test_print_sending_dg_sim(message): + print_and_log("Sending to DG simulator: {0} on channel {1}".format(message["message"], message["channel_id"])) -def tst_dg(log_level): +def test_dg(log_level): test_messenger = DenaliCanMessenger(log_level=log_level) test_received_channel_id = DenaliChannels.hd_to_ui_ch_id test_received_message_id = 0x100 test_messenger.register_receiving_publication_function(test_received_channel_id, test_received_message_id, - tst_function_for_sync) + test_function_for_sync) test_dg_simulator_received_channel_id = DenaliChannels.dialin_to_dg_ch_id test_dg_simulator_sync_msg_id = 0x05 test_dg_simulator_msg_id = 0x03 test_messenger.register_receiving_publication_function(test_dg_simulator_received_channel_id, test_dg_simulator_sync_msg_id, - tst_function_for_sync) + test_function_for_sync) test_messenger.start() @@ -655,39 +658,42 @@ payload=[]) sleep(3.0) - tst_print_sending_dg_board(test_msg) + test_print_sending_dg_board(test_msg) test_response = test_messenger.send(test_msg) - tst_print_to_screen(test_response) + test_print_to_screen(test_response) sleep(3.0) - tst_print_sending_dg_board(test_msg) + test_print_sending_dg_board(test_msg) test_response = test_messenger.send(test_msg) - tst_print_to_screen(test_response) + test_print_to_screen(test_response) sleep(3.0) - tst_print_sending_dg_sim(test_dg_msg) + test_print_sending_dg_sim(test_dg_msg) test_response = test_messenger.send(test_dg_msg) - tst_print_to_screen(test_response) + test_print_to_screen(test_response) sleep(3.0) - tst_print_sending_dg_sim(test_dg_msg) + test_print_sending_dg_sim(test_dg_msg) test_response = test_messenger.send(test_dg_msg) - tst_print_to_screen(test_response) + test_print_to_screen(test_response) -def tst_can(): +def test_can(self): test_messenger = DenaliCanMessenger() test_messenger.start() if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Dial-In Core Can Protocol") + parser = argparse.ArgumentParser(description="Dial-In Core Can Protocol \n" + "\tExample: \n" + "\tpython3 DialityCoreCanProtocol.py --test-dg --log-level=\"debug\"", + formatter_class=RawTextHelpFormatter) parser.add_argument("--test-dg", action="store_true") - parser.add_argument("--log-level", default="debug") + parser.add_argument("--log-level", default="error") args = parser.parse_args() if args.test_dg: - tst_dg(args.log_level) + test_dg(args.log_level) if len(sys.argv) < 2: parser.print_help()