1#
2#  Copyright (C) 2017 Codethink Limited
3#
4#  This program is free software; you can redistribute it and/or
5#  modify it under the terms of the GNU Lesser General Public
6#  License as published by the Free Software Foundation; either
7#  version 2 of the License, or (at your option) any later version.
8#
9#  This library is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12#  Lesser General Public License for more details.
13#
14#  You should have received a copy of the GNU Lesser General Public
15#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
16#
17#  Authors:
18#        Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>
19
20import datetime
21import os
22
23
24# Types of status messages.
25#
26class MessageType():
27    DEBUG = "debug"        # Debugging message
28    STATUS = "status"      # Status message, verbose details
29    INFO = "info"          # Informative messages
30    WARN = "warning"       # Warning messages
31    ERROR = "error"        # Error messages
32    BUG = "bug"            # An unhandled exception was raised in a plugin
33    LOG = "log"            # Messages for log files _only_, never in the frontend
34
35    # Timed Messages: SUCCESS and FAIL have duration timestamps
36    START = "start"        # Status start message
37    SUCCESS = "success"    # Successful status complete message
38    FAIL = "failure"       # Failing status complete message
39    SKIPPED = "skipped"
40
41
42# Messages which should be reported regardless of whether
43# they are currently silenced or not
44unconditional_messages = [
45    MessageType.INFO,
46    MessageType.WARN,
47    MessageType.FAIL,
48    MessageType.ERROR,
49    MessageType.BUG
50]
51
52
53# Message object
54#
55class Message():
56
57    def __init__(self, unique_id, message_type, message,
58                 task_id=None,
59                 detail=None,
60                 action_name=None,
61                 elapsed=None,
62                 depth=None,
63                 logfile=None,
64                 sandbox=None,
65                 scheduler=False):
66        self.message_type = message_type  # Message type
67        self.message = message            # The message string
68        self.detail = detail              # An additional detail string
69        self.action_name = action_name    # Name of the task queue (fetch, refresh, build, etc)
70        self.elapsed = elapsed            # The elapsed time, in timed messages
71        self.depth = depth                # The depth of a timed message
72        self.logfile = logfile            # The log file path where commands took place
73        self.sandbox = sandbox            # The sandbox directory where an error occurred (if any)
74        self.pid = os.getpid()            # The process pid
75        self.unique_id = unique_id        # The plugin object ID issueing the message
76        self.task_id = task_id            # The plugin object ID of the task
77        self.scheduler = scheduler        # Whether this is a scheduler level message
78        self.creation_time = datetime.datetime.now()
79        if message_type in (MessageType.SUCCESS, MessageType.FAIL):
80            assert elapsed is not None
81