Last active
October 21, 2015 14:25
-
-
Save wildart/fe2ea28228f9935db38d to your computer and use it in GitHub Desktop.
Logging interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Log | |
export AbstractLogger, call, logging | |
import Base: call, getindex | |
@enum Level OFF=0 FATAL=100 ERROR=200 WARN=300 INFO=400 DEBUG=500 TRACE=600 ALL=1000 | |
abstract AbstractLogger | |
type LogWrapper{T <: AbstractLogger} | |
logger::T | |
warn::Function | |
LogWrapper(l) = new(l) | |
end | |
LogWrapper{T <: AbstractLogger}(l::T) = LogWrapper{T}(l) | |
function create{L<:AbstractLogger}(l::L) | |
w = LogWrapper(l) | |
w.warn = (msg...)->logging(w.logger, WARN, msg...) | |
return w | |
end | |
call(wrapper::LogWrapper, msg...) = logging(wrapper.logger, msg...) | |
getindex(wrapper::LogWrapper, level::Level) = (msg...)->logging(wrapper.logger, level, msg...) | |
"This function is common interface" | |
function logging end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Log | |
import Log: create, WARN | |
# Definition of backend | |
type IOLogger <: Log.AbstractLogger | |
io::IO | |
end | |
Log.logging(l::IOLogger, msg...) = println(l.io, "=>", msg...) | |
# Create logging object | |
lgr = Log.create(IOLogger(STDOUT)) | |
lgr[WARN](1, " a ", 0.9) | |
lgr.warn(1, " a ", 0.9) | |
lgr(1, " a ", 0.9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment