loganb-scribble-client 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,10 @@
1
+ == 0.0.2 2009-05-28
2
+
3
+ * Changed the structure of contexts, they are now named and contain only one hash. A named context may be applied for the duration of a block
4
+ * Set SND_BUF socket option to 24k in syslog client, supporting the sending of larger messages
5
+ * Changed time format on SyslogClient for better compatibility
6
+
7
+ == 0.0.1 2009-05-01
8
+
9
+ * 1 major enhancement:
10
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,14 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/scribble-client.rb
7
+ lib/scribble-client/scribbler.rb
8
+ lib/scribble-client/syslog_client.rb
9
+ scribble-client.gemspec
10
+ script/console
11
+ script/destroy
12
+ script/generate
13
+ test/test_helper.rb
14
+ test/test_scribble_client.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on scribble-client, see http://scribble-client.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = scribble-client
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/scribble-client'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('scribble-client', ScribbleClient::VERSION) do |p|
7
+ p.developer('Logan Bowers', 'logan@datacurrent.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ p.url = 'http://www.example.com/'
12
+ p.extra_deps = [
13
+ ['uuidtools','>= 1.0.7'],
14
+ ['collections','>= 0.1.4'],
15
+ ['json', '>= 1.1.3']
16
+ ]
17
+ p.extra_dev_deps = [
18
+ ['newgem', ">= #{::Newgem::VERSION}"]
19
+ ]
20
+
21
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
22
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
23
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
24
+ p.rsync_args = '-av --delete --ignore-errors'
25
+ end
26
+
27
+ require 'newgem/tasks' # load /tasks/*.rake
28
+ Dir['tasks/**/*.rake'].each { |t| load t }
29
+
30
+ # TODO - want other tests/tasks run by default? Add them to the list
31
+ # task :default => [:spec, :features]
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ Dir["#{File.dirname(__FILE__)}/scribble-client/**/*.rb"].sort.each { |lib|
5
+ require lib
6
+ }
7
+
8
+ module ScribbleClient
9
+ VERSION = '0.0.2'
10
+ end
@@ -0,0 +1,123 @@
1
+ require 'collections'
2
+ require 'uuidtools'
3
+ require 'socket'
4
+ require 'json'
5
+
6
+ # Scribbler provides support for logging structured data via syslog. The
7
+ #
8
+ class Scribbler
9
+ def self.thread_context
10
+ t = Thread.current
11
+ t[:_scribbler_context] ||= Context.new {
12
+ sequence_num = 0
13
+ {
14
+ :thuid => UUID.random_create.to_s,
15
+ :thseq => lambda { |ctx,evt|
16
+ sequence_num += 1
17
+ }
18
+ }
19
+ }
20
+ end
21
+
22
+ def self.process_context
23
+ @proc_scribbler ||= Context.new {
24
+ {
25
+ :host => Socket.gethostname,
26
+ :pid => Process.pid
27
+ }
28
+ }
29
+ end
30
+
31
+ #def log(string,context)
32
+ # event('log',:msg => string,context)
33
+ #end
34
+
35
+ def event(hash)
36
+ result = {}
37
+ #Merge data from the system-wide contexts
38
+ (self.class.process_context.apply_context result)
39
+ (self.class.thread_context.apply_context result)
40
+
41
+ #Merge the contexts on this instance
42
+ @contexts.keys.reverse_each { |k|
43
+ ctx = @contexts[k]
44
+ ctx.apply_context result
45
+ }
46
+ (result.merge! hash)
47
+
48
+ #21 == local5 facility, 6 == info priority
49
+ @syslogger.syslog(21,6,result.to_json)
50
+ end
51
+
52
+ #
53
+ # Returns the context of the given name, creating a new one if it does not exist.
54
+ # If a block is supplied, the context is created for the duration of the block
55
+ # and removed afterwards (in this case the method returns the result of the block).
56
+ #
57
+ def context(name = :default,&block)
58
+ ctx = @contexts[name]
59
+ if(ctx.nil?)
60
+ delete_when_finished = true
61
+ @contexts[name] = ctx = Context.new
62
+ else
63
+ delete_when_finished = false
64
+ end
65
+
66
+ if(block_given?)
67
+ begin
68
+ return (yield ctx)
69
+ ensure
70
+ @contexts.delete(name) if(delete_when_finished)
71
+ end
72
+ else
73
+ return ctx
74
+ end
75
+ end
76
+
77
+
78
+ # Initializes a new Logger.
79
+ # target is a string, it is assumed to be <host>:<port> format of the target syslog server
80
+ #
81
+ def initialize(target,context = nil)
82
+ @syslogger = SyslogClient.new(target)
83
+
84
+ @contexts = SequencedHash.new
85
+ self.context.add(context) if context
86
+ end
87
+
88
+ protected
89
+
90
+ class Context
91
+ # Can optionally be initialized/reset with starting context data supplied via a block
92
+ #
93
+ def initialize(&block)
94
+ @contexts = Hash.new()
95
+ @init_proc = block
96
+
97
+ reset!
98
+ end
99
+
100
+ # Merges the supplied key-value pairs into the context
101
+ #
102
+ def add(hash)
103
+ hash.each_pair { |k,v|
104
+ @contexts[k] = v
105
+ }
106
+ end
107
+
108
+ def reset!
109
+ @contexts.clear
110
+ add(@init_proc.call) if(@init_proc)
111
+ end
112
+
113
+ def apply_context(target_hash)
114
+ @contexts.each_pair { |k,v|
115
+ unless (target_hash.key? k)
116
+ target_hash[k] = (v.is_a? Proc) ? (v.call target_hash,self) : v
117
+ end
118
+ }
119
+ end
120
+ end
121
+
122
+ self.process_context
123
+ end
@@ -0,0 +1,27 @@
1
+ require 'socket'
2
+ require 'time'
3
+
4
+ class SyslogClient
5
+
6
+ def initialize(server = nil)
7
+ raise "Don't support system logger (yet)" unless server
8
+
9
+ (host,port) = server.split(/:/)
10
+ @socket = UDPSocket.new
11
+ @socket.connect(host,port)
12
+ #Allow for really big log lines
13
+ @socket.setsockopt(Socket::SOL_SOCKET,Socket::SO_SNDBUF,24000)
14
+ end
15
+
16
+ def syslog(facility,priority,message)
17
+ #Build the header
18
+ fpnum = ((facility & 0x1F) << 3) | (priority & 0x07)
19
+ host = Socket.gethostname
20
+ tim = Time.now.strftime("%b %d %H:%M:%S")
21
+ #tim = Time.now.xmlschema
22
+ buf = "<#{fpnum}>#{tim} #{host} thetag #{message}"
23
+ @socket.send(buf,0)
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{scribble-client}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Logan Bowers"]
9
+ s.date = %q{2009-05-28}
10
+ s.description = %q{FIX (describe your package)}
11
+ s.email = ["logan@datacurrent.com"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc"]
13
+ s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "lib/scribble-client.rb", "lib/scribble-client/scribbler.rb", "lib/scribble-client/syslog_client.rb", "scribble-client.gemspec", "script/console", "script/destroy", "script/generate", "test/test_helper.rb", "test/test_scribble_client.rb"]
14
+ s.homepage = %q{http://www.example.com/}
15
+ s.post_install_message = %q{PostInstall.txt}
16
+ s.rdoc_options = ["--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{scribble-client}
19
+ s.rubygems_version = %q{1.3.3}
20
+ s.summary = %q{FIX (describe your package)}
21
+ s.test_files = ["test/test_helper.rb", "test/test_scribble_client.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<uuidtools>, [">= 1.0.7"])
29
+ s.add_runtime_dependency(%q<collections>, [">= 0.1.4"])
30
+ s.add_runtime_dependency(%q<json>, [">= 1.1.3"])
31
+ s.add_development_dependency(%q<newgem>, [">= 1.3.0"])
32
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
33
+ else
34
+ s.add_dependency(%q<uuidtools>, [">= 1.0.7"])
35
+ s.add_dependency(%q<collections>, [">= 0.1.4"])
36
+ s.add_dependency(%q<json>, [">= 1.1.3"])
37
+ s.add_dependency(%q<newgem>, [">= 1.3.0"])
38
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
39
+ end
40
+ else
41
+ s.add_dependency(%q<uuidtools>, [">= 1.0.7"])
42
+ s.add_dependency(%q<collections>, [">= 0.1.4"])
43
+ s.add_dependency(%q<json>, [">= 1.1.3"])
44
+ s.add_dependency(%q<newgem>, [">= 1.3.0"])
45
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
46
+ end
47
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/scribble-client.rb'}"
9
+ puts "Loading scribble-client gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/scribble-client'
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestScribbleClient < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+
12
+ def test_makingasyslog
13
+ sc = SyslogClient.new("localhost:511")
14
+
15
+ sc.syslog(23,1,"A syslog!!!")
16
+ end
17
+
18
+ def test_makingascribbler
19
+ scr = Scribbler.new("lrrr.freelard.com:514")
20
+
21
+ scr.event({:evid => 'first event'})
22
+ scr.event({:evid => 'second event'})
23
+ scr.context.reset!
24
+ scr.event({:evid => 'third event (local context reset)'})
25
+ scr.class.thread_context.reset!
26
+ scr.event({:evid => 'fourth event (local and thread contexts reset)'})
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loganb-scribble-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Logan Bowers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: uuidtools
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.7
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: collections
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.4
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.3
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: newgem
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.0
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.8.0
64
+ version:
65
+ description: FIX (describe your package)
66
+ email:
67
+ - logan@datacurrent.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files:
73
+ - History.txt
74
+ - Manifest.txt
75
+ - PostInstall.txt
76
+ - README.rdoc
77
+ files:
78
+ - History.txt
79
+ - Manifest.txt
80
+ - PostInstall.txt
81
+ - README.rdoc
82
+ - Rakefile
83
+ - lib/scribble-client.rb
84
+ - lib/scribble-client/scribbler.rb
85
+ - lib/scribble-client/syslog_client.rb
86
+ - scribble-client.gemspec
87
+ - script/console
88
+ - script/destroy
89
+ - script/generate
90
+ - test/test_helper.rb
91
+ - test/test_scribble_client.rb
92
+ has_rdoc: false
93
+ homepage: http://www.example.com/
94
+ post_install_message: PostInstall.txt
95
+ rdoc_options:
96
+ - --main
97
+ - README.rdoc
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "0"
105
+ version:
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: "0"
111
+ version:
112
+ requirements: []
113
+
114
+ rubyforge_project: scribble-client
115
+ rubygems_version: 1.2.0
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: FIX (describe your package)
119
+ test_files:
120
+ - test/test_helper.rb
121
+ - test/test_scribble_client.rb