Skip to content

Commit

Permalink
added Y2Logger
Browse files Browse the repository at this point in the history
- Y2Logger provides a Ruby Logger for writing logs using the Yast
  format, can be used by external libraries for logging into y2log
- added support for RSpec tests
- 3.1.7
  • Loading branch information
lslezak committed Feb 3, 2014
1 parent ab2e953 commit e65b0f8
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 24 deletions.
7 changes: 7 additions & 0 deletions package/yast2-ruby-bindings.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Mon Feb 3 14:15:39 UTC 2014 - lslezak@suse.cz

- added Y2Logger - Ruby Logger for writing logs using the Yast
format, can be used by external libraries for logging into y2log
- 3.1.7

-------------------------------------------------------------------
Mon Feb 3 10:07:48 UTC 2014 - jreidinger@suse.com

Expand Down
3 changes: 2 additions & 1 deletion package/yast2-ruby-bindings.spec
Expand Up @@ -3,7 +3,7 @@
#

Name: yast2-ruby-bindings
Version: 3.1.6
Version: 3.1.7
Release: 0
License: GPL-2.0
Group: System/YaST
Expand All @@ -16,6 +16,7 @@ BuildRequires: yast2-devtools >= 3.1.10
# libzypp-devel is missing .la requires
BuildRequires: ruby-devel
BuildRequires: rubygem-fast_gettext
BuildRequires: rubygem-rspec
Requires: rubygem-fast_gettext
Requires: yast2-core >= 2.24.0
BuildRequires: yast2-core-devel >= 2.24.0
Expand Down
1 change: 1 addition & 0 deletions src/ruby/yast.rb
Expand Up @@ -35,6 +35,7 @@
require "yast/fun_ref"
require "yast/i18n"
require "yast/logger"
require "yast/y2logger"
require "yast/module"
require "yast/ops"
require "yast/path"
Expand Down
68 changes: 68 additions & 0 deletions src/ruby/yast/y2logger.rb
@@ -0,0 +1,68 @@
# encoding: utf-8

require "logger"
require "singleton"

require "yast/logger"

module Yast

# A Ruby Logger which wraps Yast.y2*() calls
class Y2Logger < ::Logger
include Singleton

# location of the caller
CALL_FRAME = 2

def add(severity, progname = nil, message = nil, &block)
message = yield if block_given?

case severity
when DEBUG
Yast.y2debug(CALL_FRAME, message)
when INFO
Yast.y2milestone(CALL_FRAME, message)
when WARN
Yast.y2warning(CALL_FRAME, message)
when ERROR
Yast.y2error(CALL_FRAME, message)
when FATAL
Yast.y2error(CALL_FRAME, message)
when UNKNOWN
Yast.y2internal(CALL_FRAME, message)
else
Yast.y2internal(CALL_FRAME, "Unknown error level #{severity}: Error: #{message}")
end
end

def initialize(*args)
# do not write to any file, the actual logging is implemented in add()
super(nil)
# process also debug messages but might not be logged in the end
self.level = ::Logger::DEBUG
end
end

# This module provides access to Yast specific logging
#
# @example Use YastLogger in an easy way
# class Foo
# include Yast::Logger
#
# def foo
# # this will be logged into y2log using the usual y2log format
# log.debug "debug"
# log.error "error"
# end
# end
module Logger
def log
Y2Logger.instance
end

def self.included(base)
base.extend self
end
end

end
7 changes: 7 additions & 0 deletions tests/ruby/CMakeLists.txt
Expand Up @@ -9,3 +9,10 @@ FILE(GLOB Tests "*_test.rb")
foreach(test ${Tests})
ADD_TEST(${test} ruby -C ${CMAKE_CURRENT_SOURCE_DIR} ${test})
endforeach(test)


FILE(GLOB Specs "*_spec.rb")

foreach(test ${Specs})
ADD_TEST(${test} rspec ${test})
endforeach(test)
3 changes: 1 addition & 2 deletions tests/ruby/builtins_test.rb
@@ -1,7 +1,6 @@
# encoding: utf-8

$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast/builtins"
require "yast/path"
Expand Down
3 changes: 1 addition & 2 deletions tests/ruby/convert_test.rb
@@ -1,7 +1,6 @@
# encoding: utf-8

$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast/convert"
require "yast/path"
Expand Down
3 changes: 1 addition & 2 deletions tests/ruby/exportable_test.rb
@@ -1,5 +1,4 @@
$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require 'yast/exportable'

Expand Down
3 changes: 1 addition & 2 deletions tests/ruby/import_test.rb
Expand Up @@ -2,8 +2,7 @@
# Test Ycp.import
#

$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast"

Expand Down
3 changes: 1 addition & 2 deletions tests/ruby/include_test.rb
Expand Up @@ -2,8 +2,7 @@
# Test Ycp.import
#

$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast"

Expand Down
3 changes: 1 addition & 2 deletions tests/ruby/ops_test.rb
@@ -1,7 +1,6 @@
# encoding: utf-8

$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast/ops"
require "yast/convert"
Expand Down
3 changes: 1 addition & 2 deletions tests/ruby/path_test.rb
@@ -1,5 +1,4 @@
$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast/path"

Expand Down
3 changes: 1 addition & 2 deletions tests/ruby/term_test.rb
@@ -1,5 +1,4 @@
$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast/term"

Expand Down
5 changes: 0 additions & 5 deletions tests/ruby/test_helper.rb
Expand Up @@ -8,8 +8,3 @@
$:.unshift "#{ROOT_DIR}/src/ruby" # yast.rb
ENV["Y2DIR"] = File.dirname(__FILE__)

require 'test/unit'

module Yast
TestCase = Test::Unit::TestCase
end
3 changes: 3 additions & 0 deletions tests/ruby/test_helper_rspec.rb
@@ -0,0 +1,3 @@
$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"

9 changes: 9 additions & 0 deletions tests/ruby/test_helper_test_unit.rb
@@ -0,0 +1,9 @@
$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"


require 'test/unit'

module Yast
TestCase = Test::Unit::TestCase
end
3 changes: 1 addition & 2 deletions tests/ruby/ui_shortcuts_test.rb
@@ -1,5 +1,4 @@
$LOAD_PATH << File.dirname(__FILE__)
require "test_helper"
require_relative "test_helper_test_unit"

require "yast/ui_shortcuts"

Expand Down
57 changes: 57 additions & 0 deletions tests/ruby/y2logger_spec.rb
@@ -0,0 +1,57 @@
#!/usr/bin/env rspec

require_relative "test_helper_rspec"

require "yast/y2logger"

module Yast
describe Y2Logger do

TEST_MESSAGE = "Testing"

before do
@test_logger = Y2Logger.instance
end

it "logs debug messages via y2debug()" do
Yast.should_receive(:y2debug).with(Y2Logger::CALL_FRAME, TEST_MESSAGE)
@test_logger.debug TEST_MESSAGE
end

it "logs info messages via y2milestone()" do
Yast.should_receive(:y2milestone).with(Y2Logger::CALL_FRAME, TEST_MESSAGE)
@test_logger.info TEST_MESSAGE
end

it "logs warnings via y2warning()" do
Yast.should_receive(:y2warning).with(Y2Logger::CALL_FRAME, TEST_MESSAGE)
@test_logger.warn TEST_MESSAGE
end

it "logs errors via y2error()" do
Yast.should_receive(:y2error).with(Y2Logger::CALL_FRAME, TEST_MESSAGE)
@test_logger.error TEST_MESSAGE
end

it "logs fatal errors via y2error()" do
Yast.should_receive(:y2error).with(Y2Logger::CALL_FRAME, TEST_MESSAGE)
@test_logger.fatal TEST_MESSAGE
end

it "handles a message passed via block" do
Yast.should_receive(:y2milestone).with(Y2Logger::CALL_FRAME, TEST_MESSAGE)
@test_logger.info { TEST_MESSAGE }
end
end

describe Yast::Logger do
it "module adds log() method for accessing the Logger" do
class Test
include Yast::Logger
end
expect(Test.log).to be_kind_of(::Logger)
expect(Test.new.log).to be_kind_of(::Logger)
end
end

end

0 comments on commit e65b0f8

Please sign in to comment.