Skip to content

Instantly share code, notes, and snippets.

@mwchambers
Created October 26, 2011 11:24
Show Gist options
  • Save mwchambers/1316078 to your computer and use it in GitHub Desktop.
Save mwchambers/1316078 to your computer and use it in GitHub Desktop.
Bitwise logging
require 'pp'
class LoggingLevel
ONE = 1
TWO = 2
DOCUMENTS = 4
ATTENDANCE = 8
DATABASE = 16
@@modification_time = nil
@@configuration_file = "/tmp/logging.conf"
def self.is?(level)
return ( ( level & @@value ) != 0 )
end
def self.value=(val); @@value = val; end
def self.value; @@value ; end
def self.unused_from_file_is?(level)
mod_time = File.stat(@@configuration_file).mtime
if @@modification_time != mod_time
@@value = File.open(@@configuration_file) { |f| f.read }.to_i
end
return ( ( level & @@value ) != 0 )
end
end
def display_logging
puts "The current log value is: #{LoggingLevel.value}"
puts "I log for ONE" if LoggingLevel.is?(LoggingLevel::ONE)
puts "I log for TWO" if LoggingLevel.is?(LoggingLevel::TWO)
puts "I log for DOCUMENTS" if LoggingLevel.is?(LoggingLevel::DOCUMENTS)
puts "I log for ATTENDANCE" if LoggingLevel.is?(LoggingLevel::ATTENDANCE)
puts "I log for DATABASE" if LoggingLevel.is?(LoggingLevel::DATABASE)
puts "-" * 72
end
# Logging only 'ONE'
LoggingLevel::value = 1
display_logging
# Logging only 'TWO'
LoggingLevel::value = 2
display_logging
# Logging both 'ONE' and 'TWO'
LoggingLevel::value = 3
display_logging
# Logging 'DATABASE' and 'DOCUMENTS'
LoggingLevel::value = 20
display_logging
# Logging 'DATABASE' only
LoggingLevel::value = 16
display_logging
# Logging 'TWO' and 'ATTENDANCE'
#
LoggingLevel::value = 10
display_logging
# Turn on all logging
LoggingLevel::value = 31
display_logging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment