Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7132aae8275e4b40f71f74031b407de671b8414e (plain) (blame)
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
# ###############################################################################
# Copyright (c) 2014 CEA LIST.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Laurent Wouters laurent.wouters@cea.fr - Initial API and implementation
#
# ###############################################################################

"""
API for console logging with log levels and colors
"""


# Log levels
LEVELS = {
    "DEBUG": 0,
    "INFO": 0,
    "WARNING": 93,
    "ERROR": 91
}

# Color codes for the console colors
COLORS = {
    "ENDC": 0,  # RESET COLOR
    "GREY70": 97,
    "RED": 91,
    "YELLOW": 93,
    "BLUE": 94,
    "PURPLE": 95,
    "GREEN": 92,
}

# Activate/Deactivate the use of console colors
USE_COLOR = False
# Command line option to activate colors
CLI_COLOR = "--color"


def log(message, level="INFO"):
    """
    Logs the given message at the given level
    :param message: The message to log
    :param level: The log level
    :return: None
    """
    print(__get_formatted(message, level))


def __get_formatted(message, level):
    """
    Gets the formatted string for the given message and level
    :param message: The message to format
    :param level: The log level
    :return: The formatted message string
    """
    if USE_COLOR and LEVELS[level] > 0:
        return __termcode(LEVELS[level]) + "[" + level + "] " + message + __termcode(0)
    else:
        return "[" + level + "] " + message


def __termcode(num):
    """
    Gets the string for the given console color
    :param num: A console color
    :return: The terminal string for the specified color
    """
    return "\033[%sm" % num

Back to the top