Skip to content

Commit

Permalink
Added a better logger for internal use
Browse files Browse the repository at this point in the history
Now we have some context based logger
  • Loading branch information
IceDragon200 committed Jul 14, 2016
1 parent 84bdeaa commit 2c3bb54
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 20 deletions.
37 changes: 27 additions & 10 deletions modules/engine/mrblib/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Engine
# @!attribute step
# @return [Proc] per frame step function
attr_accessor :step

# @return [Hash<Symbol, Object>]
attr_accessor :config

attr_accessor :log
# @return [Moon::ContextLogger, ILogger] any logger like interface
attr_accessor :logger
# @return [Moon::Screen]
attr_reader :screen
# @return [Moon::Input]
attr_reader :input

# Initializes the engine. The block should be a step function to be called
Expand All @@ -27,7 +29,7 @@ def initialize(&block)
@input = nil
@fps = Moon::Clock.new
@step = block
@log = STDERR
@logger = Moon::ContextLogger.new(STDERR, 'Engine')
end

# @return [Float] Returns the time since the engine has started.
Expand All @@ -39,10 +41,12 @@ def uptime
#
# @raise {EngineQuit}
def quit
@logger.debug 'Quitting Engine!'
raise EngineQuit
end

private def setup_glfw
@logger.debug 'Initializing Screen'
@screen = Screen.new(@config.fetch(:width), @config.fetch(:height))
@screen.make_current
# debugging
Expand All @@ -51,18 +55,21 @@ def quit
end

private def create_input
@logger.debug 'Initializing Input'
@input = Input.new @screen.window
@log.puts 'Input initialized'
@logger.debug 'Input initialized'
end

private def setup_glew
@logger.debug 'Initializing GLEW'
# http://openglbook.com/blog/glgenvertexarrays-access-violationsegfault-with-glew/
GLEW.experimental = true
GLEW.init
@log.puts 'GLEW initialized'
@logger.debug 'GLEW initialized'
end

private def setup_default_shaders
@logger.debug 'Initializing default shaders'
@shaders = {}
sd = Moon::Shader::DEFAULTS
shader_path = Moon::Shader.is_legacy ? '120' : '330'
Expand All @@ -79,28 +86,37 @@ def quit
Moon::Spritesheet.default_shader = @shaders[:quad]
Moon::Tilemap.default_shader = @shaders[:quad]
Moon::Text.default_shader = @shaders[:text]
@logger.debug 'Default Shaders Initialized'
self
end

# Initializes all internal systems and prepares for main loop
#
# @return [self]
def setup
@logger.debug "Initializing"
setup_glfw
setup_glew
@screen.setup
create_input
setup_default_shaders
@logger.debug "Initialized"
self
end

# Destroys the current screen and cleans up.
# Destroys the systems and cleans up
def shutdown
@screen.close if @screen
@logger.debug "Shutting down"
@input.shutdown if @input
@screen.shutdown if @screen
@logger.debug "Shutdown"
self
end

# Starts the main loop, terminate the loop using {#quit}
def main
@log.puts "Audio Module: #{Audio::NAME}"
@log.puts 'Starting main loop'
@logger.info "Audio Module: #{Audio::NAME}"
@logger.debug 'Starting main loop'
clear_bits = GL2::GL_COLOR_BUFFER_BIT | GL2::GL_DEPTH_BUFFER_BIT
until @screen.should_close?
GL2.glClear clear_bits
Expand All @@ -111,6 +127,7 @@ def main
GLFW.poll_events
end
rescue EngineQuit
@logger.debug 'Got an EngineQuit request'
end

# Sets everything up, runs the main loop, and ensures we take care of
Expand Down
37 changes: 35 additions & 2 deletions modules/engine/mrblib/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,33 @@ class Input
attr_reader :window
# @return [Moon::Input::Mouse]
attr_reader :mouse
# @return [Moon::ContextLogger, ILogger] any logger like interface
attr_accessor :logger
#attr_reader :keyboard

# @param [GLFW::Window] window
def initialize(window)
@logger = Moon::ContextLogger.new STDERR, 'Input'
@window = window
init_mouse
init
end

# Initializes handlers and callbacks
#
# @api private
def init
initialize_mouse
register_callbacks
end

private def init_mouse
private def initialize_mouse
@logger.debug 'Initializing Mouse Handler'
@mouse = Mouse.new @window
@logger.debug 'Mouse Handler Initialized'
end

private def register_callbacks
@logger.debug 'Registering Callbacks'
@window.set_key_callback do |_, key_id, scancode, action, mods|
on_key KEY_MAP.fetch(key_id), scancode, STATE_MAP.fetch(action), mods unless key_id == -1
end
Expand All @@ -179,6 +192,26 @@ def initialize(window)
@window.set_cursor_pos_callback do |_, x, y|
on_mousemove x, y
end
@logger.debug 'Callbacks Registered'
end

private def unregister_callbacks
@logger.debug 'Unregistering Callbacks'
@window.set_key_callback
@window.set_mouse_button_callback
@window.set_char_callback
@window.set_cursor_pos_callback
@logger.debug 'Callbacks Unregistered'
end

# Removes all handlers to the underlying window
#
# @api private
def shutdown
@logger.debug 'Shutting down'
unregister_callbacks
@mouse = nil
@logger.debug 'Shutdown'
end

# Called on every keypress.
Expand Down
30 changes: 22 additions & 8 deletions modules/graphics/mrblib/screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ class Screen
attr_reader :clear_color
# Underlying window implementation (in default case GLFW).
attr_reader :window
attr_accessor :log
# @return [Moon::ContextLogger, ILogger] any logger like interface
attr_accessor :logger

# Creates a new game window with the given width and height.
#
# @param [Integer] w
# @param [Integer] h
def initialize(w, h)
@scale = 1.0
@log = STDERR
@logger = Moon::ContextLogger.new(STDERR, 'Screen')
create_window w, h
initialize_renderer
initialize_clear_color
Expand All @@ -35,6 +36,7 @@ def initialize(w, h)
# @param [Integer] w width of the window
# @param [Integer] h height of the window
def create_window(w, h)
@logger.debug "Creating Window: w=#{w} h=#{h}"
GLFW.window_hint GLFW::RESIZABLE, GL2::GL_FALSE
GLFW.window_hint GLFW::CONTEXT_VERSION_MAJOR, 3
GLFW.window_hint GLFW::CONTEXT_VERSION_MINOR, 3
Expand All @@ -45,13 +47,16 @@ def create_window(w, h)
title = 'Moon Player'
begin
@window = GLFW::Window.new w, h, title
@logger.warn "3.3 Window Created"
rescue GLFWError
@logger.warn "Failed to obtain 3.3 context, falling back to 2.1"
GLFW.default_window_hints
GLFW.window_hint GLFW::CONTEXT_VERSION_MAJOR, 2
GLFW.window_hint GLFW::CONTEXT_VERSION_MINOR, 1
Moon::Shader.is_legacy = true

@window = GLFW::Window.new w, h, title
@logger.warn "2.1 Window Created"
end
end

Expand All @@ -77,9 +82,9 @@ def glsl_version

# Prints all the available version strings to the @log
def print_versions
@log.puts "OpenGL v" + gl_version
@log.puts "GLSL v" + glsl_version
@log.puts "GLFW v" + GLFW.version_string
@logger.info "OpenGL v" + gl_version
@logger.info "GLSL v" + glsl_version
@logger.info "GLFW v" + GLFW.version_string
end

# Make the screen the current context
Expand All @@ -99,10 +104,18 @@ def swap
@window.swap_buffers
end

# Closes the underlying window
def close
@window.destroy if @window
# Closes the underlying window and cleans up
def shutdown
@logger.debug "Shutting Down"
if @window
@logger.debug "Closing Window"
@window.should_close = true
GLFW.wait_events
@window.destroy
end
@window = nil
@logger.debug "Shutdown"
self
end

private def initialize_clear_color
Expand Down Expand Up @@ -135,6 +148,7 @@ def update_projection
# @param [Integer] w
# @param [Integer] h
def resize(w, h)
@logger.debug "Resizing Screen to w=#{w} h=#{h}"
@window.window_size = [w, h]
@w, @h = *@window.window_size
@rect = Rect.new(0, 0, @w, @h)
Expand Down
133 changes: 133 additions & 0 deletions modules/system/mrblib/context_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
module Moon
# Internal Logger used by moon classes and systems
# It implements a very generic subset of the Logger methods
class ContextLogger
# The logging level
module Level
# Low-level information, mostly for developers.
DEBUG = 0
# Generic (useful) information about system operation.
INFO = 1
# A warning.
WARN = 2
# A handleable error condition.
ERROR = 3
# An unhandleable error that results in a program crash.
FATAL = 4
# An unknown message that should always be logged.
UNKNOWN = 5
# @return [Hash<Integer, String>]
LEVEL_TO_NAME = {
DEBUG => 'DEBUG',
INFO => 'INFO',
WARN => 'WARN',
ERROR => 'ERROR',
FATAL => 'FATAL',
UNKNOWN => 'UNKNOWN'
}
# @return [Hash<String, Integer>]
NAME_TO_LEVEL = LEVEL_TO_NAME.invert
end

class << self
# @!attribute [rw] context_separator
# @return [String] default '/'
attr_accessor :context_separator
end

self.context_separator = '/'

# @return [IO, #puts]
attr_accessor :device
# @return [Integer]
attr_reader :level
# @return [String]
attr_accessor :context
protected :context=

# @param [IO, #puts] device
# @param [String] ctx
# @param [String, Symbol, Integer] lvl
def initialize(device, ctx = nil, lvl = Level::DEBUG)
@device = device
@context = ctx
self.level = lvl
end

# Change the logger's level
#
# @param [String, Symbol, Integer] lvl
def level=(lvl)
@level = if lvl.is_a?(Integer)
lvl
else
Level::NAME_TO_LEVEL.fetch(lvl.to_s.upcase)
end
end

def initialize_copy(other)
@device = other.device
@context = other.context.dup
@level = other.level
end

private def context_separator
self.class.context_separator
end

# Writes the specified message to the device, if the level is above the
# current logging level
#
# @param [Integer] lvl logging level
# @param [String] message message to log
private def write(lvl, msg)
return if lvl < @level
level_name = Level::LEVEL_TO_NAME.fetch(lvl)
if @context
@device.puts "#{level_name}: [#{@context}] #{msg}"
else
@device.puts "#{level_name}: #{msg}"
end
end

# @param [String] msg message to log
def debug(msg)
write Level::DEBUG, msg
end

# @param [String] msg message to log
def info(msg)
write Level::INFO, msg
end

# @param [String] msg message to log
def error(msg)
write Level::ERROR, msg
end

# @param [String] msg message to log
def warn(msg)
write Level::WARN, msg
end

# @param [String] msg message to log
def fatal(msg)
write Level::FATAL, msg
end

# @param [String] msg message to log
def unknown(msg)
write Level::UNKNOWN, msg
end

# Creates a new logger with the context joined to the current
#
# @param [String] context
# @return [Moon::ContextLogger] new instance with context appended
def new(context)
dup.tap do |logger|
logger.context = "#{logger.context}#{context_separator}#{context}"
end
end
end
end

0 comments on commit 2c3bb54

Please sign in to comment.