eventhub-processor 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,89 +1,89 @@
1
- require 'logger'
2
-
3
- # format adaptation
4
- class Logger
5
- class Formatter
6
- def call(severity, time, progname, msg)
7
- time_in_string = "#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%04d" % (time.usec/100)}"
8
- [time_in_string,Process.pid,severity,msg].join("\t") + "\n"
9
- end
10
- end
11
-
12
- end
13
-
14
-
15
- module EventHub
16
-
17
- class MultiLogger
18
-
19
- MAX_EXCEPTIONS_FILES = 500
20
-
21
- attr_accessor :folder, :devices
22
-
23
- def initialize(folder=nil)
24
- @folder_base = folder || Dir.pwd
25
- @folder_base.chomp!('/')
26
- @folder = [@folder_base,'logs'].join('/')
27
- @folder_exceptions = [@folder_base,'exceptions'].join('/')
28
-
29
- @devices = []
30
-
31
- FileUtils.makedirs(@folder)
32
- end
33
-
34
- def add_device(device)
35
- @devices << device
36
- end
37
-
38
- def save_detailed_error(feedback,message=nil)
39
- time = Time.now
40
- stamp = "#{time.strftime("%Y%m%d_%H%M%S")}_#{"%03d" % (time.usec/1000)}"
41
- filename = "#{stamp}.log"
42
-
43
- FileUtils.makedirs(@folder_exceptions)
44
-
45
- # check max exception log files
46
- exception_files = Dir.glob(@folder_exceptions + '/*.log')
47
- if exception_files.size > MAX_EXCEPTIONS_FILES
48
- exception_files.reverse[MAX_EXCEPTIONS_FILES..-1].each do |file|
49
- begin
50
- File.delete(file)
51
- File.delete(File.dirname(file) + '/' + File.basename(file,".*") + '.msg.raw')
52
- rescue
53
- end
54
- end
55
- end
56
-
57
- File.open("#{@folder_exceptions}/#{filename}","w") do |output|
58
- output.write("#{feedback}\n\n")
59
- output.write("Exception: #{feedback.class.to_s}\n\n")
60
- output.write("Call Stack:\n")
61
- feedback.backtrace.each do |line|
62
- output.write("#{line}\n")
63
- end
64
- end
65
-
66
- # save message if provided
67
- if message
68
- File.open("#{@folder_exceptions}/#{stamp}.msg.raw","wb") do |output|
69
- output.write(message)
70
- end
71
- end
72
-
73
- return stamp
74
- end
75
-
76
- %w(log debug info warn error).each do |m|
77
- define_method(m) do |*args|
78
- @devices.map { |d| d.send(m, *args) }
79
- end
80
- end
81
-
82
- end
83
-
84
- end
85
-
86
-
87
-
88
-
89
-
1
+ require 'logger'
2
+
3
+ # format adaptation
4
+ class Logger
5
+ class Formatter
6
+ def call(severity, time, progname, msg)
7
+ time_in_string = "#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%04d" % (time.usec/100)}"
8
+ [time_in_string,Process.pid,severity,msg].join("\t") + "\n"
9
+ end
10
+ end
11
+
12
+ end
13
+
14
+
15
+ module EventHub
16
+
17
+ class MultiLogger
18
+
19
+ MAX_EXCEPTIONS_FILES = 500
20
+
21
+ attr_accessor :folder, :devices
22
+
23
+ def initialize(folder = nil)
24
+ @folder_base = folder || Dir.pwd
25
+ @folder_base.chomp!('/')
26
+ @folder = [@folder_base,'logs'].join('/')
27
+ @folder_exceptions = [@folder_base,'exceptions'].join('/')
28
+
29
+ @devices = []
30
+
31
+ FileUtils.makedirs(@folder)
32
+ end
33
+
34
+ def add_device(device)
35
+ @devices << device
36
+ end
37
+
38
+ def save_detailed_error(feedback,message=nil)
39
+ time = Time.now
40
+ stamp = "#{time.strftime("%Y%m%d_%H%M%S")}_#{"%03d" % (time.usec/1000)}"
41
+ filename = "#{stamp}.log"
42
+
43
+ FileUtils.makedirs(@folder_exceptions)
44
+
45
+ # check max exception log files
46
+ exception_files = Dir.glob(@folder_exceptions + '/*.log')
47
+ if exception_files.size > MAX_EXCEPTIONS_FILES
48
+ exception_files.reverse[MAX_EXCEPTIONS_FILES..-1].each do |file|
49
+ begin
50
+ File.delete(file)
51
+ File.delete(File.dirname(file) + '/' + File.basename(file,".*") + '.msg.raw')
52
+ rescue
53
+ end
54
+ end
55
+ end
56
+
57
+ File.open("#{@folder_exceptions}/#{filename}","w") do |output|
58
+ output.write("#{feedback}\n\n")
59
+ output.write("Exception: #{feedback.class.to_s}\n\n")
60
+ output.write("Call Stack:\n")
61
+ feedback.backtrace.each do |line|
62
+ output.write("#{line}\n")
63
+ end
64
+ end
65
+
66
+ # save message if provided
67
+ if message
68
+ File.open("#{@folder_exceptions}/#{stamp}.msg.raw","wb") do |output|
69
+ output.write(message)
70
+ end
71
+ end
72
+
73
+ return stamp
74
+ end
75
+
76
+ %w(log debug info warn error).each do |m|
77
+ define_method(m) do |*args|
78
+ @devices.map { |d| d.send(m, *args) }
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+
87
+
88
+
89
+
@@ -0,0 +1,2 @@
1
+ class EventHub::NoDeadletterException < EventHub::BaseException
2
+ end
@@ -0,0 +1,21 @@
1
+ class EventHub::Pidfile
2
+ attr_reader :file
3
+ def initialize(file)
4
+ @file = file
5
+ end
6
+
7
+ # write the pid to the file specified in the initializer
8
+ def write(pid)
9
+ FileUtils.makedirs(File.dirname(file))
10
+ IO.write(file, pid.to_s)
11
+ end
12
+
13
+ # Try to delete file, ignore all errors
14
+ def delete
15
+ begin
16
+ File.delete(file)
17
+ rescue
18
+ # ignore
19
+ end
20
+ end
21
+ end