# Copyright 2017 The Tulsi Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Logging routines used by Tulsi scripts."""

import logging
import logging.handlers
import os


class Logger(object):
  """Tulsi specific logging."""

  def __init__(self):
    logging_dir = os.path.expanduser('~/Library/Application Support/Tulsi')
    if not os.path.exists(logging_dir):
      os.mkdir(logging_dir)

    logfile = os.path.join(logging_dir, 'build_log.txt')

    # Currently only creates a single logger called 'tulsi_logging'. If
    # additional loggers are needed, consider adding a name attribute to the
    # Logger.
    self._logger = logging.getLogger('tulsi_logging')
    self._logger.setLevel(logging.INFO)

    file_handler = logging.handlers.RotatingFileHandler(logfile, backupCount=5)
    file_handler.setLevel(logging.INFO)
    self._logger.addHandler(file_handler)

    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    self._logger.addHandler(console)

  def log_action(self, action_name, action_id, seconds):
    del action_id  # Unused by this logger.
    # Log to file and print to stdout for display in the Xcode log.
    self._logger.info('<*> %s completed in %0.3f ms',
                      action_name, seconds * 1000)

# Copyright 2017 The Tulsi Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Logging routines used by Tulsi scripts."""

import logging
import logging.handlers
import os


class Logger(object):
  """Tulsi specific logging."""

  def __init__(self):
    logging_dir = os.path.expanduser('~/Library/Application Support/Tulsi')
    if not os.path.exists(logging_dir):
      os.mkdir(logging_dir)

    logfile = os.path.join(logging_dir, 'build_log.txt')

    # Currently only creates a single logger called 'tulsi_logging'. If
    # additional loggers are needed, consider adding a name attribute to the
    # Logger.
    self._logger = logging.getLogger('tulsi_logging')
    self._logger.setLevel(logging.INFO)

    file_handler = logging.handlers.RotatingFileHandler(logfile, backupCount=20)
    file_handler.setLevel(logging.INFO)
    # Create a new log file for each build.
    file_handler.doRollover()
    self._logger.addHandler(file_handler)

    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    self._logger.addHandler(console)

  def log_action(self, action_name, action_id, seconds):
    del action_id  # Unused by this logger.
    # Log to file and print to stdout for display in the Xcode log.
    self._logger.info('<*> %s completed in %0.3f ms',
                      action_name, seconds * 1000)

Add an explicit logfile rollover at the beginning of each Tulsi bazel build.