<commit_before>#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2014-2015  <NAME> <<EMAIL>>
# Copyright (C) 2014-2015  <NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

'''Qubes logging routines

See also: :py:attr:`qubes.vm.qubesvm.QubesVM.log`
'''

import logging
import sys

import dbus

FORMAT_CONSOLE = '%(name)s: %(message)s'
FORMAT_LOG = '%(asctime)s %(message)s'
FORMAT_DEBUG = '%(asctime)s ' \
    '[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'

formatter_console = logging.Formatter(FORMAT_CONSOLE)
formatter_log = logging.Formatter(FORMAT_LOG)
formatter_debug = logging.Formatter(FORMAT_DEBUG)


class DBusHandler(logging.Handler):
    '''Handler which displays records as DBus notifications'''

    #: mapping of loglevels to icons
    app_icons = {
        logging.ERROR:      'dialog-error',
        logging.WARNING:    'dialog-warning',
        logging.NOTSET:     'dialog-information',
    }

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

        self._notify_object = dbus.SessionBus().get_object(
            'org.freedesktop.Notifications', '/org/freedesktop/Notifications')


    def emit(self, record):
        app_icon = self.app_icons[
            max(level for level in self.app_icons if level <= record.levelno)]

        try:
            # https://developer.gnome.org/notification-spec/#command-notify
            self._notify_object.Notify(
                'Qubes',    # STRING app_name
                0,          # UINT32 replaces_id
                app_icon,   # STRING app_icon
                record.msg, # STRING summary
                '',         # STRING body
                (),         # ARRAY actions
                {},         # DICT hints
                0,          # INT32 timeout
                dbus_interface='org.freedesktop.Notifications')
        except dbus.DBusException:
            pass


def enable():
    '''Enable global logging

    Use :py:mod:`logging` module from standard library to log messages.

    >>> import qubes.log
    >>> qubes.log.enable()          # doctest: +SKIP
    >>> import logging
    >>> logging.warning('Foobar')   # doctest: +SKIP
    '''

    if logging.root.handlers:
        return
<commit_msg>Drop log.DBusHandler<commit_after>#
# The Qubes OS Project, https://www.qubes-os.org/
#
# Copyright (C) 2014-2015  <NAME> <<EMAIL>>
# Copyright (C) 2014-2015  <NAME> <<EMAIL>>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

'''Qubes logging routines

See also: :py:attr:`qubes.vm.qubesvm.QubesVM.log`
'''

import logging
import sys

FORMAT_CONSOLE = '%(name)s: %(message)s'
FORMAT_LOG = '%(asctime)s %(message)s'
FORMAT_DEBUG = '%(asctime)s ' \
    '[%(processName)s %(module)s.%(funcName)s:%(lineno)d] %(name)s: %(message)s'

formatter_console = logging.Formatter(FORMAT_CONSOLE)
formatter_log = logging.Formatter(FORMAT_LOG)
formatter_debug = logging.Formatter(FORMAT_DEBUG)


def enable():
    '''Enable global logging

    Use :py:mod:`logging` module from standard library to log messages.

    >>> import qubes.log
    >>> qubes.log.enable()          # doctest: +SKIP
    >>> import logging
    >>> logging.warning('Foobar')   # doctest: +SKIP
    '''

    if logging.root.handlers:
        return

{'fraction_non_alphanumeric': 0.11213353306227263, 'fraction_numerical': 0.013695698694628718, 'mean_word_length': 3.121693121693122, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 39, 'https://': 3, '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': '2437831', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1356, 'n_words': 450}