# Path: artman/utils/logger.py
# OUTPUT = 22
# SUCCESS = 25
# COLORS = {
#     'DEBUG': 'blue',
#     'INFO': 'cyan',
#     'OUTPUT': 'white',
#     'SUCCESS': 'green',
#     'WARNING': 'yellow',
#     'ERROR': 'red',
#     'CRITICAL': 'red,bold',
# }
# class Logger(logging.getLoggerClass()):
#     def __init__(self, name, level=logging.NOTSET):
#     def output(self, message, *args, **kwargs):
#     def success(self, message, *args, **kwargs):
# def setup_logging(level=logging.DEBUG):  # pragma: no cover
# def setup_logger(name, level, colors=COLORS,
#                  format_string="%(purple)s%(name)s> %(log_color)s%(message)s"):
# 
# Path: artman/utils/logger.py
# OUTPUT = 22
# SUCCESS = 25
# COLORS = {
#     'DEBUG': 'blue',
#     'INFO': 'cyan',
#     'OUTPUT': 'white',
#     'SUCCESS': 'green',
#     'WARNING': 'yellow',
#     'ERROR': 'red',
#     'CRITICAL': 'red,bold',
# }
# class Logger(logging.getLoggerClass()):
#     def __init__(self, name, level=logging.NOTSET):
#     def output(self, message, *args, **kwargs):
#     def success(self, message, *args, **kwargs):
# def setup_logging(level=logging.DEBUG):  # pragma: no cover
# def setup_logger(name, level, colors=COLORS,
#                  format_string="%(purple)s%(name)s> %(log_color)s%(message)s"):
# 
# Path: artman/utils/logger.py
# OUTPUT = 22


import logging
import subprocess
from gcloud import logging as cloud_logging
from taskflow.task import Task
from artman.utils.logger import logger as artman_logger
from artman.utils.logger import output_logger
from artman.utils.logger import OUTPUT
class TaskBase(Task):

    cloud_logger = None

    def __init__(self, *args, **kwargs):
        super(TaskBase, self).__init__(*args, **kwargs)

    def log(self, msg, logger=artman_logger, level=logging.INFO):
        """Do local logging, and optionally cloud logging.

        Args:
            msg (str): The message to be logged.
            logger (logging.Logger): The logger to use. This defaults to
                logging.getLogger('artman').
            level (int): The log level. Defaults to logging.INFO.
        """
        logger.log(level, msg)

    def exec_command(self, args):
        """ Execute command and return output.

        TODO(ethanbao): Use subprocess.Popen which is recommended."""
        try:
            self.log(' '.join(args), level=logging.DEBUG)
            output = subprocess.check_output(args, stderr=subprocess.STDOUT)
            if output:
                output = output.decode('utf8')
                self.log(output, logger=output_logger, level=OUTPUT)
            return output
        except subprocess.CalledProcessError as e:

# Path: artman/utils/logger.py
# OUTPUT = 22
# SUCCESS = 25
# COLORS = {
#     'DEBUG': 'blue',
#     'INFO': 'cyan',
#     'OUTPUT': 'white',
#     'SUCCESS': 'green',
#     'WARNING': 'yellow',
#     'ERROR': 'red',
#     'CRITICAL': 'red,bold',
# }
# class Logger(logging.getLoggerClass()):
#     def __init__(self, name, level=logging.NOTSET):
#     def output(self, message, *args, **kwargs):
#     def success(self, message, *args, **kwargs):
# def setup_logging(level=logging.DEBUG):  # pragma: no cover
# def setup_logger(name, level, colors=COLORS,
#                  format_string="%(purple)s%(name)s> %(log_color)s%(message)s"):
# 
# Path: artman/utils/logger.py
# OUTPUT = 22
# SUCCESS = 25
# COLORS = {
#     'DEBUG': 'blue',
#     'INFO': 'cyan',
#     'OUTPUT': 'white',
#     'SUCCESS': 'green',
#     'WARNING': 'yellow',
#     'ERROR': 'red',
#     'CRITICAL': 'red,bold',
# }
# class Logger(logging.getLoggerClass()):
#     def __init__(self, name, level=logging.NOTSET):
#     def output(self, message, *args, **kwargs):
#     def success(self, message, *args, **kwargs):
# def setup_logging(level=logging.DEBUG):  # pragma: no cover
# def setup_logger(name, level, colors=COLORS,
#                  format_string="%(purple)s%(name)s> %(log_color)s%(message)s"):
# 
# Path: artman/utils/logger.py
# OUTPUT = 22


# Path: artman/tasks/task_base.py
import logging
import subprocess
from gcloud import logging as cloud_logging
from taskflow.task import Task
from artman.utils.logger import logger as artman_logger
from artman.utils.logger import output_logger
from artman.utils.logger import OUTPUT

class TaskBase(Task):

    cloud_logger = None

    def __init__(self, *args, **kwargs):
        super(TaskBase, self).__init__(*args, **kwargs)

    def log(self, msg, logger=artman_logger, level=logging.INFO):
        """Do local logging, and optionally cloud logging.

        Args:
            msg (str): The message to be logged.
            logger (logging.Logger): The logger to use. This defaults to
                logging.getLogger('artman').
            level (int): The log level. Defaults to logging.INFO.
        """
        logger.log(level, msg)

    def exec_command(self, args):
        """ Execute command and return output.

        TODO(ethanbao): Use subprocess.Popen which is recommended."""
        try:
            self.log(' '.join(args), level=logging.DEBUG)
            output = subprocess.check_output(args, stderr=subprocess.STDOUT)
            if output:
                output = output.decode('utf8')
                self.log(output, logger=output_logger, level=OUTPUT)
            return output
        except subprocess.CalledProcessError as e:

            self.log(e.output.decode('utf8'), logger=output_logger,