Skip to content
Newer
Older
100644 305 lines (254 sloc) 6.69 KB
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
1 # Created by Ari Brown on 2008-02-23.
2 # For rubinius. All pwnage reserved.
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 27, 2009
3 #
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
4
5 # ** Syslog(Module)
6
7 # Included Modules: Syslog::Constants
8
9 # require 'syslog'
10
11 # A Simple wrapper for the UNIX syslog system calls that might be handy
12 # if you're writing a server in Ruby. For the details of the syslog(8)
13 # architecture and constants, see the syslog(3) manual page of your
14 # platform.
15
16 module Syslog
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
17
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
18 module Constants
19 @@@
20 constants do |c|
21 c.include 'syslog.h'
22
23 c.const 'LOG_EMERG'
24 c.const 'LOG_ALERT'
25 c.const 'LOG_ERR'
26 c.const 'LOG_CRIT'
27 c.const 'LOG_WARNING'
28 c.const 'LOG_NOTICE'
29 c.const 'LOG_INFO'
30 c.const 'LOG_DEBUG'
31 c.const 'LOG_PID'
32 c.const 'LOG_CONS'
33 c.const 'LOG_ODELAY'
af93182 Get syslog passing the specs
Evan Phoenix authored Oct 15, 2010
34 c.const 'LOG_NDELAY'
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
35 c.const 'LOG_NOWAIT'
36 c.const 'LOG_PERROR'
37 c.const 'LOG_AUTH'
38 c.const 'LOG_AUTHPRIV'
39 c.const 'LOG_CONSOLE'
40 c.const 'LOG_CRON'
41 c.const 'LOG_DAEMON'
42 c.const 'LOG_FTP'
43 c.const 'LOG_KERN'
44 c.const 'LOG_LPR'
45 c.const 'LOG_MAIL'
46 c.const 'LOG_NEWS'
47 c.const 'LOG_NTP'
48 c.const 'LOG_SECURITY'
49 c.const 'LOG_SYSLOG'
50 c.const 'LOG_USER'
51 c.const 'LOG_UUCP'
52 c.const 'LOG_LOCAL0'
53 c.const 'LOG_LOCAL1'
54 c.const 'LOG_LOCAL2'
55 c.const 'LOG_LOCAL3'
56 c.const 'LOG_LOCAL4'
57 c.const 'LOG_LOCAL5'
58 c.const 'LOG_LOCAL6'
59 c.const 'LOG_LOCAL7'
60 end
61 @@@
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
62
63 ##
64 # LOG_MASK(pri)
65 #
66 # HACK copied from macro
67 # Creates a mask for one priority.
68 def self.LOG_MASK(pri)
69 1 << pri
70 end
71
72 ##
73 # LOG_UPTO(pri)
74 # HACK copied from macro
75 # Creates a mask for all priorities up to pri.
76 def self.LOG_UPTO(pri)
77 (1 << ((pri)+1)) - 1
78 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
79 end
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
80
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
81 include Constants
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
82
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
83 module Foreign
fcbb626 Fixed FFI attach_function calls in syslog.
Brian Ford authored Jan 31, 2009
84 extend FFI::Library
85
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
86 # methods
af93182 Get syslog passing the specs
Evan Phoenix authored Oct 15, 2010
87 attach_function :open, "openlog", [:pointer, :int, :int], :void
fcbb626 Fixed FFI attach_function calls in syslog.
Brian Ford authored Jan 31, 2009
88 attach_function :close, "closelog", [], :void
89 attach_function :write, "syslog", [:int, :string, :string], :void
90 attach_function :set_mask, "setlogmask", [:int], :int
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
91 end
92
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
93 # Not open by default.
94 #
95 # Yes, a normal ivar, on Syslog, the module.
96 @open = false
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
97
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
98 ##
99 # returns the ident of the last open call
100 def self.ident
101 @open ? @ident : nil
102 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
103
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
104 ##
105 # returns the options of the last open call
106 def self.options
107 @open ? @options : nil
108 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
109
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
110 ##
111 # returns the facility of the last open call
112 def self.facility
113 @open ? @facility : nil
114 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
115
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
116 ##
117 # mask
118 # mask=(mask)
119 #
120 # Returns or sets the log priority mask. The value of the mask
121 # is persistent and will not be reset by Syslog::open or
122 # Syslog::close.
123 #
124 # Example:
125 # Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
126 def self.mask=(mask)
127 unless @open
128 raise RuntimeError, "must open syslog before setting log mask"
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
129 end
130
af93182 Get syslog passing the specs
Evan Phoenix authored Oct 15, 2010
131 @mask = Type.coerce_to mask, Fixnum, :to_int
132
133 Foreign.set_mask(@mask)
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
134 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
135
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
136 def self.mask
137 @open ? @mask : nil
138 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
139
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
140 ##
141 # open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER) [{ |syslog| ... }]
142 #
143 # Opens syslog with the given options and returns the module
144 # itself. If a block is given, calls it with an argument of
145 # itself. If syslog is already opened, raises RuntimeError.
146 #
147 # Examples:
148 # Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, Syslog::LOG_FTP)
149 # open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)
150 # reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER)
151 def self.open(ident=nil, opt=nil, fac=nil)
152 raise "Syslog already open" unless not @open
153
154 ident ||= $0
155 opt ||= Constants::LOG_PID | Constants::LOG_CONS
156 fac ||= Constants::LOG_USER
157
158 @ident = ident
159 @options = opt
160 @facility = fac
161
af93182 Get syslog passing the specs
Evan Phoenix authored Oct 15, 2010
162 # syslog rereads the string everytime syslog() is called, so we have to use
163 # an FFI pointer to keep the memory the string is in alive
164 @ident_pointer = FFI::MemoryPointer.new(@ident.size + 1)
165 @ident_pointer.write_string(@ident)
166
167 Foreign.open(@ident_pointer, opt, fac)
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
168
169 @open = true
170
171 # Calling set_mask twice is the standard way to set the 'default' mask
172 @mask = Foreign.set_mask(0)
173 Foreign.set_mask(@mask)
174
175 if block_given?
176 begin
177 yield self
178 ensure
179 close
180 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
181 end
182
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
183 self
184 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
185
af93182 Get syslog passing the specs
Evan Phoenix authored Oct 15, 2010
186 def self.reopen(*args, &block)
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
187 close
af93182 Get syslog passing the specs
Evan Phoenix authored Oct 15, 2010
188 open(*args, &block)
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
189 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
190
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
191 class << self
192 alias_method :open!, :reopen
193 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
194
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
195 ##
196 # Is it open?
197 def self.opened?
198 @open
199 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
200
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
201 ##
202 # Close the log
203 # close will raise an error if it is already closed
204 def self.close
205 raise "Syslog not opened" unless @open
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
206
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
207 Foreign.close
208 @ident = nil
209 @options = @facility = @mask = -1;
210 @open = false
211 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
212
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
213 ##
214 # log(Syslog::LOG_CRIT, "The %s is falling!", "sky")
215 #
216 # Doesn't take any platform specific printf statements
217 # logs things to $stderr
218 # log(Syslog::LOG_CRIT, "Welcome, %s, to my %s!", "leethaxxor", "lavratory")
219 def self.log(pri, format, *args)
220 raise "Syslog must be opened before write" unless @open
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
221
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
222 pri = Type.coerce_to(pri, Fixnum, :to_i)
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
223
af93182 Get syslog passing the specs
Evan Phoenix authored Oct 15, 2010
224 message = StringValue(format) % args
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
225 Foreign.write(pri, "%s", message)
226 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
227
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
228 ##
229 # handy little shortcut for LOG_EMERG as the priority
230 def self.emerg(*args);
231 log(LOG_EMERG, *args)
232 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
233
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
234 ##
235 # handy little shortcut for LOG_ALERT as the priority
236 def self.alert(*args)
237 log(LOG_ALERT, *args)
238 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
239
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
240 ##
241 # handy little shortcut for LOG_ERR as the priority
242 def self.err(*args)
243 log(LOG_ERR, *args)
244 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
245
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
246 ##
247 # handy little shortcut for LOG_CRIT as the priority
248 def self.crit(*args)
249 log(LOG_CRIT, *args)
250 end
251
252 ##
253 # handy little shortcut for LOG_WARNING as the priority
254 def self.warning(*args)
255 log(LOG_WARNING, *args)
256 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
257
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
258 ##
259 # handy little shortcut for LOG_NOTICE as the priority
260 def self.notice(*args)
261 log(LOG_NOTICE, *args)
262 end
263
264 ##
265 # handy little shortcut for LOG_INFO as the priority
266 def self.info(*args)
267 log(LOG_INFO, *args)
268 end
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
269
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
270 ##
271 # handy little shortcut for LOG_DEBUG as the priority
272 def self.debug(*args)
273 log(LOG_DEBUG, *args)
274 end
275
276 def self.LOG_MASK(pri)
277 Constants.LOG_MASK(pri)
278 end
279
280 ##
281 # LOG_UPTO(pri)
282 # HACK copied from macro
283 # Creates a mask for all priorities up to pri.
284 def self.LOG_UPTO(pri)
285 Constants.LOG_UPTO(pri)
286 end
287
288 def self.inspect
289 if @open
290 "#<%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>" %
291 [self.name, @ident, @options, @facility, @mask]
292 else
293 "#<#{self.name}: opened=false>"
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
294 end
5fc6d8e Cleanup Syslog
Evan Phoenix authored Nov 28, 2009
295 end
296
297 ##
298 # Syslog.instance # => Syslog
299 # Returns the Syslog module
300 def self.instance
301 self
4283525 Pulled everything I could from master into lib
Ryan Davis authored Sep 10, 2008
302 end
303 end
304
Something went wrong with that request. Please try again.