forked from DMTF/Redfish-Schema-C-Struct-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolLogger.py
120 lines (105 loc) · 3.55 KB
/
ToolLogger.py
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
112
113
114
115
116
117
#
# Redfish JSON resource to C structure converter source code generator.
#
# Copyright Notice:
# Copyright 2021 DMTF. All rights reserved.
# License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/Redfish-Tacklebox/blob/main/LICENSE.md
#
import sys, os, logging
import datetime
##
# Common definitions
#
TOOL_LOG_NONE = 0
TOOL_LOG_TO_CONSOLE = 1
TOOL_LOG_TO_FILE = 2
TOOL_LOG_SIMPLE_PROGRESS_DOTS = 3
TOOL_LOG_ON = True
TOOL_LOG_OFF = False
## Class of logger for logging messages.
#
# This class is for logging message to console, file or etc.
#
class ToolLog(object):
# Logtype
# TurnOn = Boolean
## Initial the ToolLog class.
#
# Initial the information of ToolLog class
#
# @param[in] LogType Tool log type, see the common definitions
#
def __init__(self, LogType, LogFilePath):
self.Logtype = LogType
self.TurnOn = True
self.PreviousLogType = LogType
self.CurrentSimpleDotMessages = 0
self.CurrentSimpleDotsCarryReturn = 0
self.SimpleDotMessages = 30
self.SimpleDotsCarryReturn = 80
if self.Logtype == TOOL_LOG_TO_FILE:
self.LogFilePath = LogFilePath
if os.path.exists (self.LogFilePath) == True:
fdo = open (self.LogFilePath, "a")
else:
fdo = open (self.LogFilePath, "w")
fdo.writelines ("\n\n======= " + str(datetime.datetime.now()) + " =============\n")
fdo.close()
## Log the message
#
# This function logs the message according
# to the ToolLog.LogType
#
# @param[in] message Message to log.
#
def LogIt (self, message):
if self.TurnOn == False:
return
if self.Logtype == TOOL_LOG_NONE:
return
elif self.Logtype == TOOL_LOG_SIMPLE_PROGRESS_DOTS:
#
# log as the progress dots.
#
self.CurrentSimpleDotMessages += 1
if self.CurrentSimpleDotMessages == self.SimpleDotMessages:
sys.stdout.write ('.')
self.CurrentSimpleDotMessages = 0
self.CurrentSimpleDotsCarryReturn += 1
if self.CurrentSimpleDotsCarryReturn == self.SimpleDotsCarryReturn:
self.CurrentSimpleDotsCarryReturn = 0
sys.stdout.write ('\n')
elif self.Logtype == TOOL_LOG_TO_CONSOLE:
#
# log to console.
#
print (message)
elif self.Logtype == TOOL_LOG_TO_FILE:
#
# log to file.
#
if self.LogFilePath == "":
self.LogFilePath = "RedfishCs.log"
if os.path.exists (self.LogFilePath) == True:
fdo = open (self.LogFilePath, "a")
else:
fdo = open (self.LogFilePath, "w")
if fdo == 0:
self.LogFilePath == ""
return
fdo.writelines (message + "\n")
fdo.close()
## Turn on/off the log
#
# This function turns on/off the log
# to the ToolLog.LogType
#
# @param[in] TurnOn Set to True for turning on the log.
#
def LogTurnOnOff (self, TurnOn):
if TurnOn == True:
self.Logtype = self.PreviousLogType
self.TurnOn = True
else:
self.PreviousLogType = self.Logtype
self.TurnOn = False