Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 256 lines (212 sloc) 6.352 kb
4283525 Pulled everything I could from master into lib
Ryan Davis authored
1 # Created by Ari Brown on 2008-02-23.
2 # For rubinius. All pwnage reserved.
3 #
4 # Used in pwning teh nubs with FFI instead of C
5
6 # ** Syslog(Module)
7
8 # Included Modules: Syslog::Constants
9
10 # require 'syslog'
11
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
15 # platform.
16
17 module Syslog
18
19 module Constants
20 @@@
21 constants do |c|
22 c.include 'syslog.h'
23
24 c.const 'LOG_EMERG'
25 c.const 'LOG_ALERT'
26 c.const 'LOG_ERR'
27 c.const 'LOG_CRIT'
28 c.const 'LOG_WARNING'
29 c.const 'LOG_NOTICE'
30 c.const 'LOG_INFO'
31 c.const 'LOG_DEBUG'
32 c.const 'LOG_PID'
33 c.const 'LOG_CONS'
34 c.const 'LOG_ODELAY'
35 c.const 'LOG_NODELAY'
36 c.const 'LOG_NOWAIT'
37 c.const 'LOG_PERROR'
38 c.const 'LOG_AUTH'
39 c.const 'LOG_AUTHPRIV'
40 c.const 'LOG_CONSOLE'
41 c.const 'LOG_CRON'
42 c.const 'LOG_DAEMON'
43 c.const 'LOG_FTP'
44 c.const 'LOG_KERN'
45 c.const 'LOG_LPR'
46 c.const 'LOG_MAIL'
47 c.const 'LOG_NEWS'
48 c.const 'LOG_NTP'
49 c.const 'LOG_SECURITY'
50 c.const 'LOG_SYSLOG'
51 c.const 'LOG_USER'
52 c.const 'LOG_UUCP'
53 c.const 'LOG_LOCAL0'
54 c.const 'LOG_LOCAL1'
55 c.const 'LOG_LOCAL2'
56 c.const 'LOG_LOCAL3'
57 c.const 'LOG_LOCAL4'
58 c.const 'LOG_LOCAL5'
59 c.const 'LOG_LOCAL6'
60 c.const 'LOG_LOCAL7'
61 end
62 @@@
63 end
64
65 include Constants
66
67 module Foreign
fcbb626 @brixen Fixed FFI attach_function calls in syslog.
brixen authored
68 extend FFI::Library
69
4283525 Pulled everything I could from master into lib
Ryan Davis authored
70 # methods
fcbb626 @brixen Fixed FFI attach_function calls in syslog.
brixen authored
71 attach_function :open, "openlog", [:string, :int, :int], :void
72 attach_function :close, "closelog", [], :void
73 attach_function :write, "syslog", [:int, :string, :string], :void
74 attach_function :set_mask, "setlogmask", [:int], :int
4283525 Pulled everything I could from master into lib
Ryan Davis authored
75 end
76
77 class << self
78
79 ##
80 # returns the ident of the last open call
81 attr_reader :ident
82
83 ##
84 # returns the options of the last open call
85 attr_reader :options
86
87 ##
88 # returns the facility of the last open call
89 attr_reader :facility
90
91 ##
92 # mask
93 # mask=(mask)
94 #
95 # Returns or sets the log priority mask. The value of the mask
96 # is persistent and will not be reset by Syslog::open or
97 # Syslog::close.
98 #
99 # Example:
100 # Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
101 def mask; @mask ||= -1; end
102 attr_writer :mask
103
104 ##
105 # open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER) [{ |syslog| ... }]
106 #
107 # Opens syslog with the given options and returns the module
108 # itself. If a block is given, calls it with an argument of
109 # itself. If syslog is already opened, raises RuntimeError.
110 #
111 # Examples:
112 # Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, Syslog::LOG_FTP)
113 # open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)
114 # reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)
115 def open(ident=nil, opt=nil, fac=nil)
116 raise "Syslog already open" unless not @opened
117
118 ident ||= $0
119 opt ||= Constants::LOG_PID | Constants::LOG_CONS
120 fac ||= Constants::LOG_USER
121
122 @ident = ident
123 @options = opt
124 @facility = fac
125
126 Foreign.open(ident, opt, fac)
127
128 @opened = true
129
130 # Calling set_mask twice is the standard way to set the 'default' mask
131 @mask = Foreign.set_mask(0)
132 Foreign.set_mask(@mask)
133
134 if block_given?
135 begin
136 yield self
137 ensure
138 close
139 end
140 end
141
142 self
143 end
144 alias_method :open!, :open
145
146 ##
147 # like open, but closes it first
148 def reopen(*args)
149 close
150 open(*args)
151 end
152
153 ##
154 # Is it open?
155 def opened?
156 @opened
157 end
158
159 ##
160 # Close the log
161 # close will raise an error if it is already closed
162 def close
163 raise "Syslog not opened" unless @opened
164
165 Foreign.close
166 @ident = nil
167 @options = @facility = @mask = -1;
168 @opened = false
169 end
170
171 ##
172 # log(Syslog::LOG_CRIT, "The %s is falling!", "sky")
173 #
174 # Doesn't take any platform specific printf statements
175 # logs things to $stderr
176 # log(Syslog::LOG_CRIT, "Welcome, %s, to my %s!", "leethaxxor", "lavratory")
177 def log(pri, *args)
178 write(pri, *args)
179 end
180
181 ##
182 # handy little shortcut for LOG_EMERG as the priority
183 def emerg(*args); Foreign.write(LOG_EMERG, *args); end
184
185 ##
186 # handy little shortcut for LOG_ALERT as the priority
187 def alert(*args); Foreign.write(LOG_ALERT, *args); end
188
189 ##
190 # handy little shortcut for LOG_ERR as the priority
191 def err(*args); Foreign.write(LOG_ERR, *args); end
192
193 ##
194 # handy little shortcut for LOG_CRIT as the priority
195 def crit(*args); Foreign.write(LOG_CRIT, *args); end
196
197 ##
198 # handy little shortcut for LOG_WARNING as the priority
199 def warning(*args);Foreign.write(LOG_WARNING, *args); end
200
201 ##
202 # handy little shortcut for LOG_NOTICE as the priority
203 def notice(*args); Foreign.write(LOG_NOTICE, *args); end
204
205 ##
206 # handy little shortcut for LOG_INFO as the priority
207 def info(*args); Foreign.write(LOG_INFO, *args); end
208
209 ##
210 # handy little shortcut for LOG_DEBUG as the priority
211 def debug(*args); Foreign.write(LOG_DEBUG, *args); end
212
213 ##
214 # LOG_MASK(pri)
215 #
216 # HACK copied from macro
217 # Creates a mask for one priority.
218 def LOG_MASK(pri)
219 1 << pri
220 end
221
222 ##
223 # LOG_UPTO(pri)
224 # HACK copied from macro
225 # Creates a mask for all priorities up to pri.
226 def LOG_UPTO(pri)
227 (1 << ((pri)+1)) - 1
228 end
229
230 def inspect
231 if @opened
232 "#<%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>" %
233 [self.name, @ident, @options, @facility, @mask]
234 else
235 "#<#{self.name}: opened=false>"
236 end
237 end
238
239 ##
240 # Syslog.instance # => Syslog
241 # Returns the Syslog module
242 def instance
243 self
244 end
245
246 def write(pri, format, *args)
247 raise "Syslog must be opened before write" unless @opened
248
249 message = format % args
250 Foreign.write(pri, "%s", message)
251 end
252 private :write
253 end
254 end
255
Something went wrong with that request. Please try again.