1 # Created by Ari Brown on 2008-02-23.
2 # For rubinius. All pwnage reserved.
4 # Used in pwning teh nubs with FFI instead of C
8 # Included Modules: Syslog::Constants
12 # A Simple wrapper for the UNIX syslog system calls that might be handy
13 # if you're writing a server in Ruby. For the details of the syslog(8)
14 # architecture and constants, see the syslog(3) manual page of your
39 c.const 'LOG_AUTHPRIV'
49 c.const 'LOG_SECURITY'
69 attach_function "openlog", :open, [:string, :int, :int], :void
70 attach_function "closelog", :close, [], :void
71 attach_function "syslog", :write, [:int, :string, :string], :void
72 attach_function "setlogmask", :set_mask, [:int], :int
78 # returns the ident of the last open call
82 # returns the options of the last open call
86 # returns the facility of the last open call
93 # Returns or sets the log priority mask. The value of the mask
94 # is persistent and will not be reset by Syslog::open or
98 # Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
99 def mask; @mask ||= -1; end
103 # open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER) [{ |syslog| ... }]
105 # Opens syslog with the given options and returns the module
106 # itself. If a block is given, calls it with an argument of
107 # itself. If syslog is already opened, raises RuntimeError.
110 # Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, Syslog::LOG_FTP)
111 # open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)
112 # reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)
113 def open(ident=nil, opt=nil, fac=nil)
114 raise "Syslog already open" unless not @opened
117 opt ||= Constants::LOG_PID | Constants::LOG_CONS
118 fac ||= Constants::LOG_USER
124 Foreign.open(ident, opt, fac)
128 # Calling set_mask twice is the standard way to set the 'default' mask
129 @mask = Foreign.set_mask(0)
130 Foreign.set_mask(@mask)
142 alias_method :open!, :open
145 # like open, but closes it first
159 # close will raise an error if it is already closed
161 raise "Syslog not opened" unless @opened
165 @options = @facility = @mask = -1;
170 # log(Syslog::LOG_CRIT, "The %s is falling!", "sky")
172 # Doesn't take any platform specific printf statements
173 # logs things to $stderr
174 # log(Syslog::LOG_CRIT, "Welcome, %s, to my %s!", "leethaxxor", "lavratory")
180 # handy little shortcut for LOG_EMERG as the priority
181 def emerg(*args); Foreign.write(LOG_EMERG, *args); end
184 # handy little shortcut for LOG_ALERT as the priority
185 def alert(*args); Foreign.write(LOG_ALERT, *args); end
188 # handy little shortcut for LOG_ERR as the priority
189 def err(*args); Foreign.write(LOG_ERR, *args); end
192 # handy little shortcut for LOG_CRIT as the priority
193 def crit(*args); Foreign.write(LOG_CRIT, *args); end
196 # handy little shortcut for LOG_WARNING as the priority
197 def warning(*args);Foreign.write(LOG_WARNING, *args); end
200 # handy little shortcut for LOG_NOTICE as the priority
201 def notice(*args); Foreign.write(LOG_NOTICE, *args); end
204 # handy little shortcut for LOG_INFO as the priority
205 def info(*args); Foreign.write(LOG_INFO, *args); end
208 # handy little shortcut for LOG_DEBUG as the priority
209 def debug(*args); Foreign.write(LOG_DEBUG, *args); end
214 # HACK copied from macro
215 # Creates a mask for one priority.
222 # HACK copied from macro
223 # Creates a mask for all priorities up to pri.
230 "#<%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>" %
231 [self.name, @ident, @options, @facility, @mask]
233 "#<#{self.name}: opened=false>"
238 # Syslog.instance # => Syslog
239 # Returns the Syslog module
244 def write(pri, format, *args)
245 raise "Syslog must be opened before write" unless @opened
247 message = format % args
248 Foreign.write(pri, "%s", message)