Class: Wsl::CustomLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/Logger.rb

Constant Summary collapse

@@HEADER =

Class constant cannot be accessed outside of class.

"======"
@@localCacheFile =
".\\logs\\.cache\\TestSummary.dat"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logFile) ⇒ CustomLogger

Constructor logs to file as well as screen if specified.

logFile: path to log file.



44
45
46
47
48
49
50
51
52
53
# File 'lib/utils/Logger.rb', line 44

def initialize(logFile)			
	# Initialise instance counters.
	@step = 1
	@passedTests = 0
	@failedTests = 0
	@logFile = logFile
	
	# Create the log dir and cache dir structure.
	createDirs()
end

Instance Attribute Details

#failedTestsObject (readonly)

Returns the value of attribute failedTests.



36
37
38
# File 'lib/utils/Logger.rb', line 36

def failedTests
  @failedTests
end

#passedTestsObject (readonly)

Returns the value of attribute passedTests.



36
37
38
# File 'lib/utils/Logger.rb', line 36

def passedTests
  @passedTests
end

Class Method Details

.debugObject

Static methods to get log level (cannot assign to these). Within class, but outside of method, use ‘self.debug’ to call. within method use ‘CustomLogger.debug’ to call.



26
# File 'lib/utils/Logger.rb', line 26

def self.debug; "debug"; end

.errorObject



29
# File 'lib/utils/Logger.rb', line 29

def self.error; "error"; end

.failObject



34
# File 'lib/utils/Logger.rb', line 34

def self.fail; "FAIL"; end

.fatalObject



30
# File 'lib/utils/Logger.rb', line 30

def self.fatal; "fatal"; end

.infoObject



27
# File 'lib/utils/Logger.rb', line 27

def self.info; "info"; end

.initializeSuiteLogger(logFile) ⇒ Object

Initialises a logger with the standard WSL logger settings and returns it.



195
196
197
# File 'lib/utils/Logger.rb', line 195

def self.initializeSuiteLogger(logFile)
	CustomLogger.initializeLogger(logFile)
end

.passObject

Actual results constants.



33
# File 'lib/utils/Logger.rb', line 33

def self.pass; "PASS"; end

.warnObject



28
# File 'lib/utils/Logger.rb', line 28

def self.warn; "warn"; end

Instance Method Details

#clearCacheObject

Clears the cache with the results of any old tests that have run.



175
176
177
178
179
180
# File 'lib/utils/Logger.rb', line 175

def clearCache
	# Delete the file.
	if File.exist?(@@localCacheFile) then
		File.delete(@@localCacheFile)		
	end
end

#log(entry) ⇒ Object

Logs an entry to the log.

entry: What to log



187
188
189
# File 'lib/utils/Logger.rb', line 187

def log(entry)
	logEntry(CustomLogger.info, entry)
end

#logActualResult(passOrFail, message) ⇒ Object

Logs the actual results.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/utils/Logger.rb', line 99

def logActualResult(passOrFail, message)
	logEntry(CustomLogger.info, " Actual Result:")
	logEntry(CustomLogger.info, "  " + passOrFail + ": " + message)
	logEntry(CustomLogger.info, "")
	
	if passOrFail == CustomLogger.pass then
		@passedTests += 1
	else
		@failedTests += 1
	end
end

#logComment(description) ⇒ Object

Logs a test comment.



75
76
77
# File 'lib/utils/Logger.rb', line 75

def logComment(description)
	logEntry(CustomLogger.info, ">" +  description)	
end

#logEndTestObject



123
124
125
# File 'lib/utils/Logger.rb', line 123

def logEndTest()		
	logEntry(CustomLogger.info, @@HEADER + " Finished Test: " + @testName + " " + @@HEADER)
end

#logException(exception) ⇒ Object

Logs an exception.

exception: The exception to log.



116
117
118
119
120
121
# File 'lib/utils/Logger.rb', line 116

def logException(exception)
	logEntry(CustomLogger.error, "  FAIL WITH EXCEPTION: " + exception.message + "\n" + 
			exception.backtrace.join("\n"))
			
	@failedTests += 1
end

#logExpectedResult(expectedResults) ⇒ Object

Logs the expected results.



82
83
84
85
86
# File 'lib/utils/Logger.rb', line 82

def logExpectedResult(expectedResults)
	logEntry(CustomLogger.info, "")
	logEntry(CustomLogger.info, " Expected Result:")
	logEntry(CustomLogger.info, "  " + expectedResults)
end

#logStep(description) ⇒ Object

Logs a test step.



67
68
69
70
# File 'lib/utils/Logger.rb', line 67

def logStep(description)
	logEntry(CustomLogger.info, " Step " + @step.to_s + ": " + description)		
	@step += 1
end

#logTestName(testName) ⇒ Object

Logs the name of the test.



58
59
60
61
62
# File 'lib/utils/Logger.rb', line 58

def logTestName(testName)
	@testName = testName
	# logEntry(CustomLogger.info, "")
	logEntry(CustomLogger.info, @@HEADER + " Started Test: " + @testName + " " + @@HEADER)
end

#resetObject

Resets the logger by doing the following - Step counter starts from 1 again.



92
93
94
# File 'lib/utils/Logger.rb', line 92

def reset
	@step = 1
end

#summaryObject

Prints a summary of passed and failed tests for a script.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/utils/Logger.rb', line 131

def summary
	logEntry(CustomLogger.info, "")
	logEntry(CustomLogger.info, "Test Summary:")
	logEntry(CustomLogger.info, " Passed Tests: " + @passedTests.to_s)
	logEntry(CustomLogger.info, " Failed Tests: " + @failedTests.to_s)	
	logEntry(CustomLogger.info, "")
				
	file = File.open(@@localCacheFile, "a")
	file.puts("Passed:" + @passedTests.to_s + "\tFailed:" + @failedTests.to_s + "\t" + @testName)
	file.close
end

#totalSummaryObject

Prints a summary of the total passed and failed tests for all scripts in a run.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/utils/Logger.rb', line 147

def totalSummary
	
	# Ensure cache file exists first.
	if !File.exist?(@@localCacheFile) then
		return
	end 

	# Output the total summary to logger.
	
	logEntry(CustomLogger.info, "+-------------------------------------------------------------------------------+")
	logEntry(CustomLogger.info, " Total Test Summary:")
				
	lines = IO.readlines(@@localCacheFile)
	lines.each do |line|
		if line != nil then
			logEntry(CustomLogger.info, "  " + line.chomp)
		end
	end
	
	logEntry(CustomLogger.info, "+-------------------------------------------------------------------------------+")
	
	# Delete the file.
	clearCache
end