Extract syslog
This commit is contained in:
parent
4d31d6802a
commit
029b6d5b76
@ -1,161 +0,0 @@
|
|||||||
# AUTOGENERATED DEPENDENCIES START
|
|
||||||
syslog.o: $(RUBY_EXTCONF_H)
|
|
||||||
syslog.o: $(arch_hdrdir)/ruby/config.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/assert.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/assume.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/attributes.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/bool.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/inttypes.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/limits.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/long_long.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/stdalign.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/backward/2/stdarg.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/defines.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/intern.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/abi.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/anyargs.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/char.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/double.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/fixnum.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/gid_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/int.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/intptr_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/long.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/long_long.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/mode_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/off_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/pid_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/short.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/size_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/st_data_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/arithmetic/uid_t.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/assume.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/alloc_size.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/artificial.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/cold.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/const.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/constexpr.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/deprecated.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/diagnose_if.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/enum_extensibility.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/error.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/flag_enum.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/forceinline.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/format.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/maybe_unused.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/noalias.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/nodiscard.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/noexcept.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/noinline.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/nonnull.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/noreturn.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/packed_struct.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/pure.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/restrict.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/returns_nonnull.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/warning.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/attr/weakref.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/cast.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_is.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_is/apple.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_is/clang.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_is/gcc.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_is/intel.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_is/msvc.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_is/sunpro.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/compiler_since.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/config.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/constant_p.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rarray.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rbasic.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rbignum.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rclass.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rdata.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rfile.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rhash.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/robject.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rregexp.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rstring.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rstruct.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/core/rtypeddata.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/ctype.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/dllexport.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/dosish.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/error.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/eval.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/event.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/fl_type.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/gc.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/glob.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/globals.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/attribute.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/builtin.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/c_attribute.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/cpp_attribute.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/declspec_attribute.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/extension.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/feature.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/has/warning.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/array.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/bignum.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/class.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/compar.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/complex.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/cont.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/dir.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/enum.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/enumerator.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/error.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/eval.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/file.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/hash.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/io.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/load.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/marshal.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/numeric.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/object.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/parse.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/proc.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/process.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/random.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/range.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/rational.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/re.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/ruby.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/select.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/select/largesize.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/signal.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/sprintf.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/string.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/struct.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/thread.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/time.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/variable.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/intern/vm.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/interpreter.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/iterator.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/memory.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/method.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/module.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/newobj.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/scan_args.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/special_consts.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/static_assert.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/stdalign.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/stdbool.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/symbol.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/value.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/value_type.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/variable.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/warning_push.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/internal/xmalloc.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/missing.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/ruby.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/st.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/subst.h
|
|
||||||
syslog.o: $(hdrdir)/ruby/util.h
|
|
||||||
syslog.o: syslog.c
|
|
||||||
# AUTOGENERATED DEPENDENCIES END
|
|
@ -1,13 +0,0 @@
|
|||||||
# frozen_string_literal: false
|
|
||||||
# $RoughId: extconf.rb,v 1.3 2001/11/24 17:49:26 knu Exp $
|
|
||||||
# $Id$
|
|
||||||
|
|
||||||
require 'mkmf'
|
|
||||||
|
|
||||||
have_library("log") # for Android
|
|
||||||
|
|
||||||
have_header("syslog.h") &&
|
|
||||||
have_func("openlog") &&
|
|
||||||
have_func("setlogmask") &&
|
|
||||||
create_makefile("syslog")
|
|
||||||
|
|
@ -1,209 +0,0 @@
|
|||||||
# frozen_string_literal: false
|
|
||||||
require 'syslog'
|
|
||||||
require 'logger'
|
|
||||||
|
|
||||||
##
|
|
||||||
# Syslog::Logger is a Logger work-alike that logs via syslog instead of to a
|
|
||||||
# file. You can use Syslog::Logger to aggregate logs between multiple
|
|
||||||
# machines.
|
|
||||||
#
|
|
||||||
# By default, Syslog::Logger uses the program name 'ruby', but this can be
|
|
||||||
# changed via the first argument to Syslog::Logger.new.
|
|
||||||
#
|
|
||||||
# NOTE! You can only set the Syslog::Logger program name when you initialize
|
|
||||||
# Syslog::Logger for the first time. This is a limitation of the way
|
|
||||||
# Syslog::Logger uses syslog (and in some ways, a limitation of the way
|
|
||||||
# syslog(3) works). Attempts to change Syslog::Logger's program name after
|
|
||||||
# the first initialization will be ignored.
|
|
||||||
#
|
|
||||||
# === Example
|
|
||||||
#
|
|
||||||
# The following will log to syslogd on your local machine:
|
|
||||||
#
|
|
||||||
# require 'syslog/logger'
|
|
||||||
#
|
|
||||||
# log = Syslog::Logger.new 'my_program'
|
|
||||||
# log.info 'this line will be logged via syslog(3)'
|
|
||||||
#
|
|
||||||
# Also the facility may be set to specify the facility level which will be used:
|
|
||||||
#
|
|
||||||
# log.info 'this line will be logged using Syslog default facility level'
|
|
||||||
#
|
|
||||||
# log_local1 = Syslog::Logger.new 'my_program', Syslog::LOG_LOCAL1
|
|
||||||
# log_local1.info 'this line will be logged using local1 facility level'
|
|
||||||
#
|
|
||||||
#
|
|
||||||
# You may need to perform some syslog.conf setup first. For a BSD machine add
|
|
||||||
# the following lines to /etc/syslog.conf:
|
|
||||||
#
|
|
||||||
# !my_program
|
|
||||||
# *.* /var/log/my_program.log
|
|
||||||
#
|
|
||||||
# Then touch /var/log/my_program.log and signal syslogd with a HUP
|
|
||||||
# (killall -HUP syslogd, on FreeBSD).
|
|
||||||
#
|
|
||||||
# If you wish to have logs automatically roll over and archive, see the
|
|
||||||
# newsyslog.conf(5) and newsyslog(8) man pages.
|
|
||||||
|
|
||||||
class Syslog::Logger
|
|
||||||
# Default formatter for log messages.
|
|
||||||
class Formatter
|
|
||||||
def call severity, time, progname, msg
|
|
||||||
clean msg
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
##
|
|
||||||
# Clean up messages so they're nice and pretty.
|
|
||||||
|
|
||||||
def clean message
|
|
||||||
message = message.to_s.strip
|
|
||||||
message.gsub!(/\e\[[0-9;]*m/, '') # remove useless ansi color codes
|
|
||||||
return message
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# The version of Syslog::Logger you are using.
|
|
||||||
|
|
||||||
VERSION = '2.1.0'
|
|
||||||
|
|
||||||
##
|
|
||||||
# Maps Logger warning types to syslog(3) warning types.
|
|
||||||
#
|
|
||||||
# Messages from Ruby applications are not considered as critical as messages
|
|
||||||
# from other system daemons using syslog(3), so most messages are reduced by
|
|
||||||
# one level. For example, a fatal message for Ruby's Logger is considered
|
|
||||||
# an error for syslog(3).
|
|
||||||
|
|
||||||
LEVEL_MAP = {
|
|
||||||
::Logger::UNKNOWN => Syslog::LOG_ALERT,
|
|
||||||
::Logger::FATAL => Syslog::LOG_ERR,
|
|
||||||
::Logger::ERROR => Syslog::LOG_WARNING,
|
|
||||||
::Logger::WARN => Syslog::LOG_NOTICE,
|
|
||||||
::Logger::INFO => Syslog::LOG_INFO,
|
|
||||||
::Logger::DEBUG => Syslog::LOG_DEBUG,
|
|
||||||
}
|
|
||||||
|
|
||||||
##
|
|
||||||
# Returns the internal Syslog object that is initialized when the
|
|
||||||
# first instance is created.
|
|
||||||
|
|
||||||
def self.syslog
|
|
||||||
@@syslog
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Specifies the internal Syslog object to be used.
|
|
||||||
|
|
||||||
def self.syslog= syslog
|
|
||||||
@@syslog = syslog
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Builds a methods for level +meth+.
|
|
||||||
|
|
||||||
def self.make_methods meth
|
|
||||||
level = ::Logger.const_get(meth.upcase)
|
|
||||||
eval <<-EOM, nil, __FILE__, __LINE__ + 1
|
|
||||||
def #{meth}(message = nil, &block)
|
|
||||||
add(#{level}, message, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def #{meth}?
|
|
||||||
level <= #{level}
|
|
||||||
end
|
|
||||||
EOM
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# :method: unknown
|
|
||||||
#
|
|
||||||
# Logs a +message+ at the unknown (syslog alert) log level, or logs the
|
|
||||||
# message returned from the block.
|
|
||||||
|
|
||||||
##
|
|
||||||
# :method: fatal
|
|
||||||
#
|
|
||||||
# Logs a +message+ at the fatal (syslog err) log level, or logs the message
|
|
||||||
# returned from the block.
|
|
||||||
|
|
||||||
##
|
|
||||||
# :method: error
|
|
||||||
#
|
|
||||||
# Logs a +message+ at the error (syslog warning) log level, or logs the
|
|
||||||
# message returned from the block.
|
|
||||||
|
|
||||||
##
|
|
||||||
# :method: warn
|
|
||||||
#
|
|
||||||
# Logs a +message+ at the warn (syslog notice) log level, or logs the
|
|
||||||
# message returned from the block.
|
|
||||||
|
|
||||||
##
|
|
||||||
# :method: info
|
|
||||||
#
|
|
||||||
# Logs a +message+ at the info (syslog info) log level, or logs the message
|
|
||||||
# returned from the block.
|
|
||||||
|
|
||||||
##
|
|
||||||
# :method: debug
|
|
||||||
#
|
|
||||||
# Logs a +message+ at the debug (syslog debug) log level, or logs the
|
|
||||||
# message returned from the block.
|
|
||||||
|
|
||||||
Logger::Severity::constants.each do |severity|
|
|
||||||
make_methods severity.downcase
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Log level for Logger compatibility.
|
|
||||||
|
|
||||||
attr_accessor :level
|
|
||||||
|
|
||||||
# Logging formatter, as a +Proc+ that will take four arguments and
|
|
||||||
# return the formatted message. The arguments are:
|
|
||||||
#
|
|
||||||
# +severity+:: The Severity of the log message.
|
|
||||||
# +time+:: A Time instance representing when the message was logged.
|
|
||||||
# +progname+:: The #progname configured, or passed to the logger method.
|
|
||||||
# +msg+:: The _Object_ the user passed to the log message; not necessarily a
|
|
||||||
# String.
|
|
||||||
#
|
|
||||||
# The block should return an Object that can be written to the logging
|
|
||||||
# device via +write+. The default formatter is used when no formatter is
|
|
||||||
# set.
|
|
||||||
attr_accessor :formatter
|
|
||||||
|
|
||||||
##
|
|
||||||
# The facility argument is used to specify what type of program is logging the message.
|
|
||||||
|
|
||||||
attr_accessor :facility
|
|
||||||
|
|
||||||
##
|
|
||||||
# Fills in variables for Logger compatibility. If this is the first
|
|
||||||
# instance of Syslog::Logger, +program_name+ may be set to change the logged
|
|
||||||
# program name. The +facility+ may be set to specify the facility level which will be used.
|
|
||||||
#
|
|
||||||
# Due to the way syslog works, only one program name may be chosen.
|
|
||||||
|
|
||||||
def initialize program_name = 'ruby', facility = nil
|
|
||||||
@level = ::Logger::DEBUG
|
|
||||||
@formatter = Formatter.new
|
|
||||||
|
|
||||||
@@syslog ||= Syslog.open(program_name)
|
|
||||||
|
|
||||||
@facility = (facility || @@syslog.facility)
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Almost duplicates Logger#add. +progname+ is ignored.
|
|
||||||
|
|
||||||
def add severity, message = nil, progname = nil, &block
|
|
||||||
severity ||= ::Logger::UNKNOWN
|
|
||||||
level <= severity and
|
|
||||||
@@syslog.log( (LEVEL_MAP[severity] | @facility), '%s', formatter.call(severity, Time.now, progname, (message || block.call)) )
|
|
||||||
true
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,592 +0,0 @@
|
|||||||
/*
|
|
||||||
* UNIX Syslog extension for Ruby
|
|
||||||
* Amos Gouaux, University of Texas at Dallas
|
|
||||||
* <amos+ruby@utdallas.edu>
|
|
||||||
* Documented by mathew <meta@pobox.com>
|
|
||||||
*
|
|
||||||
* $RoughId: syslog.c,v 1.21 2002/02/25 12:21:17 knu Exp $
|
|
||||||
* $Id$
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ruby/ruby.h"
|
|
||||||
#include "ruby/util.h"
|
|
||||||
#include <syslog.h>
|
|
||||||
|
|
||||||
#define SYSLOG_VERSION "0.1.2"
|
|
||||||
|
|
||||||
/* Syslog class */
|
|
||||||
static VALUE mSyslog;
|
|
||||||
/*
|
|
||||||
* Module holding all Syslog constants. See Syslog::log and
|
|
||||||
* Syslog::open for constant descriptions.
|
|
||||||
*/
|
|
||||||
static VALUE mSyslogConstants;
|
|
||||||
/* Module holding Syslog option constants */
|
|
||||||
static VALUE mSyslogOption;
|
|
||||||
/* Module holding Syslog facility constants */
|
|
||||||
static VALUE mSyslogFacility;
|
|
||||||
/* Module holding Syslog level constants */
|
|
||||||
static VALUE mSyslogLevel;
|
|
||||||
/* Module holding Syslog utility macros */
|
|
||||||
static VALUE mSyslogMacros;
|
|
||||||
|
|
||||||
static const char *syslog_ident = NULL;
|
|
||||||
static int syslog_options = -1, syslog_facility = -1, syslog_mask = -1;
|
|
||||||
static int syslog_opened = 0;
|
|
||||||
|
|
||||||
/* Package helper routines */
|
|
||||||
static void syslog_write(int pri, int argc, VALUE *argv)
|
|
||||||
{
|
|
||||||
VALUE str;
|
|
||||||
|
|
||||||
if (argc < 1) {
|
|
||||||
rb_raise(rb_eArgError, "no log message supplied");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!syslog_opened) {
|
|
||||||
rb_raise(rb_eRuntimeError, "must open syslog before write");
|
|
||||||
}
|
|
||||||
|
|
||||||
str = rb_f_sprintf(argc, argv);
|
|
||||||
|
|
||||||
syslog(pri, "%s", RSTRING_PTR(str));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Closes the syslog facility.
|
|
||||||
* Raises a runtime exception if it is not open.
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_close(VALUE self)
|
|
||||||
{
|
|
||||||
if (!syslog_opened) {
|
|
||||||
rb_raise(rb_eRuntimeError, "syslog not opened");
|
|
||||||
}
|
|
||||||
|
|
||||||
closelog();
|
|
||||||
|
|
||||||
xfree((void *)syslog_ident);
|
|
||||||
syslog_ident = NULL;
|
|
||||||
syslog_options = syslog_facility = syslog_mask = -1;
|
|
||||||
syslog_opened = 0;
|
|
||||||
|
|
||||||
return Qnil;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* open(ident, options, facility) => syslog
|
|
||||||
*
|
|
||||||
* :yields: syslog
|
|
||||||
*
|
|
||||||
* Open the syslog facility.
|
|
||||||
* Raises a runtime exception if it is already open.
|
|
||||||
*
|
|
||||||
* Can be called with or without a code block. If called with a block, the
|
|
||||||
* Syslog object created is passed to the block.
|
|
||||||
*
|
|
||||||
* If the syslog is already open, raises a RuntimeError.
|
|
||||||
*
|
|
||||||
* +ident+ is a String which identifies the calling program.
|
|
||||||
*
|
|
||||||
* +options+ is the logical OR of any of the following:
|
|
||||||
*
|
|
||||||
* LOG_CONS:: If there is an error while sending to the system logger,
|
|
||||||
* write directly to the console instead.
|
|
||||||
*
|
|
||||||
* LOG_NDELAY:: Open the connection now, rather than waiting for the first
|
|
||||||
* message to be written.
|
|
||||||
*
|
|
||||||
* LOG_NOWAIT:: Don't wait for any child processes created while logging
|
|
||||||
* messages. (Has no effect on Linux.)
|
|
||||||
*
|
|
||||||
* LOG_ODELAY:: Opposite of LOG_NDELAY; wait until a message is sent before
|
|
||||||
* opening the connection. (This is the default.)
|
|
||||||
*
|
|
||||||
* LOG_PERROR:: Print the message to stderr as well as sending it to syslog.
|
|
||||||
* (Not in POSIX.1-2001.)
|
|
||||||
*
|
|
||||||
* LOG_PID:: Include the current process ID with each message.
|
|
||||||
*
|
|
||||||
* +facility+ describes the type of program opening the syslog, and is
|
|
||||||
* the logical OR of any of the following which are defined for the host OS:
|
|
||||||
*
|
|
||||||
* LOG_AUTH:: Security or authorization. Deprecated, use LOG_AUTHPRIV
|
|
||||||
* instead.
|
|
||||||
*
|
|
||||||
* LOG_AUTHPRIV:: Security or authorization messages which should be kept
|
|
||||||
* private.
|
|
||||||
*
|
|
||||||
* LOG_CONSOLE:: System console message.
|
|
||||||
*
|
|
||||||
* LOG_CRON:: System task scheduler (cron or at).
|
|
||||||
*
|
|
||||||
* LOG_DAEMON:: A system daemon which has no facility value of its own.
|
|
||||||
*
|
|
||||||
* LOG_FTP:: An FTP server.
|
|
||||||
*
|
|
||||||
* LOG_KERN:: A kernel message (not sendable by user processes, so not of
|
|
||||||
* much use to Ruby, but listed here for completeness).
|
|
||||||
*
|
|
||||||
* LOG_LPR:: Line printer subsystem.
|
|
||||||
*
|
|
||||||
* LOG_MAIL:: Mail delivery or transport subsystem.
|
|
||||||
*
|
|
||||||
* LOG_NEWS:: Usenet news system.
|
|
||||||
*
|
|
||||||
* LOG_NTP:: Network Time Protocol server.
|
|
||||||
*
|
|
||||||
* LOG_SECURITY:: General security message.
|
|
||||||
*
|
|
||||||
* LOG_SYSLOG:: Messages generated internally by syslog.
|
|
||||||
*
|
|
||||||
* LOG_USER:: Generic user-level message.
|
|
||||||
*
|
|
||||||
* LOG_UUCP:: UUCP subsystem.
|
|
||||||
*
|
|
||||||
* LOG_LOCAL0 to LOG_LOCAL7:: Locally-defined facilities.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
*
|
|
||||||
* Syslog.open("webrick", Syslog::LOG_PID,
|
|
||||||
* Syslog::LOG_DAEMON | Syslog::LOG_LOCAL3)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
|
|
||||||
{
|
|
||||||
VALUE ident, opt, fac;
|
|
||||||
const char *ident_ptr;
|
|
||||||
|
|
||||||
if (syslog_opened) {
|
|
||||||
rb_raise(rb_eRuntimeError, "syslog already open");
|
|
||||||
}
|
|
||||||
|
|
||||||
rb_scan_args(argc, argv, "03", &ident, &opt, &fac);
|
|
||||||
|
|
||||||
if (NIL_P(ident)) {
|
|
||||||
ident = rb_gv_get("$0");
|
|
||||||
}
|
|
||||||
ident_ptr = StringValueCStr(ident);
|
|
||||||
syslog_ident = strdup(ident_ptr);
|
|
||||||
|
|
||||||
if (NIL_P(opt)) {
|
|
||||||
syslog_options = LOG_PID | LOG_CONS;
|
|
||||||
} else {
|
|
||||||
syslog_options = NUM2INT(opt);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NIL_P(fac)) {
|
|
||||||
syslog_facility = LOG_USER;
|
|
||||||
} else {
|
|
||||||
syslog_facility = NUM2INT(fac);
|
|
||||||
}
|
|
||||||
|
|
||||||
openlog(syslog_ident, syslog_options, syslog_facility);
|
|
||||||
|
|
||||||
syslog_opened = 1;
|
|
||||||
|
|
||||||
setlogmask(syslog_mask = setlogmask(0));
|
|
||||||
|
|
||||||
/* be like File.new.open {...} */
|
|
||||||
if (rb_block_given_p()) {
|
|
||||||
rb_ensure(rb_yield, self, mSyslog_close, self);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* reopen(ident, options, facility) => syslog
|
|
||||||
*
|
|
||||||
* :yields: syslog
|
|
||||||
*
|
|
||||||
* Closes and then reopens the syslog.
|
|
||||||
*
|
|
||||||
* Arguments are the same as for open().
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
|
|
||||||
{
|
|
||||||
mSyslog_close(self);
|
|
||||||
|
|
||||||
return mSyslog_open(argc, argv, self);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* opened?
|
|
||||||
*
|
|
||||||
* Returns true if the syslog is open.
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_isopen(VALUE self)
|
|
||||||
{
|
|
||||||
return syslog_opened ? Qtrue : Qfalse;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns the identity string used in the last call to open()
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_ident(VALUE self)
|
|
||||||
{
|
|
||||||
return syslog_opened ? rb_str_new2(syslog_ident) : Qnil;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns the options bitmask used in the last call to open()
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_options(VALUE self)
|
|
||||||
{
|
|
||||||
return syslog_opened ? INT2NUM(syslog_options) : Qnil;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns the facility number used in the last call to open()
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_facility(VALUE self)
|
|
||||||
{
|
|
||||||
return syslog_opened ? INT2NUM(syslog_facility) : Qnil;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns the log priority mask in effect. The mask is not reset by opening
|
|
||||||
* or closing syslog.
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_get_mask(VALUE self)
|
|
||||||
{
|
|
||||||
return syslog_opened ? INT2NUM(syslog_mask) : Qnil;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* mask=(priority_mask)
|
|
||||||
*
|
|
||||||
* Sets the log priority mask. A method LOG_UPTO is defined to make it easier
|
|
||||||
* to set mask values. Example:
|
|
||||||
*
|
|
||||||
* Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
|
|
||||||
*
|
|
||||||
* Alternatively, specific priorities can be selected and added together using
|
|
||||||
* binary OR. Example:
|
|
||||||
*
|
|
||||||
* Syslog.mask = Syslog::LOG_MASK(Syslog::LOG_ERR) | Syslog::LOG_MASK(Syslog::LOG_CRIT)
|
|
||||||
*
|
|
||||||
* The priority mask persists through calls to open() and close().
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
|
|
||||||
{
|
|
||||||
if (!syslog_opened) {
|
|
||||||
rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
|
|
||||||
}
|
|
||||||
|
|
||||||
setlogmask(syslog_mask = NUM2INT(mask));
|
|
||||||
|
|
||||||
return mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* log(priority, format_string, *format_args)
|
|
||||||
*
|
|
||||||
* Log a message with the specified priority. Example:
|
|
||||||
*
|
|
||||||
* Syslog.log(Syslog::LOG_CRIT, "Out of disk space")
|
|
||||||
* Syslog.log(Syslog::LOG_CRIT, "User %s logged in", ENV['USER'])
|
|
||||||
*
|
|
||||||
* The priority levels, in descending order, are:
|
|
||||||
*
|
|
||||||
* LOG_EMERG:: System is unusable
|
|
||||||
* LOG_ALERT:: Action needs to be taken immediately
|
|
||||||
* LOG_CRIT:: A critical condition has occurred
|
|
||||||
* LOG_ERR:: An error occurred
|
|
||||||
* LOG_WARNING:: Warning of a possible problem
|
|
||||||
* LOG_NOTICE:: A normal but significant condition occurred
|
|
||||||
* LOG_INFO:: Informational message
|
|
||||||
* LOG_DEBUG:: Debugging information
|
|
||||||
*
|
|
||||||
* Each priority level also has a shortcut method that logs with it's named priority.
|
|
||||||
* As an example, the two following statements would produce the same result:
|
|
||||||
*
|
|
||||||
* Syslog.log(Syslog::LOG_ALERT, "Out of memory")
|
|
||||||
* Syslog.alert("Out of memory")
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
|
|
||||||
{
|
|
||||||
VALUE pri;
|
|
||||||
|
|
||||||
rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
|
|
||||||
|
|
||||||
argc--;
|
|
||||||
pri = *argv++;
|
|
||||||
|
|
||||||
if (!FIXNUM_P(pri)) {
|
|
||||||
rb_raise(rb_eTypeError, "type mismatch: %"PRIsVALUE" given", rb_obj_class(pri));
|
|
||||||
}
|
|
||||||
|
|
||||||
syslog_write(FIX2INT(pri), argc, argv);
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns an inspect() string summarizing the object state.
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_inspect(VALUE self)
|
|
||||||
{
|
|
||||||
Check_Type(self, T_MODULE);
|
|
||||||
|
|
||||||
if (!syslog_opened)
|
|
||||||
return rb_sprintf("<#%"PRIsVALUE": opened=false>", self);
|
|
||||||
|
|
||||||
return rb_sprintf("<#%"PRIsVALUE": opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>",
|
|
||||||
self,
|
|
||||||
syslog_ident,
|
|
||||||
syslog_options,
|
|
||||||
syslog_facility,
|
|
||||||
syslog_mask);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns self, for backward compatibility.
|
|
||||||
*/
|
|
||||||
static VALUE mSyslog_instance(VALUE self)
|
|
||||||
{
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define define_syslog_shortcut_method(pri, name) \
|
|
||||||
static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \
|
|
||||||
{ \
|
|
||||||
syslog_write((pri), argc, argv); \
|
|
||||||
\
|
|
||||||
return self; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef LOG_EMERG
|
|
||||||
define_syslog_shortcut_method(LOG_EMERG, emerg)
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_ALERT
|
|
||||||
define_syslog_shortcut_method(LOG_ALERT, alert)
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_CRIT
|
|
||||||
define_syslog_shortcut_method(LOG_CRIT, crit)
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_ERR
|
|
||||||
define_syslog_shortcut_method(LOG_ERR, err)
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_WARNING
|
|
||||||
define_syslog_shortcut_method(LOG_WARNING, warning)
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_NOTICE
|
|
||||||
define_syslog_shortcut_method(LOG_NOTICE, notice)
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_INFO
|
|
||||||
define_syslog_shortcut_method(LOG_INFO, info)
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_DEBUG
|
|
||||||
define_syslog_shortcut_method(LOG_DEBUG, debug)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* LOG_MASK(priority_level) => priority_mask
|
|
||||||
*
|
|
||||||
* Generates a mask bit for a priority level. See #mask=
|
|
||||||
*/
|
|
||||||
static VALUE mSyslogMacros_LOG_MASK(VALUE mod, VALUE pri)
|
|
||||||
{
|
|
||||||
return INT2FIX(LOG_MASK(NUM2INT(pri)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call-seq:
|
|
||||||
* LOG_UPTO(priority_level) => priority_mask
|
|
||||||
*
|
|
||||||
* Generates a mask value for priority levels at or below the level specified.
|
|
||||||
* See #mask=
|
|
||||||
*/
|
|
||||||
static VALUE mSyslogMacros_LOG_UPTO(VALUE mod, VALUE pri)
|
|
||||||
{
|
|
||||||
return INT2FIX(LOG_UPTO(NUM2INT(pri)));
|
|
||||||
}
|
|
||||||
|
|
||||||
static VALUE mSyslogMacros_included(VALUE mod, VALUE target)
|
|
||||||
{
|
|
||||||
rb_extend_object(target, mSyslogMacros);
|
|
||||||
return mod;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The syslog package provides a Ruby interface to the POSIX system logging
|
|
||||||
* facility.
|
|
||||||
*
|
|
||||||
* Syslog messages are typically passed to a central logging daemon.
|
|
||||||
* The daemon may filter them; route them into different files (usually
|
|
||||||
* found under /var/log); place them in SQL databases; forward
|
|
||||||
* them to centralized logging servers via TCP or UDP; or even alert the
|
|
||||||
* system administrator via email, pager or text message.
|
|
||||||
*
|
|
||||||
* Unlike application-level logging via Logger or Log4r, syslog is designed
|
|
||||||
* to allow secure tamper-proof logging.
|
|
||||||
*
|
|
||||||
* The syslog protocol is standardized in RFC 5424.
|
|
||||||
*/
|
|
||||||
void Init_syslog(void)
|
|
||||||
{
|
|
||||||
#undef rb_intern
|
|
||||||
mSyslog = rb_define_module("Syslog");
|
|
||||||
|
|
||||||
mSyslogConstants = rb_define_module_under(mSyslog, "Constants");
|
|
||||||
|
|
||||||
mSyslogOption = rb_define_module_under(mSyslog, "Option");
|
|
||||||
mSyslogFacility = rb_define_module_under(mSyslog, "Facility");
|
|
||||||
mSyslogLevel = rb_define_module_under(mSyslog, "Level");
|
|
||||||
mSyslogMacros = rb_define_module_under(mSyslog, "Macros");
|
|
||||||
|
|
||||||
rb_define_module_function(mSyslog, "open", mSyslog_open, -1);
|
|
||||||
rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1);
|
|
||||||
rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1);
|
|
||||||
rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0);
|
|
||||||
|
|
||||||
rb_define_module_function(mSyslog, "ident", mSyslog_ident, 0);
|
|
||||||
rb_define_module_function(mSyslog, "options", mSyslog_options, 0);
|
|
||||||
rb_define_module_function(mSyslog, "facility", mSyslog_facility, 0);
|
|
||||||
|
|
||||||
rb_define_module_function(mSyslog, "log", mSyslog_log, -1);
|
|
||||||
rb_define_module_function(mSyslog, "close", mSyslog_close, 0);
|
|
||||||
rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0);
|
|
||||||
rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1);
|
|
||||||
|
|
||||||
rb_define_singleton_method(mSyslog, "inspect", mSyslog_inspect, 0);
|
|
||||||
rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0);
|
|
||||||
|
|
||||||
/* Syslog options */
|
|
||||||
|
|
||||||
#define rb_define_syslog_option(c) \
|
|
||||||
rb_define_const(mSyslogOption, #c, INT2NUM(c))
|
|
||||||
|
|
||||||
#ifdef LOG_PID
|
|
||||||
rb_define_syslog_option(LOG_PID);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_CONS
|
|
||||||
rb_define_syslog_option(LOG_CONS);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_ODELAY
|
|
||||||
rb_define_syslog_option(LOG_ODELAY); /* deprecated */
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_NDELAY
|
|
||||||
rb_define_syslog_option(LOG_NDELAY);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_NOWAIT
|
|
||||||
rb_define_syslog_option(LOG_NOWAIT); /* deprecated */
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_PERROR
|
|
||||||
rb_define_syslog_option(LOG_PERROR);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Syslog facilities */
|
|
||||||
|
|
||||||
#define rb_define_syslog_facility(c) \
|
|
||||||
rb_define_const(mSyslogFacility, #c, INT2NUM(c))
|
|
||||||
|
|
||||||
#ifdef LOG_AUTH
|
|
||||||
rb_define_syslog_facility(LOG_AUTH);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_AUTHPRIV
|
|
||||||
rb_define_syslog_facility(LOG_AUTHPRIV);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_CONSOLE
|
|
||||||
rb_define_syslog_facility(LOG_CONSOLE);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_CRON
|
|
||||||
rb_define_syslog_facility(LOG_CRON);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_DAEMON
|
|
||||||
rb_define_syslog_facility(LOG_DAEMON);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_FTP
|
|
||||||
rb_define_syslog_facility(LOG_FTP);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_KERN
|
|
||||||
rb_define_syslog_facility(LOG_KERN);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LPR
|
|
||||||
rb_define_syslog_facility(LOG_LPR);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_MAIL
|
|
||||||
rb_define_syslog_facility(LOG_MAIL);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_NEWS
|
|
||||||
rb_define_syslog_facility(LOG_NEWS);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_NTP
|
|
||||||
rb_define_syslog_facility(LOG_NTP);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_SECURITY
|
|
||||||
rb_define_syslog_facility(LOG_SECURITY);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_SYSLOG
|
|
||||||
rb_define_syslog_facility(LOG_SYSLOG);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_USER
|
|
||||||
rb_define_syslog_facility(LOG_USER);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_UUCP
|
|
||||||
rb_define_syslog_facility(LOG_UUCP);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL0
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL0);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL1
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL1);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL2
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL2);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL3
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL3);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL4
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL4);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL5
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL5);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL6
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL6);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_LOCAL7
|
|
||||||
rb_define_syslog_facility(LOG_LOCAL7);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Syslog levels and the shortcut methods */
|
|
||||||
|
|
||||||
#define rb_define_syslog_level(c, m) \
|
|
||||||
rb_define_const(mSyslogLevel, #c, INT2NUM(c)); \
|
|
||||||
rb_define_module_function(mSyslog, #m, mSyslog_##m, -1)
|
|
||||||
|
|
||||||
#ifdef LOG_EMERG
|
|
||||||
rb_define_syslog_level(LOG_EMERG, emerg);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_ALERT
|
|
||||||
rb_define_syslog_level(LOG_ALERT, alert);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_CRIT
|
|
||||||
rb_define_syslog_level(LOG_CRIT, crit);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_ERR
|
|
||||||
rb_define_syslog_level(LOG_ERR, err);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_WARNING
|
|
||||||
rb_define_syslog_level(LOG_WARNING, warning);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_NOTICE
|
|
||||||
rb_define_syslog_level(LOG_NOTICE, notice);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_INFO
|
|
||||||
rb_define_syslog_level(LOG_INFO, info);
|
|
||||||
#endif
|
|
||||||
#ifdef LOG_DEBUG
|
|
||||||
rb_define_syslog_level(LOG_DEBUG, debug);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Syslog macros */
|
|
||||||
|
|
||||||
rb_define_const(mSyslog, "VERSION", rb_str_new_cstr(SYSLOG_VERSION));
|
|
||||||
|
|
||||||
rb_define_method(mSyslogMacros, "LOG_MASK", mSyslogMacros_LOG_MASK, 1);
|
|
||||||
rb_define_method(mSyslogMacros, "LOG_UPTO", mSyslogMacros_LOG_UPTO, 1);
|
|
||||||
rb_define_singleton_method(mSyslogMacros, "included", mSyslogMacros_included, 1);
|
|
||||||
|
|
||||||
rb_include_module(mSyslogConstants, mSyslogOption);
|
|
||||||
rb_include_module(mSyslogConstants, mSyslogFacility);
|
|
||||||
rb_include_module(mSyslogConstants, mSyslogLevel);
|
|
||||||
rb_funcall(mSyslogConstants, rb_intern("include"), 1, mSyslogMacros);
|
|
||||||
|
|
||||||
rb_define_singleton_method(mSyslogConstants, "included", mSyslogMacros_included, 1);
|
|
||||||
rb_funcall(mSyslog, rb_intern("include"), 1, mSyslogConstants);
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
source_version = %w[. ext/syslog].find do |dir|
|
|
||||||
break $1 if File.foreach(File.join(__dir__, dir, "syslog.c")).any?(/^#define\s+SYSLOG_VERSION\s+"(.+)"/)
|
|
||||||
rescue Errno::ENOENT
|
|
||||||
end
|
|
||||||
|
|
||||||
Gem::Specification.new do |spec|
|
|
||||||
spec.name = "syslog"
|
|
||||||
spec.version = source_version
|
|
||||||
spec.authors = ["Akinori MUSHA"]
|
|
||||||
spec.email = ["knu@idaemons.org"]
|
|
||||||
|
|
||||||
spec.summary = %q{Ruby interface for the POSIX system logging facility.}
|
|
||||||
spec.description = %q{Ruby interface for the POSIX system logging facility.}
|
|
||||||
spec.homepage = "https://github.com/ruby/syslog"
|
|
||||||
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
|
||||||
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
|
||||||
|
|
||||||
spec.metadata["homepage_uri"] = spec.homepage
|
|
||||||
spec.metadata["source_code_uri"] = spec.homepage
|
|
||||||
|
|
||||||
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
||||||
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
||||||
end
|
|
||||||
spec.extensions = ["ext/syslog/extconf.rb"]
|
|
||||||
spec.bindir = "exe"
|
|
||||||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
||||||
spec.require_paths = ["lib"]
|
|
||||||
end
|
|
@ -1,124 +0,0 @@
|
|||||||
.\" syslog.txt - -*- Indented-Text -*-
|
|
||||||
$RoughId: syslog.txt,v 1.18 2002/02/25 08:20:14 knu Exp $
|
|
||||||
$Id$
|
|
||||||
|
|
||||||
UNIX Syslog extension for Ruby
|
|
||||||
Amos Gouaux, University of Texas at Dallas
|
|
||||||
<amos+ruby@utdallas.edu>
|
|
||||||
&
|
|
||||||
Akinori MUSHA
|
|
||||||
<knu@iDaemons.org>
|
|
||||||
|
|
||||||
Contact:
|
|
||||||
- Akinori MUSHA <knu@iDaemons.org> (current maintainer)
|
|
||||||
|
|
||||||
** Syslog(Module)
|
|
||||||
|
|
||||||
Included Modules: Syslog::Constants
|
|
||||||
|
|
||||||
require 'syslog'
|
|
||||||
|
|
||||||
A Simple wrapper for the UNIX syslog system calls that might be handy
|
|
||||||
if you're writing a server in Ruby. For the details of the syslog(8)
|
|
||||||
architecture and constants, see the syslog(3) manual page of your
|
|
||||||
platform.
|
|
||||||
|
|
||||||
Module Methods:
|
|
||||||
|
|
||||||
open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS,
|
|
||||||
facility = Syslog::LOG_USER) [{ |syslog| ... }]
|
|
||||||
|
|
||||||
Opens syslog with the given options and returns the module
|
|
||||||
itself. If a block is given, calls it with an argument of
|
|
||||||
itself. If syslog is already opened, raises RuntimeError.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY,
|
|
||||||
Syslog::LOG_FTP)
|
|
||||||
|
|
||||||
open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS,
|
|
||||||
facility = Syslog::LOG_USER)
|
|
||||||
reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS,
|
|
||||||
facility = Syslog::LOG_USER)
|
|
||||||
|
|
||||||
Same as open, but does a close first.
|
|
||||||
|
|
||||||
opened?
|
|
||||||
|
|
||||||
Returns true if syslog opened, otherwise false.
|
|
||||||
|
|
||||||
ident
|
|
||||||
options
|
|
||||||
facility
|
|
||||||
|
|
||||||
Returns the parameters given in the last open, respectively.
|
|
||||||
Every call of Syslog::open resets these values.
|
|
||||||
|
|
||||||
log(pri, message, ...)
|
|
||||||
|
|
||||||
Writes message to syslog.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
Syslog.log(Syslog::LOG_CRIT, "the sky is falling in %d seconds!", 10)
|
|
||||||
|
|
||||||
crit(message, ...)
|
|
||||||
emerg(message, ...)
|
|
||||||
alert(message, ...)
|
|
||||||
err(message, ...)
|
|
||||||
warning(message, ...)
|
|
||||||
notice(message, ...)
|
|
||||||
info(message, ...)
|
|
||||||
debug(message, ...)
|
|
||||||
|
|
||||||
These are shortcut methods of Syslog::log(). The lineup may
|
|
||||||
vary depending on what priorities are defined on your system.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
Syslog.crit("the sky is falling in %d seconds!", 5)
|
|
||||||
|
|
||||||
mask
|
|
||||||
mask=(mask)
|
|
||||||
|
|
||||||
Returns or sets the log priority mask. The value of the mask
|
|
||||||
is persistent and will not be reset by Syslog::open or
|
|
||||||
Syslog::close.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
|
|
||||||
|
|
||||||
close
|
|
||||||
|
|
||||||
Closes syslog.
|
|
||||||
|
|
||||||
inspect
|
|
||||||
|
|
||||||
Returns the "inspect" string of the Syslog module.
|
|
||||||
|
|
||||||
instance
|
|
||||||
|
|
||||||
Returns the module itself. (Just for backward compatibility)
|
|
||||||
|
|
||||||
LOG_MASK(pri)
|
|
||||||
|
|
||||||
Creates a mask for one priority.
|
|
||||||
|
|
||||||
LOG_UPTO(pri)
|
|
||||||
|
|
||||||
Creates a mask for all priorities up to pri.
|
|
||||||
|
|
||||||
** Syslog::Constants(Module)
|
|
||||||
|
|
||||||
require 'syslog'
|
|
||||||
include Syslog::Constants
|
|
||||||
|
|
||||||
This module includes the LOG_* constants available on the system.
|
|
||||||
|
|
||||||
Module Methods:
|
|
||||||
|
|
||||||
LOG_MASK(pri)
|
|
||||||
|
|
||||||
Creates a mask for one priority.
|
|
||||||
|
|
||||||
LOG_UPTO(pri)
|
|
||||||
|
|
||||||
Creates a mask for all priorities up to pri.
|
|
@ -31,3 +31,4 @@ resolv-replace 0.1.1 https://github.com/ruby/resolv-replace
|
|||||||
rinda 0.2.0 https://github.com/ruby/rinda
|
rinda 0.2.0 https://github.com/ruby/rinda
|
||||||
drb 2.2.0 https://github.com/ruby/drb
|
drb 2.2.0 https://github.com/ruby/drb
|
||||||
nkf 0.2.0 https://github.com/ruby/nkf
|
nkf 0.2.0 https://github.com/ruby/nkf
|
||||||
|
syslog 0.1.2 https://github.com/ruby/syslog
|
||||||
|
@ -1,588 +0,0 @@
|
|||||||
# coding: US-ASCII
|
|
||||||
# frozen_string_literal: false
|
|
||||||
require 'test/unit'
|
|
||||||
require 'tempfile'
|
|
||||||
begin
|
|
||||||
require 'syslog/logger'
|
|
||||||
rescue LoadError
|
|
||||||
# skip. see the bottom of this file.
|
|
||||||
end
|
|
||||||
|
|
||||||
# These tests ensure Syslog::Logger works like Logger
|
|
||||||
|
|
||||||
class TestSyslogRootLogger < Test::Unit::TestCase
|
|
||||||
|
|
||||||
module MockSyslog
|
|
||||||
|
|
||||||
PRIMASK = Syslog::Level.constants.inject(0) { |mask, name| mask | Syslog::Level.const_get(name) }
|
|
||||||
|
|
||||||
LEVEL_LABEL_MAP = {
|
|
||||||
Syslog::LOG_ALERT => 'ALERT',
|
|
||||||
Syslog::LOG_ERR => 'ERR',
|
|
||||||
Syslog::LOG_WARNING => 'WARNING',
|
|
||||||
Syslog::LOG_NOTICE => 'NOTICE',
|
|
||||||
Syslog::LOG_INFO => 'INFO',
|
|
||||||
Syslog::LOG_DEBUG => 'DEBUG'
|
|
||||||
}
|
|
||||||
|
|
||||||
@facility = Syslog::LOG_USER
|
|
||||||
|
|
||||||
class << self
|
|
||||||
|
|
||||||
attr_reader :facility
|
|
||||||
attr_reader :line
|
|
||||||
attr_reader :program_name
|
|
||||||
|
|
||||||
def log(priority, format, *args)
|
|
||||||
level = priority & PRIMASK
|
|
||||||
@line = "<#{priority}> #{LEVEL_LABEL_MAP[level]} - #{format % args}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def open(program_name)
|
|
||||||
@program_name = program_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def reset
|
|
||||||
@line = ''
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
Syslog::Logger.syslog = MockSyslog
|
|
||||||
|
|
||||||
LEVEL_LABEL_MAP = {
|
|
||||||
Logger::DEBUG => 'DEBUG',
|
|
||||||
Logger::INFO => 'INFO',
|
|
||||||
Logger::WARN => 'WARN',
|
|
||||||
Logger::ERROR => 'ERROR',
|
|
||||||
Logger::FATAL => 'FATAL',
|
|
||||||
Logger::UNKNOWN => 'ANY',
|
|
||||||
}
|
|
||||||
|
|
||||||
def setup
|
|
||||||
@logger = Logger.new(nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
class Log
|
|
||||||
attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
|
|
||||||
def initialize(line)
|
|
||||||
@line = line
|
|
||||||
/\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ @line
|
|
||||||
@label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def log_add(severity, msg, progname = nil, &block)
|
|
||||||
log(:add, severity, msg, progname, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def log(msg_id, *arg, &block)
|
|
||||||
Log.new(log_raw(msg_id, *arg, &block))
|
|
||||||
end
|
|
||||||
|
|
||||||
def log_raw(msg_id, *arg, &block)
|
|
||||||
Tempfile.create(File.basename(__FILE__) + '.log') {|logdev|
|
|
||||||
@logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
|
|
||||||
assert_equal true, @logger.__send__(msg_id, *arg, &block)
|
|
||||||
logdev.rewind
|
|
||||||
logdev.read
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_initialize
|
|
||||||
assert_equal Logger::DEBUG, @logger.level
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_custom_formatter
|
|
||||||
@logger.formatter = Class.new {
|
|
||||||
def call severity, time, progname, msg
|
|
||||||
"hi mom!"
|
|
||||||
end
|
|
||||||
}.new
|
|
||||||
|
|
||||||
assert_match(/hi mom!/, log_raw(:fatal, 'fatal level message'))
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add_level_unknown
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add_level_fatal
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add_level_error
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add_level_warn
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add_level_info
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add_level_debug
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_unknown
|
|
||||||
msg = log :unknown, 'unknown level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
msg = log :unknown, 'unknown level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
msg = log :unknown, 'unknown level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
msg = log :unknown, 'unknown level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
msg = log :unknown, 'unknown level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
msg = log :unknown, 'unknown level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
msg = log :unknown, 'unknown level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_fatal
|
|
||||||
msg = log :fatal, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
msg = log :fatal, 'fatal level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
msg = log :fatal, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
msg = log :fatal, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
msg = log :fatal, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
msg = log :fatal, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
msg = log :fatal, 'fatal level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_fatal_eh
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
assert_equal true, @logger.fatal?
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
assert_equal false, @logger.fatal?
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_error
|
|
||||||
msg = log :error, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
msg = log :error, 'error level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
msg = log :error, 'error level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
msg = log :error, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
msg = log :error, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
msg = log :error, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
msg = log :error, 'error level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_error_eh
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
assert_equal true, @logger.error?
|
|
||||||
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
assert_equal false, @logger.error?
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_warn
|
|
||||||
msg = log :warn, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
msg = log :warn, 'warn level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
msg = log :warn, 'warn level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
msg = log :warn, 'warn level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
msg = log :warn, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
msg = log :warn, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
msg = log :warn, 'warn level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_warn_eh
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
assert_equal true, @logger.warn?
|
|
||||||
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
assert_equal false, @logger.warn?
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_info
|
|
||||||
msg = log :info, 'info level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
msg = log :info, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
msg = log :info, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
msg = log :info, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
msg = log :info, 'info level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
msg = log :info, 'info level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
msg = log :info, 'info level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_info_eh
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
assert_equal true, @logger.info?
|
|
||||||
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
assert_equal false, @logger.info?
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_debug
|
|
||||||
msg = log :debug, 'debug level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
msg = log :debug, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::FATAL
|
|
||||||
msg = log :debug, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::ERROR
|
|
||||||
msg = log :debug, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::WARN
|
|
||||||
msg = log :debug, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
msg = log :debug, 'debug level message'
|
|
||||||
assert_equal '', msg.line
|
|
||||||
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
msg = log :debug, 'debug level message'
|
|
||||||
assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_debug_eh
|
|
||||||
@logger.level = Logger::DEBUG
|
|
||||||
assert_equal true, @logger.debug?
|
|
||||||
|
|
||||||
@logger.level = Logger::INFO
|
|
||||||
assert_equal false, @logger.debug?
|
|
||||||
end
|
|
||||||
|
|
||||||
end if defined?(Syslog)
|
|
||||||
|
|
||||||
class TestSyslogLogger < TestSyslogRootLogger
|
|
||||||
|
|
||||||
@facility = Syslog::LOG_USER
|
|
||||||
|
|
||||||
def facility
|
|
||||||
self.class.instance_variable_get("@facility")
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup
|
|
||||||
super
|
|
||||||
@logger = Syslog::Logger.new
|
|
||||||
end
|
|
||||||
|
|
||||||
SEVERITY_MAP = {}.tap { |map|
|
|
||||||
level2severity = Syslog::Logger::LEVEL_MAP.invert
|
|
||||||
|
|
||||||
MockSyslog::LEVEL_LABEL_MAP.each { |level, name|
|
|
||||||
map[name] = TestSyslogRootLogger::LEVEL_LABEL_MAP[level2severity[level]]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Log
|
|
||||||
attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg, :priority
|
|
||||||
def initialize(line)
|
|
||||||
@line = line
|
|
||||||
return unless /\A<(\d+)> (\w+) - (.*)\Z/ =~ @line
|
|
||||||
priority, severity, @msg = $1, $2, $3
|
|
||||||
@severity = SEVERITY_MAP[severity]
|
|
||||||
@priority = priority.to_i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def log_add(severity, msg, progname = nil, &block)
|
|
||||||
log(:add, severity, msg, progname, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def log(msg_id, *arg, &block)
|
|
||||||
Log.new(log_raw(msg_id, *arg, &block))
|
|
||||||
end
|
|
||||||
|
|
||||||
def log_raw(msg_id, *arg, &block)
|
|
||||||
assert_equal true, @logger.__send__(msg_id, *arg, &block)
|
|
||||||
msg = MockSyslog.line
|
|
||||||
MockSyslog.reset
|
|
||||||
return msg
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_unknown_eh
|
|
||||||
@logger.level = Logger::UNKNOWN
|
|
||||||
assert_equal true, @logger.unknown?
|
|
||||||
|
|
||||||
@logger.level = Logger::UNKNOWN + 1
|
|
||||||
assert_equal false, @logger.unknown?
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_facility
|
|
||||||
assert_equal facility, @logger.facility
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_priority
|
|
||||||
msg = log_add nil, 'unknown level message' # nil == unknown
|
|
||||||
assert_equal facility|Syslog::LOG_ALERT, msg.priority
|
|
||||||
|
|
||||||
msg = log_add Logger::FATAL, 'fatal level message'
|
|
||||||
assert_equal facility|Syslog::LOG_ERR, msg.priority
|
|
||||||
|
|
||||||
msg = log_add Logger::ERROR, 'error level message'
|
|
||||||
assert_equal facility|Syslog::LOG_WARNING, msg.priority
|
|
||||||
|
|
||||||
msg = log_add Logger::WARN, 'warn level message'
|
|
||||||
assert_equal facility|Syslog::LOG_NOTICE, msg.priority
|
|
||||||
|
|
||||||
msg = log_add Logger::INFO, 'info level message'
|
|
||||||
assert_equal facility|Syslog::LOG_INFO, msg.priority
|
|
||||||
|
|
||||||
msg = log_add Logger::DEBUG, 'debug level message'
|
|
||||||
assert_equal facility|Syslog::LOG_DEBUG, msg.priority
|
|
||||||
end
|
|
||||||
|
|
||||||
class CustomSyslogLogger < Syslog::Logger
|
|
||||||
def level
|
|
||||||
Logger::INFO
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_overriding_level
|
|
||||||
@logger = CustomSyslogLogger.new
|
|
||||||
log = log_add Logger::INFO, 'msg'
|
|
||||||
assert_equal 'msg', log.msg
|
|
||||||
|
|
||||||
log = log_add Logger::DEBUG, 'msg'
|
|
||||||
assert_nil log.msg
|
|
||||||
end
|
|
||||||
|
|
||||||
end if defined?(Syslog)
|
|
||||||
|
|
||||||
|
|
||||||
# Create test class for each available facility
|
|
||||||
|
|
||||||
Syslog::Facility.constants.each do |facility_symb|
|
|
||||||
|
|
||||||
test_syslog_class = Class.new(TestSyslogLogger) do
|
|
||||||
|
|
||||||
@facility = Syslog.const_get(facility_symb)
|
|
||||||
|
|
||||||
def setup
|
|
||||||
super
|
|
||||||
@logger.facility = facility
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
Object.const_set("TestSyslogLogger_#{facility_symb}", test_syslog_class)
|
|
||||||
|
|
||||||
end if defined?(Syslog)
|
|
@ -34,7 +34,7 @@ class TestExtLibs < Test::Unit::TestCase
|
|||||||
end.flatten.compact
|
end.flatten.compact
|
||||||
excluded << '+' if excluded.empty?
|
excluded << '+' if excluded.empty?
|
||||||
if windows?
|
if windows?
|
||||||
excluded.map! {|i| i == '+' ? ['pty', 'syslog'] : i}
|
excluded.map! {|i| i == '+' ? ['pty'] : i}
|
||||||
excluded.flatten!
|
excluded.flatten!
|
||||||
else
|
else
|
||||||
excluded.map! {|i| i == '+' ? '*win32*' : i}
|
excluded.map! {|i| i == '+' ? '*win32*' : i}
|
||||||
@ -68,7 +68,6 @@ class TestExtLibs < Test::Unit::TestCase
|
|||||||
check_existence "socket"
|
check_existence "socket"
|
||||||
check_existence "stringio"
|
check_existence "stringio"
|
||||||
check_existence "strscan"
|
check_existence "strscan"
|
||||||
check_existence "syslog"
|
|
||||||
check_existence "thread"
|
check_existence "thread"
|
||||||
check_existence "win32ole"
|
check_existence "win32ole"
|
||||||
check_existence "zlib", "this may be false positive, but should assert because rubygems requires this"
|
check_existence "zlib", "this may be false positive, but should assert because rubygems requires this"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user