# Path: lava_dispatcher/log.py
# class YAMLLogger(logging.Logger):
#     def __init__(self, name):
#         super(YAMLLogger, self).__init__(name)
#         self.handler = None
# 
#     def addZMQHandler(self, logging_url, master_cert, slave_cert, job_id, ipv6):
#         self.handler = ZMQPushHandler(logging_url, master_cert,
#                                       slave_cert, job_id, ipv6)
#         self.addHandler(self.handler)
#         return self.handler
# 
#     def close(self, linger=-1):
#         if self.handler is not None:
#             self.handler.close(linger)
#             self.removeHandler(self.handler)
#             self.handler = None
# 
#     def log_message(self, level, level_name, message, *args, **kwargs):  # pylint: disable=unused-argument
#         # Build the dictionnary
#         data = {'dt': datetime.datetime.utcnow().isoformat(),
#                 'lvl': level_name}
# 
#         if isinstance(message, str) and args:
#             data['msg'] = message % args
#         else:
#             data['msg'] = message
# 
#         # Set width to a really large value in order to always get one line.
#         # But keep this reasonable because the logs will be loaded by CLoader
#         # that is limited to around 10**7 chars
#         data_str = yaml.dump(data, default_flow_style=True,
#                              default_style='"',
#                              width=10 ** 6,
#                              Dumper=yaml.CDumper)[:-1]
#         # Test the limit and skip if the line is too long
#         if len(data_str) >= 10 ** 6:
#             if isinstance(message, str):
#                 data['msg'] = "<line way too long ...>"
#             else:
#                 data['msg'] = {"skip": "line way too long ..."}
#             data_str = yaml.dump(data, default_flow_style=True,
#                                  default_style='"',
#                                  width=10 ** 6,
#                                  Dumper=yaml.CDumper)[:-1]
#         self._log(level, data_str, ())
# 
#     def exception(self, exc, *args, **kwargs):
#         self.log_message(logging.ERROR, 'exception', exc, *args, **kwargs)
# 
#     def error(self, message, *args, **kwargs):
#         self.log_message(logging.ERROR, 'error', message, *args, **kwargs)
# 
#     def warning(self, message, *args, **kwargs):
#         self.log_message(logging.WARNING, 'warning', message, *args, **kwargs)
# 
#     def info(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'info', message, *args, **kwargs)
# 
#     def debug(self, message, *args, **kwargs):
#         self.log_message(logging.DEBUG, 'debug', message, *args, **kwargs)
# 
#     def input(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'input', message, *args, **kwargs)
# 
#     def target(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'target', message, *args, **kwargs)
# 
#     def feedback(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'feedback', message, *args, **kwargs)
# 
#     def results(self, results, *args, **kwargs):
#         if 'extra' in results and 'level' not in results:
#             raise Exception("'level' is mandatory when 'extra' is used")
#         self.log_message(logging.INFO, 'results', results, *args, **kwargs)


import os
import argparse
import logging
import subprocess
import sys
import time
import yaml
from lava_dispatcher.log import YAMLLogger
# Usage: lava_lxc_device_add.py <lxc-name> <device-node>



# Wait 10s maximum to close the socket
LINGER = 10000

# pylint: disable=line-too-long,missing-docstring


def setup_logger(options):
    # Pipeline always log as YAML so change the base logger.
    # Every calls to logging.getLogger will now return a YAMLLogger
    logging.setLoggerClass(YAMLLogger)

    # The logger can be used by the parser and the Job object in all phases.
    logger = logging.getLogger('dispatcher')
    if options.logging_url is not None:
        if options.master_cert and options.slave_cert:
            if not os.path.exists(options.master_cert) or not os.path.exists(options.slave_cert):
                return None
        # pylint: disable=no-member
        logger.addZMQHandler(options.logging_url,
                             options.master_cert,
                             options.slave_cert,
                             options.job_id,
                             options.ipv6)
    else:
        logger.addHandler(logging.StreamHandler())


# Path: lava_dispatcher/log.py
# class YAMLLogger(logging.Logger):
#     def __init__(self, name):
#         super(YAMLLogger, self).__init__(name)
#         self.handler = None
# 
#     def addZMQHandler(self, logging_url, master_cert, slave_cert, job_id, ipv6):
#         self.handler = ZMQPushHandler(logging_url, master_cert,
#                                       slave_cert, job_id, ipv6)
#         self.addHandler(self.handler)
#         return self.handler
# 
#     def close(self, linger=-1):
#         if self.handler is not None:
#             self.handler.close(linger)
#             self.removeHandler(self.handler)
#             self.handler = None
# 
#     def log_message(self, level, level_name, message, *args, **kwargs):  # pylint: disable=unused-argument
#         # Build the dictionnary
#         data = {'dt': datetime.datetime.utcnow().isoformat(),
#                 'lvl': level_name}
# 
#         if isinstance(message, str) and args:
#             data['msg'] = message % args
#         else:
#             data['msg'] = message
# 
#         # Set width to a really large value in order to always get one line.
#         # But keep this reasonable because the logs will be loaded by CLoader
#         # that is limited to around 10**7 chars
#         data_str = yaml.dump(data, default_flow_style=True,
#                              default_style='"',
#                              width=10 ** 6,
#                              Dumper=yaml.CDumper)[:-1]
#         # Test the limit and skip if the line is too long
#         if len(data_str) >= 10 ** 6:
#             if isinstance(message, str):
#                 data['msg'] = "<line way too long ...>"
#             else:
#                 data['msg'] = {"skip": "line way too long ..."}
#             data_str = yaml.dump(data, default_flow_style=True,
#                                  default_style='"',
#                                  width=10 ** 6,
#                                  Dumper=yaml.CDumper)[:-1]
#         self._log(level, data_str, ())
# 
#     def exception(self, exc, *args, **kwargs):
#         self.log_message(logging.ERROR, 'exception', exc, *args, **kwargs)
# 
#     def error(self, message, *args, **kwargs):
#         self.log_message(logging.ERROR, 'error', message, *args, **kwargs)
# 
#     def warning(self, message, *args, **kwargs):
#         self.log_message(logging.WARNING, 'warning', message, *args, **kwargs)
# 
#     def info(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'info', message, *args, **kwargs)
# 
#     def debug(self, message, *args, **kwargs):
#         self.log_message(logging.DEBUG, 'debug', message, *args, **kwargs)
# 
#     def input(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'input', message, *args, **kwargs)
# 
#     def target(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'target', message, *args, **kwargs)
# 
#     def feedback(self, message, *args, **kwargs):
#         self.log_message(logging.INFO, 'feedback', message, *args, **kwargs)
# 
#     def results(self, results, *args, **kwargs):
#         if 'extra' in results and 'level' not in results:
#             raise Exception("'level' is mandatory when 'extra' is used")
#         self.log_message(logging.INFO, 'results', results, *args, **kwargs)


# Path: share/lava_lxc_device_add.py
import os
import argparse
import logging
import subprocess
import sys
import time
import yaml
from lava_dispatcher.log import YAMLLogger

# Usage: lava_lxc_device_add.py <lxc-name> <device-node>



# Wait 10s maximum to close the socket
LINGER = 10000

# pylint: disable=line-too-long,missing-docstring


def setup_logger(options):
    # Pipeline always log as YAML so change the base logger.
    # Every calls to logging.getLogger will now return a YAMLLogger
    logging.setLoggerClass(YAMLLogger)

    # The logger can be used by the parser and the Job object in all phases.
    logger = logging.getLogger('dispatcher')
    if options.logging_url is not None:
        if options.master_cert and options.slave_cert:
            if not os.path.exists(options.master_cert) or not os.path.exists(options.slave_cert):
                return None
        # pylint: disable=no-member
        logger.addZMQHandler(options.logging_url,
                             options.master_cert,
                             options.slave_cert,
                             options.job_id,
                             options.ipv6)
    else:
        logger.addHandler(logging.StreamHandler())


    return logger