Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 305 lines (254 sloc) 6.849 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
# Created by Ari Brown on 2008-02-23.
# For rubinius. All pwnage reserved.
#

# ** 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 Syslog

  module Constants
    @@@
    constants do |c|
      c.include 'syslog.h'

      c.const 'LOG_EMERG'
      c.const 'LOG_ALERT'
      c.const 'LOG_ERR'
      c.const 'LOG_CRIT'
      c.const 'LOG_WARNING'
      c.const 'LOG_NOTICE'
      c.const 'LOG_INFO'
      c.const 'LOG_DEBUG'
      c.const 'LOG_PID'
      c.const 'LOG_CONS'
      c.const 'LOG_ODELAY'
      c.const 'LOG_NDELAY'
      c.const 'LOG_NOWAIT'
      c.const 'LOG_PERROR'
      c.const 'LOG_AUTH'
      c.const 'LOG_AUTHPRIV'
      c.const 'LOG_CONSOLE'
      c.const 'LOG_CRON'
      c.const 'LOG_DAEMON'
      c.const 'LOG_FTP'
      c.const 'LOG_KERN'
      c.const 'LOG_LPR'
      c.const 'LOG_MAIL'
      c.const 'LOG_NEWS'
      c.const 'LOG_NTP'
      c.const 'LOG_SECURITY'
      c.const 'LOG_SYSLOG'
      c.const 'LOG_USER'
      c.const 'LOG_UUCP'
      c.const 'LOG_LOCAL0'
      c.const 'LOG_LOCAL1'
      c.const 'LOG_LOCAL2'
      c.const 'LOG_LOCAL3'
      c.const 'LOG_LOCAL4'
      c.const 'LOG_LOCAL5'
      c.const 'LOG_LOCAL6'
      c.const 'LOG_LOCAL7'
    end
    @@@

    ##
    # LOG_MASK(pri)
    #
    # HACK copied from macro
    # Creates a mask for one priority.
    def self.LOG_MASK(pri)
      1 << pri
    end

    ##
    # LOG_UPTO(pri)
    # HACK copied from macro
    # Creates a mask for all priorities up to pri.
    def self.LOG_UPTO(pri)
      (1 << ((pri)+1)) - 1
    end
  end

  include Constants

  module Foreign
    extend FFI::Library

    # methods
    attach_function :open, "openlog", [:pointer, :int, :int], :void
    attach_function :close, "closelog", [], :void
    attach_function :write, "syslog", [:int, :string, :string], :void
    attach_function :set_mask, "setlogmask", [:int], :int
  end

  # Not open by default.
  #
  # Yes, a normal ivar, on Syslog, the module.
  @open = false

  ##
  # returns the ident of the last open call
  def self.ident
    @open ? @ident : nil
  end

  ##
  # returns the options of the last open call
  def self.options
    @open ? @options : nil
  end

  ##
  # returns the facility of the last open call
  def self.facility
    @open ? @facility : nil
  end

  ##
  # 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)
  def self.mask=(mask)
    unless @open
      raise RuntimeError, "must open syslog before setting log mask"
    end

    @mask = Type.coerce_to mask, Fixnum, :to_int

    Foreign.set_mask(@mask)
  end

  def self.mask
    @open ? @mask : nil
  end

  ##
  # 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.
  #
  # Examples:
  # 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)
  def self.open(ident=nil, opt=nil, fac=nil)
    raise "Syslog already open" unless not @open

    ident ||= $0
    opt ||= Constants::LOG_PID | Constants::LOG_CONS
    fac ||= Constants::LOG_USER

    @ident = ident
    @options = opt
    @facility = fac

    # syslog rereads the string everytime syslog() is called, so we have to use
    # an FFI pointer to keep the memory the string is in alive
    @ident_pointer = FFI::MemoryPointer.new(@ident.size + 1)
    @ident_pointer.write_string(@ident)

    Foreign.open(@ident_pointer, opt, fac)

    @open = true

    # Calling set_mask twice is the standard way to set the 'default' mask
    @mask = Foreign.set_mask(0)
    Foreign.set_mask(@mask)

    if block_given?
      begin
        yield self
      ensure
        close
      end
    end

    self
  end

  def self.reopen(*args, &block)
    close
    open(*args, &block)
  end

  class << self
    alias_method :open!, :reopen
  end

  ##
  # Is it open?
  def self.opened?
    @open
  end

  ##
  # Close the log
  # close will raise an error if it is already closed
  def self.close
    raise "Syslog not opened" unless @open

    Foreign.close
    @ident = nil
    @options = @facility = @mask = -1;
    @open = false
  end

  ##
  # log(Syslog::LOG_CRIT, "The %s is falling!", "sky")
  #
  # Doesn't take any platform specific printf statements
  # logs things to $stderr
  # log(Syslog::LOG_CRIT, "Welcome, %s, to my %s!", "leethaxxor", "lavratory")
  def self.log(pri, format, *args)
    raise "Syslog must be opened before write" unless @open

    pri = Type.coerce_to(pri, Fixnum, :to_i)

    message = StringValue(format) % args
    Foreign.write(pri, "%s", message)
  end

  ##
  # handy little shortcut for LOG_EMERG as the priority
  def self.emerg(*args);
    log(LOG_EMERG, *args)
  end

  ##
  # handy little shortcut for LOG_ALERT as the priority
  def self.alert(*args)
    log(LOG_ALERT, *args)
  end

  ##
  # handy little shortcut for LOG_ERR as the priority
  def self.err(*args)
    log(LOG_ERR, *args)
  end

  ##
  # handy little shortcut for LOG_CRIT as the priority
  def self.crit(*args)
    log(LOG_CRIT, *args)
  end

  ##
  # handy little shortcut for LOG_WARNING as the priority
  def self.warning(*args)
    log(LOG_WARNING, *args)
  end

  ##
  # handy little shortcut for LOG_NOTICE as the priority
  def self.notice(*args)
    log(LOG_NOTICE, *args)
  end

  ##
  # handy little shortcut for LOG_INFO as the priority
  def self.info(*args)
    log(LOG_INFO, *args)
  end

  ##
  # handy little shortcut for LOG_DEBUG as the priority
  def self.debug(*args)
    log(LOG_DEBUG, *args)
  end

  def self.LOG_MASK(pri)
    Constants.LOG_MASK(pri)
  end

  ##
  # LOG_UPTO(pri)
  # HACK copied from macro
  # Creates a mask for all priorities up to pri.
  def self.LOG_UPTO(pri)
    Constants.LOG_UPTO(pri)
  end

  def self.inspect
    if @open
      "#<%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>" %
      [self.name, @ident, @options, @facility, @mask]
    else
      "#<#{self.name}: opened=false>"
    end
  end

  ##
  # Syslog.instance # => Syslog
  # Returns the Syslog module
  def self.instance
    self
  end
end

Something went wrong with that request. Please try again.