summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/server/CCustomLogger.py
blob: a8823cea1200092682a1ba44b59b2a12e81df9b3 (plain)
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
import sys
import os
import logging


def setup_custom_logger(name, log_path = None):
    # first make sure path availabe
#   if log_path is None:
#       log_path = os.getcwd()+'/trex_log.log'
#   else:
#       directory = os.path.dirname(log_path)
#       if not os.path.exists(directory):
#           os.makedirs(directory)
    logging.basicConfig(level   = logging.INFO, 
                        format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
                        datefmt = '%m-%d %H:%M')
#                       filename= log_path,
#                       filemode= 'w')
#
#   # define a Handler which writes INFO messages or higher to the sys.stderr
#   consoleLogger = logging.StreamHandler()
#   consoleLogger.setLevel(logging.ERROR)
#   # set a format which is simpler for console use
#   formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
#   # tell the handler to use this format
#   consoleLogger.setFormatter(formatter)
#
#   # add the handler to the logger
#   logging.getLogger(name).addHandler(consoleLogger)

def setup_daemon_logger (name, log_path = None):
    # first make sure path availabe
    try:
        os.unlink(log_path)
    except:
        pass
    logging.basicConfig(level   = logging.INFO, 
                        format  = '%(asctime)s %(name)-10s %(module)-20s %(levelname)-8s %(message)s',
                        datefmt = '%m-%d %H:%M',
                        filename= log_path,
                    filemode= 'w')

class CustomLogger(object):
  
    def __init__(self, log_filename):
        # Store the original stdout and stderr
        sys.stdout.flush()
        sys.stderr.flush()

        self.stdout_fd = os.dup(sys.stdout.fileno())
        self.devnull = os.open('/dev/null', os.O_WRONLY)
        self.log_file = open(log_filename, 'w')
        self.silenced = False
        self.pending_log_file_prints = 0

    # silence all prints from stdout
    def silence(self):
        os.dup2(self.devnull, sys.stdout.fileno())
        self.silenced = True

    # restore stdout status
    def restore(self):
        sys.stdout.flush()
        sys.stderr.flush()
        # Restore normal stdout
        os.dup2(self.stdout_fd, sys.stdout.fileno())
        self.silenced = False

    #print a message to the log (both stdout / log file)
    def log(self, text, force = False, newline = True):
        self.log_file.write((text + "\n") if newline else text)
        self.pending_log_file_prints += 1

        if (self.pending_log_file_prints >= 10):
             self.log_file.flush()
             self.pending_log_file_prints = 0

        self.console(text, force, newline)

    # print a message to the console alone
    def console(self, text, force = False, newline = True):
        _text = (text + "\n") if newline else text
        # if we are silenced and not forced - go home
        if self.silenced and not force:
            return

        if self.silenced:
            os.write(self.stdout_fd, _text)
        else:
            sys.stdout.write(_text)

        sys.stdout.flush()

    # flush
    def flush(self):
        sys.stdout.flush()
        self.log_file.flush()

    def __exit__(self, type, value, traceback):
        sys.stdout.flush()
        self.log_file.flush()
        os.close(self.devnull)
        os.close(self.log_file)