This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLogger.rb
114 lines (91 loc) · 2.77 KB
/
Logger.rb
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
module WebBlocks
module Logger
include ::WebBlocks::Config
@@log = false
def log
@@log = ::WebBlocks::Logger::Logger.new config[:build][:log][:name] unless @@log
@@log
end
class Logger
attr_accessor :file
def initialize file
@file = file
@file_handle = File.open @file, "w"
@scope = 0
@types = [:system, :task, :failure, :success, :warning, :info, :debug]
@type = :system
if ::WebBlocks.config[:options][:details]
@types_to_print = [:system, :task, :failure, :success, :warning]
else
@types_to_print = [:system, :failure, :warning]
end
@types_to_file = [:system, :task, :failure, :success, :warning, :info, :debug]
end
def types_to_print arr
arr = @types if arr == :all
@types_to_print = arr
end
def types_to_file arr
arr = @types if arr == :all
@types_to_file = arr
end
def system category, message = false
scope :system, category, message do
yield if block_given?
end
end
def task category, message = false
scope :task, category, message do
yield if block_given?
end
end
def failure category, message = false
scope :failure, category, message do
yield if block_given?
end
fail "#{message ? message : category}"
end
def success category, message = false
scope :success, category, message do
yield if block_given?
end
end
def warning category, message = false
scope :warning, category, message do
yield if block_given?
end
end
def info category, message = false
scope :info, category, message do
yield if block_given?
end
end
def debug category, message = false
scope :debug, category, message do
yield if block_given?
end
end
def write type, category, message = false
text = message ? "[#{category}] #{message}" : "#{category}"
text = "#{' ' * @scope}#{text}".gsub "\n", "\n#{' ' * @scope}"
puts text if @types_to_print.include? type unless ::WebBlocks.config[:options][:silent]
@file_handle.puts text if @types_to_file.include? type
end
def scope type, category = false, message = false
prev = @type
@type = type if @types.index(type) > @types.index(@type)
write @type, category, message if category
scope_start
yield if block_given?
scope_end
@type = prev
end
def scope_start
@scope += 1
end
def scope_end
@scope -= 1 if @scope > 0
end
end
end
end