<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2014-2018 khalim19
#
# 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.

"""
This module:
* provides logging of unhandled exceptions and debug information (if
debugging is enabled),
* defines a class to duplicate ("tee") standard output or error output.
"""

# NOTE: In order to allow logging errors as early as possible (before plug-in
# initialization):
# * we are breaking the "all imports at the beginning of module" convention
#   for some modules,
# * the `future` library is not imported in case some modules in the library are
#   not available in the installed Python distribution and would thus cause an
#   `ImportError` to be raised.

from __future__ import absolute_import, division, print_function, unicode_literals

str = unicode

import datetime
import io
import os
import sys
import traceback

from . import _pgpath_dirs

#===============================================================================

_LOG_MODES = (
  LOG_NONE, LOG_EXCEPTIONS_ONLY, LOG_OUTPUT_FILES, LOG_OUTPUT_GIMP_CONSOLE) = (0, 1, 2, 3)

_exception_logger = None


def log_output(
      log_mode, log_dirpaths, log_stdout_filename, log_stderr_filename,
      log_header_title="", gimp_console_message_delay_milliseconds=0):
  """
  Enable logging of output.
  
  Parameters:
  
  * `log_mode` - log mode. Possible values:
  
    * LOG_EXCEPTIONS_ONLY - only log exceptions to the error log file.
    * LOG_OUTPUT_FILES - redirect stdout and stderr to log files.
    * LOG_OUTPUT_GIMP_CONSOLE - redirect stdout and stderr to the GIMP error
      console.
  
  * `log_dirpaths` - list of directory paths for log files. If the first
    path is invalid or permission to write is denied, subsequent directories are
    used. For `LOG_OUTPUT_GIMP_CONSOLE` mode, this parameter has no effect.
  
  * `log_stdout_filename` - filename of the log file to write standard output
    to. Applies to `LOG_OUTPUT_FILES` mode only.
  
  * `log_stderr_filename` - filename of the log file to write error output to.<commit_msg>pglogging: Update documentation for `log_output`<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2014-2018 khalim19
#
# 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.

"""
This module:
* provides logging of unhandled exceptions and debug information (if
debugging is enabled),
* defines a class to duplicate ("tee") standard output or error output.
"""

# NOTE: In order to allow logging errors as early as possible (before plug-in
# initialization):
# * we are breaking the "all imports at the beginning of module" convention
#   for some modules,
# * the `future` library is not imported in case some modules in the library are
#   not available in the installed Python distribution and would thus cause an
#   `ImportError` to be raised.

from __future__ import absolute_import, division, print_function, unicode_literals

str = unicode

import datetime
import io
import os
import sys
import traceback

from . import _pgpath_dirs

#===============================================================================

_LOG_MODES = (
  LOG_NONE, LOG_EXCEPTIONS_ONLY, LOG_OUTPUT_FILES, LOG_OUTPUT_GIMP_CONSOLE) = (0, 1, 2, 3)

_exception_logger = None


def log_output(
      log_mode, log_dirpaths, log_stdout_filename, log_stderr_filename,
      log_header_title="", gimp_console_message_delay_milliseconds=0):
  """
  Enable logging of output.
  
  Parameters:
  
  * `log_mode` - log mode. Possible values:
    
    * LOG_NONE - do not log anything.
    * LOG_EXCEPTIONS_ONLY - only log exceptions to the error log file.
    * LOG_OUTPUT_FILES - redirect stdout and stderr to log files.
    * LOG_OUTPUT_GIMP_CONSOLE - redirect stdout and stderr to the GIMP error
      console.
  
  * `log_dirpaths` - list of directory paths for log files. If the first
    path is invalid or permission to write is denied, subsequent directories are
    used. For `LOG_OUTPUT_GIMP_CONSOLE` mode, this parameter has no effect.
  
  * `log_stdout_filename` - filename of the log file to write standard output
    to. Applies to `LOG_OUTPUT_FILES` mode only.
  
  * `log_stderr_filename` - filename of the log file to write error output to.
{'fraction_non_alphanumeric': 0.09623188405797102, 'fraction_numerical': 0.007729468599033816, 'mean_word_length': 4.145129224652088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2747200', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1415, 'n_words': 620}