Project

General

Profile

Download (2.57 KB) Statistics
| Branch: | Tag: | Revision:
$KCODE = 'UTF8'
require 'jcode'
require 'singleton'
require 'yaml'
require 'log4r'
require "cyborghood/lang_additions"

module CyborgHood

PRODUCT = "CyborgHood"
VERSION = "0.0.1"

class Logger < Log4r::Logger
include Singleton

LOG_FORMAT = "[%5l - %d] #PREFIX#%m"

def output_level(level)
l = case level
when :verbose
Log4r::DEBUG
when :quiet
Log4r::FATAL
else # normal
Log4r::WARN
end
@main_outputter.level = l
end

def log_to_file(filename)
file_outputter = Log4r::FileOutputter.new(filename, :filename => filename)
file_outputter.formatter = @default_formatter
self.outputters << file_outputter
end

def set_prefix(prefix = "")
prefix ||= ""
log_format = LOG_FORMAT.gsub("#PREFIX#", prefix)
@default_formatter = Log4r::PatternFormatter.new(:pattern => log_format)
self.outputters.each{|outputter| outputter.formatter = @default_formatter }
end

private

def initialize
super(PRODUCT)

self.level = Log4r::DEBUG
@main_outputter = Log4r::Outputter.stdout
@main_outputter.level = Log4r::WARN
self.outputters = [@main_outputter]

set_prefix()
end
end

class Config
include Singleton

CFG_DIR = './config/'

CONFFILE_GLOBAL = "cyborghood.conf"
CONFFILE_BOT = "cyborghood_%s.conf"

def self.load(name = nil)
# load all config parts
config = fetch_config
unless name.nil?
config_spc = fetch_config(name)
config = merge_configs(config, config_spc) if config_spc
end

# create config object for easier access
@@config = config.to_ostruct.freeze
end

def method_missing(sym, *args, &block)
begin
@@config.send sym, *args, &block
rescue
nil
end
end

private

def self.fetch_config(name = nil)
begin
if name.nil?
filename = CONFFILE_GLOBAL
else
filename = sprintf(CONFFILE_BOT, name)
end
filepath = File.join(File.expand_path(CFG_DIR), filename)
str = File.read(filepath)
YAML.load(str)
rescue
logger.fatal "Problems fetching configuration file '" + filepath + "': " + $!
exit 1
end
end

def self.merge_configs(conf1, conf2)
new_conf = conf1.dup
conf2.each_pair do |k, v|
if conf1.has_key?(k) and conf1[k].is_a?(Hash) and v.is_a?(Hash)
new_conf[k] = merge_configs(conf1[k], v)
else
new_conf[k] = v
end
end
return new_conf
end
end
end
(1-1/4)