Index: cloud_sync.py =================================================================== diff -u -re2b5bba1ace2613e8cd3ca6d997756b1b61d77a4 -rae065dc96f33fc1946785ee833356dae959be2d9 --- cloud_sync.py (.../cloud_sync.py) (revision e2b5bba1ace2613e8cd3ca6d997756b1b61d77a4) +++ cloud_sync.py (.../cloud_sync.py) (revision ae065dc96f33fc1946785ee833356dae959be2d9) @@ -1,9 +1,7 @@ -"""Implementation of CloudSync controller""" - -import argparse -import logging -import socket +import os import sys +import logging +from logging.handlers import TimedRotatingFileHandler import werkzeug werkzeug.cached_property = werkzeug.utils.cached_property @@ -22,39 +20,51 @@ from cloudsync.utils.heartbeat import HeartBeatProvider from cloudsync.handlers.error import Error -VERSION = "0.4.4" +VERSION = "0.4.6" -try: - arguments = sys.argv - log_level = int(arguments[1]) +arguments = sys.argv +log_level = int(arguments[1]) - if not os.path.exists(CS_LOG_PATH): - os.makedirs(CS_LOG_PATH) +if not os.path.exists(CS_LOG_PATH): + os.makedirs(CS_LOG_PATH) - logging.basicConfig(filename=CS_LOG_FILE, - level=log_level, - format='[%(asctime)s]: %(levelname)s - %(message)s | {%(pathname)s:%(lineno)d}', - ) +app = Flask(__name__) +api = Api(app=app, version=VERSION, title="CloudSync Registration API", + description="Interface with DIA Manufacturing Tool * DCS") - app = Flask(__name__) - api = Api(app=app, version=VERSION, title="CloudSync Registration API", - description="Interface with DIA Manufacturing Tool * DCS") +# Remove existing handlers +for handler in app.logger.handlers: + app.logger.removeHandler(handler) - for handler in app.logger.handlers: - app.logger.removeHandler(handler) - handler = logging.FileHandler(CS_LOG_FILE) - handler.setLevel(log_level) - default_formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s') - handler.setFormatter(default_formatter) +# Create a TimedRotatingFileHandler that writes logs to a new file every midnight, in UTC time +handler = TimedRotatingFileHandler(CS_LOG_FILE, when="midnight", interval=1, utc=True) +handler.suffix = "%m-%d-%Y" - app.logger.setLevel(log_level) +# Set the log level +handler.setLevel(log_level) - g_utils.add_logger(app.logger) +# Add a formatter +default_formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s | {%(pathname)s:%(lineno)d}') +handler.setFormatter(default_formatter) +# Add the handler to the logger +app.logger.addHandler(handler) +app.logger.setLevel(log_level) + +# Get the root logger +root_logger = logging.getLogger() + +# Add the handlers to the root logger +root_logger.addHandler(handler) +root_logger.setLevel(log_level) + +g_utils.add_logger(app.logger) + +try: g_config = helpers_read_config(CONFIG_PATH) if g_config[CONFIG_DEVICE][CONFIG_DEVICE_MODE] == 'operation': - helpers_read_config(OPERATION_CONFIG_FILE_PATH) + g_config = helpers_read_config(OPERATION_CONFIG_FILE_PATH) CONFIG_PATH = OPERATION_CONFIG_FILE_PATH reachability_provider = ReachabilityProvider(logger=app.logger) @@ -68,10 +78,12 @@ network_request_handler = NetworkRequestHandler(logger=app.logger, max_size=1, output_channel=output_channel, reachability_provider=reachability_provider, error_handler=error_handler) - message_handler = UICSMessageHandler(logger=app.logger, max_size=20, network_request_handler=network_request_handler, + message_handler = UICSMessageHandler(logger=app.logger, max_size=20, + network_request_handler=network_request_handler, output_channel=output_channel, reachability_provider=reachability_provider, error_handler=error_handler) - ui_cs_bus = FileInputBus(logger=app.logger, file_channels_path=UI2CS_FILE_CHANNELS_PATH, input_channel_name="inp.buf", + ui_cs_bus = FileInputBus(logger=app.logger, file_channels_path=UI2CS_FILE_CHANNELS_PATH, + input_channel_name="inp.buf", g_config=g_config, message_handler=message_handler) heartbeat_provider = HeartBeatProvider(logger=app.logger, network_request_handler=network_request_handler, @@ -86,10 +98,8 @@ parser = reqparse.RequestParser() except Exception as e: - error = Error("{0},2,{1},Failed to start CS - {2}".format(OutboundMessageIDs.CS2UI_ERROR.value, - ErrorIDs.GENERIC_ERROR.value, - e)) - self.error_handler.enqueue_error(error=error) + g_utils.logger.error("Failed to start CS - {0}".format(e)) + sys.exit(0) @api.route("/version") @@ -139,8 +149,8 @@ invalid_params.append("public_key") if len(invalid_params) > 0: error = Error("{0},2,{1}, invalid credentials: {2}".format(OutboundMessageIDs.CS2UI_ERROR.value, - ErrorIDs.CS_SAVE_CREDENTIALS_ERROR.value, - invalid_params)) + ErrorIDs.CS_SAVE_CREDENTIALS_ERROR.value, + invalid_params)) error_handler.enqueue_error(error=error) return {"invalidAttributes": invalid_params}, BAD_REQUEST