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
|
# vim: set sw=2 ts=2 expandtab:
#
# Copyright (C) 2010 by James Maki
#
# Author: James Maki <jamescmaki@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
require 'syslog'
# Inform.syslog = false
# if Inform.syslog
# Syslog.open("smsws", Syslog::LOG_NDELAY, Syslog::LOG_LOCAL7)
# else
# Inform.level = Inform::LOG_ERR
# Inform.file = $stdout
# end
module Inform
include Syslog
LEVELS = {
LOG_EMERG => :emerg,
LOG_ALERT => :alert,
LOG_CRIT => :crit,
LOG_ERR => :err,
LOG_WARNING => :warning,
LOG_NOTICE => :notice,
LOG_INFO => :info,
LOG_DEBUG => :debug,
}
LEVELS.each_pair do |key, value|
module_eval <<-EOS
def #{value}(msg)
inform(#{key}, msg, caller[0])
end
EOS
module_function value
end
module_function
def level=(level)
raise ArgumentError, "bad level" unless level >= LOG_EMERG && level <= LOG_DEBUG
@level = level
end
def level
@level
end
def file=(file)
@file = file
end
def file
@file
end
def syslog=(syslog)
@syslog = syslog
end
def syslog
@syslog
end
def inform(level, msg, from = caller[0])
time = Time.now.strftime('%Y-%m-%d %H:%M:%S')
from =~ /(.+)\:(\d+)(?:\:in \`(\S+)\')?/
file = File.basename($1)
line = $2
func = $3
func ||= 'main'
str = "#{time} [#{LEVELS[level].to_s.upcase}] #{file}:#{func}:#{line}: "
if msg.is_a?(Exception)
str << "#{msg.class} #{msg}"
else
str << "#{msg}"
end
log(level, str)
if msg.is_a?(Exception)
msg.backtrace[0..9].each do |line|
log(level, "> #{line}")
end
end
end
def escape_spec(str)
new = ""
str.each_byte { |c|
#if c == "%".ord
if c == ?%
new << "%%"
else
new << c
end
}
return new
end
def log(level, msg)
if @syslog
Syslog.log(level, escape_spec(msg))
elsif @level && @file
@file.puts(msg) if level <= @level
end
end
end
|