File: gimplogger.py

package info (click to toggle)
gimp 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 222,880 kB
  • sloc: ansic: 870,914; python: 10,965; lisp: 10,857; cpp: 7,355; perl: 4,536; sh: 1,753; xml: 972; yacc: 609; lex: 348; javascript: 150; makefile: 42
file content (111 lines) | stat: -rwxr-xr-x 3,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#   GIMP - The GNU Image Manipulation Program
#   Copyright (C) 1995 Spencer Kimball and Peter Mattis
#
#   gimplogger.py
#   Copyright (C) 2021-2024 Jacob Boerema
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.

"""Logging framework for use with GIMP Python plug-ins."""

import os
import logging

import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp


class GimpLogger (object):
    def __init__(self, interactive, logfile, append=False, verbose=False, debugging=False):
        self.interactive = interactive

        self.verbose = verbose
        self.debugging = debugging
        self.enabled = True

        if debugging:
            log_level = logging.DEBUG
        else:
            log_level = logging.INFO
        if append:
            log_filemode = 'a'
        else:
            log_filemode = 'w'

        try:
            logging.basicConfig(
                filename=logfile,
                filemode=log_filemode,
                encoding='utf-8',
                level=log_level,
                format='%(asctime)s | %(name)s | %(levelname)s | %(message)s')

            logging.debug("Starting logger...")
            logging.debug("Log file: %s", os.path.abspath(logfile))
        except PermissionError:
            self.enabled = False
            msg = ("We do not have permission to create a log file at " +
                   os.path.abspath(logfile) + ". " +
                   "Please use env var GIMP_TESTS_LOG_FILE to set up a location.")
            if interactive:
                Gimp.message(msg)
            else:
                print(msg)

    def set_interactive(self, interactive):
        if self.interactive != interactive:
            self.interactive = interactive
            logging.debug("Interactive set to %s", self.interactive)

    def message(self, msg):
        logging.info(msg)
        print(msg)
        if self.interactive:
            Gimp.message(msg)

    def gimp_verbose_message(self, msg):
        if self.verbose and self.interactive:
            Gimp.message(msg)

    def info(self, msg):
        if self.verbose:
            logging.info(msg)
            print(msg)

    def consoleinfo(self, msg):
        logging.info(msg)
        print(msg)

    def warning(self, msg):
        warn_msg = 'WARNING: ' + msg
        logging.warning(msg)
        print(warn_msg)
        if self.interactive:
            Gimp.message(warn_msg)

    def error(self, msg):
        err_msg = 'ERROR: ' + msg
        logging.error(msg)
        print(err_msg)
        if self.interactive:
            Gimp.message(err_msg)

    def debug(self, msg):
        logging.debug(msg)
        if self.debugging:
            print(msg)