Created
December 5, 2016 13:40
-
-
Save jcsky/10705c88877bd689848b7c19ffaf5283 to your computer and use it in GitHub Desktop.
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
class SystemLogger | |
def initialize | |
@sys_logs = [] | |
end | |
def add(msg) | |
timestamp = Time.now.strftime('%Y%m%d_%H%M%S_%N') | |
@sys_logs << [timestamp, msg] | |
end | |
def logs | |
@sys_logs | |
end | |
def create_iterator | |
SystemLoggerIterator.new(logs) | |
end | |
class SystemLoggerIterator | |
def initialize(sys_logs) | |
@items = sys_logs | |
@size = @items.size | |
@position = 0 | |
end | |
def has_next? | |
@position < @size | |
end | |
def next | |
return nil unless has_next? | |
item = @items[@position].last | |
@position += 1 | |
item | |
end | |
end | |
end | |
class ApplicationLogger | |
def initialize | |
@app_logs = {} | |
end | |
def add(msg) | |
timestamp = Time.now.strftime('%Y%m%d_%H%M%S_%N') | |
@app_logs[timestamp] = msg | |
end | |
def logs | |
@app_logs | |
end | |
def create_iterator | |
ApplicationLoggerIterator.new(logs) | |
end | |
class ApplicationLoggerIterator | |
def initialize(app_logs) | |
@items = app_logs.values | |
@size = @items.size | |
@position = 0 | |
end | |
def has_next? | |
@position < @size | |
end | |
def next | |
return nil unless has_next? | |
item = @items[@position] | |
@position += 1 | |
item | |
end | |
end | |
end | |
class LogCollector | |
def initialize(loggers) | |
@loggers = loggers | |
end | |
def collect | |
all_logs = [] | |
@loggers.each do |logger| | |
iterator = logger.create_iterator | |
while iterator.has_next? | |
all_logs << iterator.next | |
end | |
end | |
all_logs | |
end | |
end | |
sys_logger = SystemLogger.new | |
sys_logger.add('Segmentation fault.') | |
sys_logger.add('The server is on fire.') | |
sys_logger.add('yoyoyo') | |
app_logger = ApplicationLogger.new | |
app_logger.add('This is a pen, this is an apple.') | |
app_logger.add('Kait is not a cat.') | |
app_logger.add('WebTechTopic is awesome.') | |
log_collector = LogCollector.new([sys_logger, app_logger]) | |
p log_collector.collect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment