PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/ramaze/log/syslog.rb

https://github.com/benlovell/ramaze
Ruby | 76 lines | 58 code | 9 blank | 9 comment | 10 complexity | a418a97ed1791211da66d010f18cd55d MD5 | raw file
Possible License(s): GPL-2.0
  1. # Copyright (c) 2008 rob@rebeltechnologies.nl
  2. # All files in this distribution are subject to the terms of the Ruby license.
  3. require 'spec/helper'
  4. require 'ramaze/log/syslog'
  5. describe 'Syslog' do
  6. # close the syslog, if it was open for some reason before we
  7. # start a test.
  8. before do
  9. if ( Syslog.opened? )
  10. ::Syslog.close
  11. end
  12. end
  13. it 'should default initialize correctly' do
  14. syslog = Ramaze::Logger::Syslog.new
  15. ::Syslog.opened?.should == true
  16. ::Syslog.ident.should == $0
  17. ::Syslog.options.should == ( ::Syslog::LOG_PID | ::Syslog::LOG_CONS )
  18. ::Syslog.facility.should == ::Syslog::LOG_USER
  19. end
  20. it 'should handle non default initialization' do
  21. syslog = Ramaze::Logger::Syslog.new(
  22. 'ramaze_syslog_test', ::Syslog::LOG_NDELAY | ::Syslog::LOG_NOWAIT,
  23. ::Syslog::LOG_DAEMON
  24. )
  25. ::Syslog.opened?.should == true
  26. ::Syslog.ident.should == 'ramaze_syslog_test'
  27. ::Syslog.options.should == ( ::Syslog::LOG_NDELAY | ::Syslog::LOG_NOWAIT )
  28. ::Syslog.facility.should == ::Syslog::LOG_DAEMON
  29. end
  30. # We test the actual logging using a trick found in te test code of the
  31. # syslog module. We create a pipe, fork a child, reroute the childs
  32. # stderr to the pipe. Then we open the logging using LOG_PERROR, so all
  33. # log messages are written to stderror. In the parent we read the messages
  34. # from the pipe and compare them to what we expected.
  35. def test_log_msg( type, priority, msg )
  36. logpipe = IO::pipe
  37. child = fork {
  38. logpipe[0].close
  39. STDERR.reopen(logpipe[1])
  40. syslog = Ramaze::Logger::Syslog.new(
  41. 'ramaze_syslog_test',
  42. ::Syslog::LOG_PID | ::Syslog::LOG_NDELAY | ::Syslog::LOG_PERROR,
  43. ::Syslog::LOG_USER
  44. )
  45. syslog.send priority, msg
  46. Process.exit!( 0 )
  47. }
  48. logpipe[1].close
  49. Process.waitpid(child)
  50. logpipe[0].gets.should == "ramaze_syslog_test[#{child}]: #{msg}\n"
  51. end
  52. it 'should handle debug' do
  53. test_log_msg :direct, :debug, "Hello Debug World"
  54. end
  55. it 'should handle dev' do
  56. test_log_msg :direct, :dev, "Hello Dev World"
  57. end
  58. it 'should handle info' do
  59. test_log_msg :direct, :info, 'Hello Info World!'
  60. end
  61. it 'should handle warn' do
  62. test_log_msg :direct, :warn, 'Hello Warn World!'
  63. end
  64. it 'should handle error' do
  65. test_log_msg :direct, :error, 'Hello Error World!'
  66. end
  67. end