finally simple logging
instead of commenting puts
This commit is contained in:
parent
9519196a98
commit
b5e733cd11
46
lib/logging.rb
Normal file
46
lib/logging.rb
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# a simple module to be included into a class that want to log
|
||||||
|
#
|
||||||
|
# use the standard logger and the class name as the program name
|
||||||
|
#
|
||||||
|
# The standard functions are available on the log object
|
||||||
|
# And the log level may be set after inclusion with level function (that takes symbols)
|
||||||
|
|
||||||
|
require "logger"
|
||||||
|
|
||||||
|
module Logging
|
||||||
|
def self.included(base)
|
||||||
|
base.extend(Methods)
|
||||||
|
end
|
||||||
|
def log
|
||||||
|
self.class.log
|
||||||
|
end
|
||||||
|
|
||||||
|
module Methods
|
||||||
|
def log
|
||||||
|
return @logger if @logger
|
||||||
|
@logger = Logger.new STDOUT
|
||||||
|
@logger.progname = self.name.split("::").last
|
||||||
|
@logger.datetime_format = '%M:%S'
|
||||||
|
@logger.level = Logger::INFO
|
||||||
|
@logger
|
||||||
|
end
|
||||||
|
def log_level l
|
||||||
|
log.level = case l
|
||||||
|
when :unknown
|
||||||
|
Logger::UNKNOWN
|
||||||
|
when :fatal
|
||||||
|
Logger::FATAL
|
||||||
|
when :error
|
||||||
|
Logger::ERROR
|
||||||
|
when :warn
|
||||||
|
Logger::WARN
|
||||||
|
when :info
|
||||||
|
Logger::INFO
|
||||||
|
when :debug
|
||||||
|
Logger::DEBUG
|
||||||
|
else
|
||||||
|
raise "unknown log level #{l}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -10,6 +10,8 @@ module Register
|
|||||||
|
|
||||||
class Assembler
|
class Assembler
|
||||||
include Padding
|
include Padding
|
||||||
|
include Logging
|
||||||
|
#log_level :debug
|
||||||
|
|
||||||
def initialize machine
|
def initialize machine
|
||||||
@machine = machine
|
@machine = machine
|
||||||
@ -28,7 +30,7 @@ module Register
|
|||||||
objekt.binary.position = at
|
objekt.binary.position = at
|
||||||
objekt.instructions.set_position at
|
objekt.instructions.set_position at
|
||||||
len = objekt.instructions.total_byte_length
|
len = objekt.instructions.total_byte_length
|
||||||
puts "CODE #{objekt.name} at #{objekt.binary.position} len: #{len}"
|
log.debug "CODE #{objekt.name} at #{objekt.binary.position} len: #{len}"
|
||||||
objekt.binary.set_length(len/4)
|
objekt.binary.set_length(len/4)
|
||||||
at += objekt.binary.padded_length
|
at += objekt.binary.padded_length
|
||||||
end
|
end
|
||||||
@ -59,7 +61,7 @@ module Register
|
|||||||
# debugging loop accesses all positions to force an error if it's not set
|
# debugging loop accesses all positions to force an error if it's not set
|
||||||
all.each do |objekt|
|
all.each do |objekt|
|
||||||
next if objekt.is_a?(Register::Label)
|
next if objekt.is_a?(Register::Label)
|
||||||
puts "Linked #{objekt.class}(#{objekt.object_id}) at #{objekt.position} / #{objekt.padded_length}"
|
log.debug "Linked #{objekt.class}(#{objekt.object_id}) at #{objekt.position} / #{objekt.padded_length}"
|
||||||
objekt.position
|
objekt.position
|
||||||
end
|
end
|
||||||
# first we need to create the binary code for the methods
|
# first we need to create the binary code for the methods
|
||||||
@ -84,7 +86,7 @@ module Register
|
|||||||
next if objekt.is_a? Register::Label # ignore
|
next if objekt.is_a? Register::Label # ignore
|
||||||
write_any( objekt )
|
write_any( objekt )
|
||||||
end
|
end
|
||||||
puts "Assembled #{stream_position} bytes"
|
log.debug "Assembled #{stream_position} bytes"
|
||||||
return @stream.string
|
return @stream.string
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -97,12 +99,12 @@ module Register
|
|||||||
#puts "assemble #{method.source.instructions}"
|
#puts "assemble #{method.source.instructions}"
|
||||||
method.instructions.assemble_all( stream )
|
method.instructions.assemble_all( stream )
|
||||||
rescue => e
|
rescue => e
|
||||||
puts "Assembly error #{method.name}\n#{Sof.write(method.instructions).to_s[0...2000]}"
|
log.debug "Assembly error #{method.name}\n#{Sof.write(method.instructions).to_s[0...2000]}"
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
index = 1
|
index = 1
|
||||||
stream.rewind
|
stream.rewind
|
||||||
puts "Assembled #{method.name} with length #{stream.length}"
|
log.debug "Assembled #{method.name} with length #{stream.length}"
|
||||||
raise "length error #{method.binary.get_length} != #{method.instructions.total_byte_length}" if method.binary.get_length*4 != method.instructions.total_byte_length
|
raise "length error #{method.binary.get_length} != #{method.instructions.total_byte_length}" if method.binary.get_length*4 != method.instructions.total_byte_length
|
||||||
raise "length error #{stream.length} != #{method.instructions.total_byte_length}" if method.instructions.total_byte_length != stream.length
|
raise "length error #{stream.length} != #{method.instructions.total_byte_length}" if method.instructions.total_byte_length != stream.length
|
||||||
stream.each_byte do |b|
|
stream.each_byte do |b|
|
||||||
@ -112,7 +114,7 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
def write_any obj
|
def write_any obj
|
||||||
puts "Assemble #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
|
log.debug "Assemble #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
|
||||||
if @stream.length != obj.position
|
if @stream.length != obj.position
|
||||||
raise "Assemble #{obj.class} #{obj.object_id} at #{stream_position} not #{obj.position}"
|
raise "Assemble #{obj.class} #{obj.object_id} at #{stream_position} not #{obj.position}"
|
||||||
end
|
end
|
||||||
@ -121,14 +123,14 @@ module Register
|
|||||||
else
|
else
|
||||||
write_object obj
|
write_object obj
|
||||||
end
|
end
|
||||||
puts "Assemble #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
|
log.debug "Assemble #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
|
||||||
obj.position
|
obj.position
|
||||||
end
|
end
|
||||||
|
|
||||||
# write type and layout of the instance, and the variables that are passed
|
# write type and layout of the instance, and the variables that are passed
|
||||||
# variables ar values, ie int or refs. For refs the object needs to save the object first
|
# variables ar values, ie int or refs. For refs the object needs to save the object first
|
||||||
def write_object( object )
|
def write_object( object )
|
||||||
puts "Write #{object.class} #{object.inspect}"
|
log.debug "Write #{object.class} #{object.inspect}"
|
||||||
unless @machine.objects.has_key? object.object_id
|
unless @machine.objects.has_key? object.object_id
|
||||||
raise "Object(#{object.object_id}) not linked #{object.inspect}"
|
raise "Object(#{object.object_id}) not linked #{object.inspect}"
|
||||||
end
|
end
|
||||||
@ -140,15 +142,15 @@ module Register
|
|||||||
written += 4
|
written += 4
|
||||||
end
|
end
|
||||||
lay_len = written
|
lay_len = written
|
||||||
puts "instances=#{object.get_instance_variables.inspect} mem_len=#{object.padded_length}"
|
log.debug "instances=#{object.get_instance_variables.inspect} mem_len=#{object.padded_length}"
|
||||||
if( object.is_a? Parfait::Indexed)
|
if( object.is_a? Parfait::Indexed)
|
||||||
object.each do |inst|
|
object.each do |inst|
|
||||||
write_ref_for(inst)
|
write_ref_for(inst)
|
||||||
written += 4
|
written += 4
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
puts "layout #{lay_len} , total #{written} (array #{written - lay_len})"
|
log.debug "layout #{lay_len} , total #{written} (array #{written - lay_len})"
|
||||||
puts "Len = #{object.get_length} , inst = #{object.get_layout.instance_length}" if object.is_a? Parfait::Layout
|
log.debug "Len = #{object.get_length} , inst = #{object.get_layout.instance_length}" if object.is_a? Parfait::Layout
|
||||||
pad_after( written )
|
pad_after( written )
|
||||||
object.position
|
object.position
|
||||||
end
|
end
|
||||||
@ -160,11 +162,11 @@ module Register
|
|||||||
def write_String( string )
|
def write_String( string )
|
||||||
str = string.to_string if string.is_a? Parfait::Word
|
str = string.to_string if string.is_a? Parfait::Word
|
||||||
str = string.to_s if string.is_a? Symbol
|
str = string.to_s if string.is_a? Symbol
|
||||||
puts "String is #{string} at #{string.position} length #{string.length}"
|
log.debug "String is #{string} at #{string.position} length #{string.length}"
|
||||||
write_ref_for( string.get_layout ) #ref
|
write_ref_for( string.get_layout ) #ref
|
||||||
@stream.write str
|
@stream.write str
|
||||||
pad_after(str.length + 4)
|
pad_after(str.length + 4)
|
||||||
puts "String (#{string.length}) stream #{@stream.length}"
|
log.debug "String (#{string.length}) stream #{@stream.length}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_Symbol(sym)
|
def write_Symbol(sym)
|
||||||
@ -194,7 +196,7 @@ module Register
|
|||||||
@stream.write_uint8(0)
|
@stream.write_uint8(0)
|
||||||
end
|
end
|
||||||
after = stream_position
|
after = stream_position
|
||||||
puts "padded #{length} with #{pad} stream #{before}/#{after}"
|
log.debug "padded #{length} with #{pad} stream #{before}/#{after}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# return the stream length as hex
|
# return the stream length as hex
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
require 'parslet'
|
require 'parslet'
|
||||||
|
|
||||||
|
require "logging"
|
||||||
require "elf/object_writer"
|
require "elf/object_writer"
|
||||||
require 'salama-reader'
|
require 'salama-reader'
|
||||||
AST::Node.class_eval do
|
AST::Node.class_eval do
|
||||||
|
Loading…
Reference in New Issue
Block a user