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.



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

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.



38
39
40
# File 'lib/utils/Logger.rb', line 38

def failedTests
  @failedTests
end

#passedTestsObject (readonly)

Returns the value of attribute passedTests.



38
39
40
# File 'lib/utils/Logger.rb', line 38

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. ++



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

def self.debug; "debug"; end

.errorObject



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

def self.error; "error"; end

.failObject



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

def self.fail; "FAIL"; end

.fatalObject



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

def self.fatal; "fatal"; end

.infoObject



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

def self.info; "info"; end

.initializeSuiteLogger(logFile) ⇒ Object

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



197
198
199
# File 'lib/utils/Logger.rb', line 197

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

.passObject

Actual results constants.



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

def self.pass; "PASS"; end

.warnObject



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

def self.warn; "warn"; end

Instance Method Details

#clearCacheObject

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



177
178
179
180
181
182
# File 'lib/utils/Logger.rb', line 177

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



189
190
191
# File 'lib/utils/Logger.rb', line 189

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

#logActualResult(passOrFail, message) ⇒ Object

Logs the actual results.



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

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.



77
78
79
# File 'lib/utils/Logger.rb', line 77

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

#logEndTestObject



125
126
127
# File 'lib/utils/Logger.rb', line 125

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

#logException(exception) ⇒ Object

Logs an exception.

exception: The exception to log.



118
119
120
121
122
123
# File 'lib/utils/Logger.rb', line 118

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.



84
85
86
87
88
# File 'lib/utils/Logger.rb', line 84

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

#logStep(description) ⇒ Object

Logs a test step.



69
70
71
72
# File 'lib/utils/Logger.rb', line 69

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

#logTestName(testName) ⇒ Object

Logs the name of the test.



60
61
62
63
64
# File 'lib/utils/Logger.rb', line 60

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.



94
95
96
# File 'lib/utils/Logger.rb', line 94

def reset
	@step = 1
end

#summaryObject

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



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

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.



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

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