woolen_common 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/README.md +31 -0
- data/ext/woolen_common/extconf.rb +26 -0
- data/ext/woolen_common/linux.h +3 -0
- data/ext/woolen_common/win.c +18 -0
- data/ext/woolen_common/win.h +4 -0
- data/ext/woolen_common/win/puts_color.c +139 -0
- data/ext/woolen_common/win/puts_color.h +5 -0
- data/ext/woolen_common/woolen_common.c +20 -0
- data/ext/woolen_common/woolen_common.h +7 -0
- data/lib/woolen_common.rb +39 -0
- data/lib/woolen_common/abstract_middleware/builder.rb +138 -0
- data/lib/woolen_common/abstract_middleware/map_cfg_manager.rb +52 -0
- data/lib/woolen_common/abstract_middleware/runner.rb +72 -0
- data/lib/woolen_common/action_pool_proxy.rb +28 -0
- data/lib/woolen_common/actionpool.rb +10 -0
- data/lib/woolen_common/actionpool/pool.rb +295 -0
- data/lib/woolen_common/actionpool/queue.rb +41 -0
- data/lib/woolen_common/actionpool/thread.rb +181 -0
- data/lib/woolen_common/addr_helper.rb +93 -0
- data/lib/woolen_common/cache.rb +305 -0
- data/lib/woolen_common/common_helper.rb +42 -0
- data/lib/woolen_common/config_manager.rb +36 -0
- data/lib/woolen_common/drb_helper.rb +125 -0
- data/lib/woolen_common/ffi/win32_kernel32.rb +86 -0
- data/lib/woolen_common/logger.rb +419 -0
- data/lib/woolen_common/pcap/mu/fixnum_ext.rb +8 -0
- data/lib/woolen_common/pcap/mu/pcap/ethernet.rb +164 -0
- data/lib/woolen_common/pcap/mu/pcap/header.rb +76 -0
- data/lib/woolen_common/pcap/mu/pcap/io_pair.rb +68 -0
- data/lib/woolen_common/pcap/mu/pcap/io_wrapper.rb +77 -0
- data/lib/woolen_common/pcap/mu/pcap/ip.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv4.rb +274 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv6.rb +149 -0
- data/lib/woolen_common/pcap/mu/pcap/packet.rb +106 -0
- data/lib/woolen_common/pcap/mu/pcap/pkthdr.rb +162 -0
- data/lib/woolen_common/pcap/mu/pcap/reader.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/reader/http_family.rb +175 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp.rb +369 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk.rb +124 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/data.rb +135 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init.rb +101 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init_ack.rb +69 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter.rb +111 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter/ip_address.rb +49 -0
- data/lib/woolen_common/pcap/mu/pcap/stream_packetizer.rb +74 -0
- data/lib/woolen_common/pcap/mu/pcap/tcp.rb +522 -0
- data/lib/woolen_common/pcap/mu/pcap/udp.rb +81 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap.rb +175 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/fields.rb +51 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/rtp.rb +72 -0
- data/lib/woolen_common/pcap/pcap.rb +115 -0
- data/lib/woolen_common/pcap/readme.md +72 -0
- data/lib/woolen_common/ruby_ext/blank.rb +126 -0
- data/lib/woolen_common/ruby_ext/drb_ext.rb +7 -0
- data/lib/woolen_common/ruby_ext/string.rb +116 -0
- data/lib/woolen_common/ruby_ext/win32_ole.rb +4 -0
- data/lib/woolen_common/ruby_proxy.rb +5 -0
- data/lib/woolen_common/ruby_proxy/client.rb +305 -0
- data/lib/woolen_common/ruby_proxy/config.rb +44 -0
- data/lib/woolen_common/ruby_proxy/exceptions.rb +17 -0
- data/lib/woolen_common/ruby_proxy/proxy.rb +157 -0
- data/lib/woolen_common/ruby_proxy/proxy_global_set.rb +44 -0
- data/lib/woolen_common/ruby_proxy/proxy_load.rb +34 -0
- data/lib/woolen_common/ruby_proxy/server.rb +53 -0
- data/lib/woolen_common/splib.rb +36 -0
- data/lib/woolen_common/splib/Array.rb +33 -0
- data/lib/woolen_common/splib/CodeReloader.rb +59 -0
- data/lib/woolen_common/splib/Constants.rb +44 -0
- data/lib/woolen_common/splib/Conversions.rb +47 -0
- data/lib/woolen_common/splib/Exec.rb +132 -0
- data/lib/woolen_common/splib/Float.rb +13 -0
- data/lib/woolen_common/splib/HumanIdealRandomIterator.rb +40 -0
- data/lib/woolen_common/splib/Monitor.rb +214 -0
- data/lib/woolen_common/splib/PriorityQueue.rb +110 -0
- data/lib/woolen_common/splib/Sleep.rb +10 -0
- data/lib/woolen_common/splib/UrlShorteners.rb +48 -0
- data/lib/woolen_common/ssh_proxy.rb +146 -0
- data/lib/woolen_common/system_helper.rb +123 -0
- data/lib/woolen_common/system_monitor.rb +23 -0
- data/lib/woolen_common/system_monitor/linux_monitor.rb +250 -0
- data/lib/woolen_common/system_monitor/windows_monitor.rb +145 -0
- data/lib/woolen_common/type_helper.rb +42 -0
- data/lib/woolen_common/ver_ctrl_middle_ware.rb +92 -0
- data/lib/woolen_common/version.rb +3 -0
- metadata +210 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "#{File.join(File.dirname(__FILE__), 'logger')}"
|
3
|
+
module WoolenCommon
|
4
|
+
class CommonHelper
|
5
|
+
include WoolenCommon::ToolLogger
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def wait_until_stopped
|
9
|
+
info 'Press ENTER or c-C to stop it'
|
10
|
+
$stdout.flush
|
11
|
+
begin
|
12
|
+
loop do
|
13
|
+
sleep 1
|
14
|
+
end
|
15
|
+
rescue Interrupt
|
16
|
+
info 'Interrupt'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def run_project_bin(program,param_str)
|
21
|
+
program_name = get_real_path(WoolenCommon::ConfigManager.project_root,'bin',program)
|
22
|
+
trace "need to run bin [#{program_name} #{param_str}]"
|
23
|
+
if WoolenCommon::SystemHelper.windows?
|
24
|
+
cmd = "#{program_name} #{param_str}".to_gbk
|
25
|
+
`#{cmd}`.to_utf8
|
26
|
+
else
|
27
|
+
cmd = "#{program_name} #{param_str}"
|
28
|
+
`#{cmd}`
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_real_path(*args, &block)
|
33
|
+
file_path = File.expand_path(File.join(*args))
|
34
|
+
if block_given?
|
35
|
+
yield block file_path
|
36
|
+
return file_path
|
37
|
+
end
|
38
|
+
file_path
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'yaml'
|
3
|
+
module WoolenCommon
|
4
|
+
class ConfigManager
|
5
|
+
class << self
|
6
|
+
attr_accessor :project_root
|
7
|
+
|
8
|
+
=begin
|
9
|
+
功能: 获取配置
|
10
|
+
描述:
|
11
|
+
先设置好项目的根目录,如D:/ACAT3.1
|
12
|
+
返回为YAML对象的D:/ATCAT3.1/config/xx.yml
|
13
|
+
参数: name 配置文件的名字
|
14
|
+
返回值:
|
15
|
+
YAML对象
|
16
|
+
未设置root,抛出RuntimeErorr
|
17
|
+
配置不存在,抛出RuntimeErorr
|
18
|
+
举例:
|
19
|
+
ConfigureManager.root = $root
|
20
|
+
puts ConfigureManager.get("test")['test']
|
21
|
+
=end
|
22
|
+
def get(name)
|
23
|
+
name += ".yml" unless name.match(/.yml$/)
|
24
|
+
name = name
|
25
|
+
raise "not set root path, please use ConfigureManager.root=() to set it" if project_root.nil?
|
26
|
+
path = File.join(project_root, "config", name) #File.join(root,"config",name).to_gbk
|
27
|
+
path = File.expand_path(path)
|
28
|
+
#~ path = Pathname.new(path).realpath
|
29
|
+
raise "The special config path #{path} not exist" unless File.exist?(path)
|
30
|
+
# because empty file when get 'false' so we add {} return value.
|
31
|
+
YAML.load_file(path) || {}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
# WoolenCommon::ConfigManager.project_root = File.expand_path(File.join(File.dirname(__FILE__),'..','..','..'))
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'drb'
|
3
|
+
require "#{File.join(File.dirname(__FILE__), 'logger')}"
|
4
|
+
module WoolenCommon
|
5
|
+
SERVER_DEFAULT_PORT = 108801
|
6
|
+
CLIENT_DEFAULT_PORT = 108802
|
7
|
+
DEFAULT_CLIENT_ADDR = '127.0.0.1'
|
8
|
+
module DrbHelp
|
9
|
+
def get_rand_port
|
10
|
+
rand(55534) + 8000
|
11
|
+
end
|
12
|
+
def get_drb_connect_obj(connect_str)
|
13
|
+
begin
|
14
|
+
DRbObject.new_with_uri(connect_str)
|
15
|
+
rescue Exception=>e
|
16
|
+
error "获取客户端的连接信息时出错::#{e.message}"
|
17
|
+
return nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def ip_or_iport_with_default(ip_or_iport, default_port)
|
22
|
+
default_port = default_port.to_s
|
23
|
+
if ! ip_or_iport.include?(':')
|
24
|
+
iport = ip_or_iport + ':' + default_port
|
25
|
+
else
|
26
|
+
iport = ip_or_iport
|
27
|
+
end
|
28
|
+
ip2druby(iport)
|
29
|
+
end
|
30
|
+
|
31
|
+
def ip2druby(ip)
|
32
|
+
unless ip.include?('://')
|
33
|
+
return "druby://" + ip
|
34
|
+
end
|
35
|
+
ip
|
36
|
+
end
|
37
|
+
|
38
|
+
def start_service(service_addr,service_obj = nil)
|
39
|
+
if service_obj
|
40
|
+
DRb.start_service(service_addr,service_obj)
|
41
|
+
else
|
42
|
+
DRb.start_service(service_addr)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module_function :get_drb_connect_obj, :ip_or_iport_with_default, :ip2druby,:start_service,:get_rand_port
|
47
|
+
end
|
48
|
+
|
49
|
+
module DrbServerHelper
|
50
|
+
include DrbHelp
|
51
|
+
include WoolenCommon::ToolLogger
|
52
|
+
|
53
|
+
def server_init(me='127.0.0.1')
|
54
|
+
me_drb_addr = ip_or_iport_with_default(me, SERVER_DEFAULT_PORT)
|
55
|
+
@worker_connect_array = []
|
56
|
+
@worker_connect_array_mutex = Mutex.new
|
57
|
+
debug "server need to start #{me_drb_addr}"
|
58
|
+
@me_service = start_service(me_drb_addr,self)
|
59
|
+
end
|
60
|
+
|
61
|
+
def on_one_worker_connect(worker_ip)
|
62
|
+
debug "get one the worker #{worker_ip}"
|
63
|
+
worker_connect_str = ip_or_iport_with_default worker_ip,CLIENT_DEFAULT_PORT
|
64
|
+
# debug "connect str #{worker_connect_str}"
|
65
|
+
worker_connect = get_drb_connect_obj worker_connect_str
|
66
|
+
worker_id = 0
|
67
|
+
@worker_connect_array_mutex.synchronize do
|
68
|
+
@worker_connect_array << worker_connect
|
69
|
+
worker_id = @worker_connect_array.length
|
70
|
+
end
|
71
|
+
worker_id
|
72
|
+
end
|
73
|
+
|
74
|
+
# 需要阻塞的时候的阻塞函数
|
75
|
+
def wait_until_stopped
|
76
|
+
puts 'Flow replay worker started. Press ENTER or c-C to stop it'
|
77
|
+
$stdout.flush
|
78
|
+
begin
|
79
|
+
STDIN.gets
|
80
|
+
rescue Interrupt
|
81
|
+
puts "Interrupt"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
module_function :server_init
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
module DrbClientHelper
|
89
|
+
include DrbHelp
|
90
|
+
include WoolenCommon::ToolLogger
|
91
|
+
|
92
|
+
def client_init(server_addr='127.0.0.1',my_port=nil)
|
93
|
+
the_port = my_port
|
94
|
+
if my_port
|
95
|
+
@my_url = "druby://0.0.0.0:#{my_port}"
|
96
|
+
@me_service = start_service(@my_url,self)
|
97
|
+
else
|
98
|
+
100.times do
|
99
|
+
begin
|
100
|
+
port = get_rand_port
|
101
|
+
the_port = port
|
102
|
+
if server_addr == '127.0.0.1'
|
103
|
+
@my_url = "druby://127.0.0.1:#{port}"
|
104
|
+
else
|
105
|
+
@my_url = "druby://0.0.0.0:#{port}"
|
106
|
+
end
|
107
|
+
debug "client need to try url #{@my_url}"
|
108
|
+
@me_service = start_service(@my_url,self)
|
109
|
+
break
|
110
|
+
rescue Exception => e
|
111
|
+
error "在使用随机端口时出错了#{e.message}"
|
112
|
+
retry
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
debug "server addr ::#{server_addr},port#{SERVER_DEFAULT_PORT}"
|
117
|
+
server_str = ip_or_iport_with_default(server_addr,SERVER_DEFAULT_PORT)
|
118
|
+
@server_service = get_drb_connect_obj(server_str)
|
119
|
+
url = "druby://#{DEFAULT_CLIENT_ADDR}:#{the_port}"
|
120
|
+
debug "the server#{@server_service}need to invoke worker connect url::#{url}"
|
121
|
+
@server_service.on_one_worker_connect(url)
|
122
|
+
end
|
123
|
+
module_function :client_init
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'ffi'
|
3
|
+
module WoolenCommon
|
4
|
+
module Win32Kernel32
|
5
|
+
STD_INPUT_HANDLE = 0xfffffff6
|
6
|
+
STD_OUTPUT_HANDLE = 0xfffffff5
|
7
|
+
STD_ERROR_HANDLE = 0xfffffff4
|
8
|
+
|
9
|
+
#磁盘需要用到的常量
|
10
|
+
FILE_SHARE_READ = 0x00000000
|
11
|
+
FILE_SHARE_WRITE = 0x00000002
|
12
|
+
OPEN_EXISTING = 3
|
13
|
+
IOCTL_DISK_PERFORMANCE = 458784
|
14
|
+
wszDrive = "\\\\.\\PhysicalDrive0"
|
15
|
+
|
16
|
+
class Filetime < FFI::Struct
|
17
|
+
layout :dwLowDateTime, :uint,
|
18
|
+
:dwHighDateTime, :uint
|
19
|
+
end
|
20
|
+
|
21
|
+
class MemoryStatus < FFI::Struct
|
22
|
+
layout :dwLength, :uint,
|
23
|
+
:dwMemoryLoad, :uint,
|
24
|
+
:dwTotalPhys, :uint,
|
25
|
+
:dwAvailPhys, :uint,
|
26
|
+
:dwTotalPageFile, :uint,
|
27
|
+
:dwAvailPageFile, :uint,
|
28
|
+
:dwTotalVirtual, :uint,
|
29
|
+
:dwAvailVirtual, :uint
|
30
|
+
end
|
31
|
+
|
32
|
+
class Disk_Perfomance < FFI::Struct
|
33
|
+
layout :BytesRead, :int64,
|
34
|
+
:BytesWritten, :int64,
|
35
|
+
:ReadTime, :int64,
|
36
|
+
:WriteTime, :int64,
|
37
|
+
:IdleTime, :int64,
|
38
|
+
:ReadCount, :uint,
|
39
|
+
:WriteCount, :uint,
|
40
|
+
:QueueDepth, :uint,
|
41
|
+
:SplitCount, :uint,
|
42
|
+
:QueryTime, :int64,
|
43
|
+
:StorageDeviceNumber, :uint,
|
44
|
+
:StorageManagerName, [:ushort, 8]
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
extend FFI::Library
|
49
|
+
ffi_lib 'kernel32'
|
50
|
+
|
51
|
+
# 设置输出格式
|
52
|
+
attach_function :setConsoleTextAttribute, # method name (your choice)
|
53
|
+
:SetConsoleTextAttribute, # DLL function name (given)
|
54
|
+
[:uint, :uint], :int
|
55
|
+
# 获取标准输出的Handle
|
56
|
+
attach_function :getStdHandle, # method name (your choice)
|
57
|
+
:GetStdHandle, # DLL function name (given)
|
58
|
+
[:uint], :uint # specify C param / return value types
|
59
|
+
# 获取标准输出的Handle
|
60
|
+
attach_function :getSystemTimes, # method name (your choice)
|
61
|
+
:GetSystemTimes, # DLL function name (given)
|
62
|
+
[:pointer, :pointer, :pointer], :int # specify C param / return value types
|
63
|
+
# 查询内存状态API
|
64
|
+
attach_function :globalMemoryStatus, # method name (your choice)
|
65
|
+
:GlobalMemoryStatus, # DLL function name (given)
|
66
|
+
[:pointer], :void # specify C param / return value types
|
67
|
+
#创建物理磁盘文件
|
68
|
+
attach_function :createFileA, # method name (your choice)
|
69
|
+
:CreateFileA, # DLL function name (given)
|
70
|
+
[:pointer, :uint, :uint, :int, :uint, :uint, :int], :pointer # specify C param / return value types
|
71
|
+
#获取磁盘数据API
|
72
|
+
attach_function :deviceIoControl, # method name (your choice)
|
73
|
+
:DeviceIoControl, # DLL function name (given)
|
74
|
+
[:pointer, :uint, :int, :uint, :pointer, :uint, :pointer, :int], :int # specify C param / return value types
|
75
|
+
ffi_lib 'iphlpapi'
|
76
|
+
#获取网络数据API
|
77
|
+
attach_function :getIfTable, # method name (your choice)
|
78
|
+
:GetIfTable, # DLL function name (given)
|
79
|
+
[:pointer, :pointer, :int], :uint # specify C param / return value types
|
80
|
+
attach_function :getAdaptersAddresses, # method name (your choice)
|
81
|
+
:GetAdaptersAddresses, # DLL function name (given)
|
82
|
+
[:uint, :uint, :int, :pointer, :pointer], :uint # specify C param / return value types
|
83
|
+
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,419 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'logger'
|
3
|
+
require 'yaml'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'pathname'
|
6
|
+
require "#{File.join(File.dirname(__FILE__), 'system_helper')}"
|
7
|
+
require "#{File.join(File.dirname(__FILE__), 'config_manager')}"
|
8
|
+
module WoolenCommon
|
9
|
+
class MyLogger # :nodoc: all
|
10
|
+
include SystemHelper
|
11
|
+
LEVELS = ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']
|
12
|
+
COLORS = { 'TRACE' => 'white', 'DEBUG' => 'silver', 'INFO' => 'green', 'WARN' => 'yellow', 'ERROR' => 'purple', 'FATAL' => 'red' }
|
13
|
+
FOREGROUND_BLUE = 1
|
14
|
+
FOREGROUND_GREEN = 2
|
15
|
+
FOREGROUND_RED = 4
|
16
|
+
FOREGROUND_INTENSITY = 8
|
17
|
+
BACKGROUND_BLUE = 16
|
18
|
+
BACKGROUND_GREEN = 32
|
19
|
+
BACKGROUND_RED = 64
|
20
|
+
BACKGROUND_INTENSITY = 128
|
21
|
+
WIN32COLORS = [
|
22
|
+
"default", "black", "navy", "green",
|
23
|
+
"teal", "maroon", "purple", "olive",
|
24
|
+
"silver", "gray", "blue", "lime",
|
25
|
+
"aqua", "red", "fuchsia", "yellow",
|
26
|
+
"white"
|
27
|
+
]
|
28
|
+
WIN32_FOREGROUND_COLOR_MOD = [
|
29
|
+
-1, 0,
|
30
|
+
FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_BLUE | FOREGROUND_GREEN,
|
31
|
+
FOREGROUND_RED, FOREGROUND_BLUE | FOREGROUND_RED, FOREGROUND_RED | FOREGROUND_GREEN,
|
32
|
+
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
|
33
|
+
]
|
34
|
+
WIN32_BACKGROUND_COLOR_MOD = [
|
35
|
+
-1, 0,
|
36
|
+
BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_BLUE | BACKGROUND_GREEN,
|
37
|
+
BACKGROUND_RED, BACKGROUND_BLUE | BACKGROUND_RED, BACKGROUND_RED | BACKGROUND_GREEN,
|
38
|
+
BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
|
39
|
+
]
|
40
|
+
# WIN_PRINTER = Pathname.new(File.join(__FILE__, '..', '..', '..', 'bin', 'puts_color.exe')).realpath.to_s
|
41
|
+
attr_reader :file, :stdout, :name
|
42
|
+
attr_accessor :level, :log_cache, :cache_msg, :cache_count
|
43
|
+
|
44
|
+
LOGGERS = []
|
45
|
+
|
46
|
+
def linux_puts_color(message, color = nil)
|
47
|
+
case color
|
48
|
+
when 'red'
|
49
|
+
color = '31;1'
|
50
|
+
when 'green'
|
51
|
+
color = '32;1'
|
52
|
+
when 'yellow'
|
53
|
+
color = '33;1'
|
54
|
+
when 'blue'
|
55
|
+
color = '34;1'
|
56
|
+
when 'purple'
|
57
|
+
color = '35;1'
|
58
|
+
when 'silver'
|
59
|
+
color = '36;1'
|
60
|
+
when 'white'
|
61
|
+
color = '37;1'
|
62
|
+
else
|
63
|
+
color = ''
|
64
|
+
end
|
65
|
+
if color == ''
|
66
|
+
print "#{message}\n"
|
67
|
+
else
|
68
|
+
print "\e[#{color}m#{message}\e[0m\n"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def win32_puts_color(message, color)
|
73
|
+
require "#{File.join(File.dirname(__FILE__), 'ffi', 'win32_kernel32')}"
|
74
|
+
the_out_handle = Win32Kernel32.getStdHandle Win32Kernel32::STD_OUTPUT_HANDLE
|
75
|
+
if WIN32COLORS.include? color
|
76
|
+
if WIN32COLORS.index(color) >= WIN32_FOREGROUND_COLOR_MOD.length
|
77
|
+
the_color_mod = WIN32_FOREGROUND_COLOR_MOD[WIN32COLORS.index(color) % WIN32_FOREGROUND_COLOR_MOD.length] | FOREGROUND_INTENSITY
|
78
|
+
else
|
79
|
+
the_color_mod = WIN32_FOREGROUND_COLOR_MOD[WIN32COLORS.index(color)]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
Win32Kernel32.setConsoleTextAttribute the_out_handle, 0x00ff & the_color_mod
|
83
|
+
printf "#{message}\n"
|
84
|
+
Win32Kernel32.setConsoleTextAttribute the_out_handle, 0x00ff & (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
|
85
|
+
end
|
86
|
+
|
87
|
+
def my_puts(message, color = nil)
|
88
|
+
if windows?
|
89
|
+
win32_puts_color message, color
|
90
|
+
else
|
91
|
+
linux_puts_color message, color
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
def initialize(attrs = {})
|
97
|
+
#puts "=> init logger with: #{attrs.inspect}"
|
98
|
+
@stdout = (attrs[:stdout] == 1)
|
99
|
+
@name = attrs[:name]
|
100
|
+
@filename = attrs[:file].gsub(/\.log/,".#{Process.pid}.log")
|
101
|
+
FileUtils.mkdir_p(File.dirname(@filename))
|
102
|
+
@roll_type = attrs[:roll_type]
|
103
|
+
@roll_param = attrs[:roll_param]
|
104
|
+
@log_cache = attrs[:log_cache].blank? ? 1 : attrs[:max_log_cnt].to_i
|
105
|
+
@max_log_cnt = attrs[:max_log_cnt].blank? ? 10 : attrs[:max_log_cnt].to_i
|
106
|
+
@cache_msg = {}
|
107
|
+
@cache_count = 0
|
108
|
+
@caller = attrs[:caller] || 1
|
109
|
+
if @roll_type == "file_size" && @roll_param && (@roll_param = @roll_param.to_s)
|
110
|
+
size = nil
|
111
|
+
if @roll_param.index("K")
|
112
|
+
size = @roll_param.to_i * 1024
|
113
|
+
elsif @roll_param.index("M")
|
114
|
+
size = @roll_param.to_i * 1024 * 1024
|
115
|
+
end
|
116
|
+
@roll_param = size
|
117
|
+
end
|
118
|
+
@level = LEVELS.index(attrs[:level].upcase) || 1
|
119
|
+
@last_log_time = nil
|
120
|
+
clean_log @filename
|
121
|
+
@file = File.open(@filename, "a+") if @filename
|
122
|
+
# clean_log
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
def self.loggers
|
127
|
+
return LOGGERS
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.add_log(logger)
|
131
|
+
LOGGERS << logger
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.get(name)
|
135
|
+
LOGGERS.each do |__log__|
|
136
|
+
if __log__.name == name
|
137
|
+
return __log__
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
attr_writer :caller
|
143
|
+
|
144
|
+
def rename_and_create_new(newfilename)
|
145
|
+
# fix Error::EACCESS exception throw when file is opened before rename by lyf
|
146
|
+
begin
|
147
|
+
@file.flush
|
148
|
+
@file.close
|
149
|
+
FileUtils.cp(@file.path, newfilename)
|
150
|
+
clean_log @file.path
|
151
|
+
@file.reopen(@file.path, "w")
|
152
|
+
rescue Exception => e
|
153
|
+
puts "error when try to rename_and_create_new #{newfilename}"
|
154
|
+
end
|
155
|
+
#unless (FileUtils.cp(@file.path, newfilename) and FileUtils.rm_f(@file.path) and @file.reopen(@file.path, "w"))
|
156
|
+
# #puts "==> error rename #{@filename} => #{newfilename}"
|
157
|
+
#end
|
158
|
+
end
|
159
|
+
|
160
|
+
def clean_log(the_path)
|
161
|
+
log_patten = File.join(File.dirname(the_path), '*.log')
|
162
|
+
# puts "need clean #{the_path}"
|
163
|
+
FileUtils.rm_f(the_path)
|
164
|
+
sort_time_files = Dir[log_patten].sort_by { |file| test(?M, file) }
|
165
|
+
# puts "sort_time_files #{sort_time_files}"
|
166
|
+
if @max_log_cnt && @max_log_cnt > 0 && sort_time_files.length > @max_log_cnt
|
167
|
+
(sort_time_files.length - @max_log_cnt).times do |cnt|
|
168
|
+
puts "need to del #{sort_time_files[cnt]} cnt #{cnt}"
|
169
|
+
FileUtils.rm_f sort_time_files[cnt]
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def check_split_file
|
175
|
+
begin
|
176
|
+
if @roll_type == "daily"
|
177
|
+
if @last_log_time && @last_log_time.day != Time.now.day
|
178
|
+
p = @file.path
|
179
|
+
new_name = nil
|
180
|
+
if p.rindex(".") > p.rindex("/")
|
181
|
+
new_name = "#{p[0, p.rindex(".")]}.#{@last_log_time.strftime("%Y-%m-%d")}#{p[p.rindex("."), p.length]}"
|
182
|
+
else
|
183
|
+
new_name = "#{p}.#{@last_log_time.strftime("%Y-%m-%d")}"
|
184
|
+
end
|
185
|
+
rename_and_create_new(new_name)
|
186
|
+
end
|
187
|
+
elsif @roll_type == "file_size"
|
188
|
+
if File.size(@file) >= @roll_param
|
189
|
+
p = @file.path
|
190
|
+
new_name = nil
|
191
|
+
if p.rindex(".") > p.rindex("/")
|
192
|
+
new_name = "#{p[0, p.rindex(".")]}.#{Time.now.strftime("%Y%m%d%H%M")}#{p[p.rindex("."), p.length]}"
|
193
|
+
else
|
194
|
+
new_name = "#{p}.#{Time.now.strftime("%Y%m%d%H%M")}"
|
195
|
+
end
|
196
|
+
rename_and_create_new(new_name)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
rescue Exception => e
|
200
|
+
puts "\n============= log error msg:#{e.message}!!! =============\n"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def truncate
|
205
|
+
@file.truncate(0)
|
206
|
+
@file.flush
|
207
|
+
end
|
208
|
+
|
209
|
+
def log(_level, msg, err = nil)
|
210
|
+
begin
|
211
|
+
check_split_file if @file
|
212
|
+
err_msg = nil
|
213
|
+
line = ''
|
214
|
+
if caller[@caller].respond_to? :include?
|
215
|
+
#my_puts "proj root ::#{ConfigManager.project_root}"
|
216
|
+
line = find_the_project_caller_line(@caller)
|
217
|
+
else
|
218
|
+
line = caller[1].gsub(ConfigManager.project_root, '')
|
219
|
+
end
|
220
|
+
_msg = "#{_level},Thread::#{Thread.current},#{Time.now.strftime("%y-%m-%d %H:%M:%S")}\n#{line}: #{msg}"
|
221
|
+
_msg += "\n" if msg[-1] != "\n"
|
222
|
+
if $stdouttype == "GBK"
|
223
|
+
_msg = _msg.to_gbk
|
224
|
+
end
|
225
|
+
err_msg = "#{err.message}\n#{err.backtrace.join("\n\t")}" if err
|
226
|
+
the_key = _level.strip
|
227
|
+
@cache_msg[the_key] ||= ''
|
228
|
+
@cache_msg[the_key] << _msg
|
229
|
+
@cache_msg[the_key] << err_msg if err_msg
|
230
|
+
if @cache_count < @log_cache.to_i
|
231
|
+
@cache_count += 1
|
232
|
+
else
|
233
|
+
file_need_to_pus_cache = ''
|
234
|
+
the_msg_cache = @cache_msg
|
235
|
+
@cache_count = 0
|
236
|
+
@cache_msg = {}
|
237
|
+
the_msg_cache.each do |key, value|
|
238
|
+
my_puts value, COLORS[key] if @stdout
|
239
|
+
#puts "need to log with #{@file} [#{value}]"
|
240
|
+
file_need_to_pus_cache << value
|
241
|
+
#@file.dup if @file
|
242
|
+
end
|
243
|
+
if @file
|
244
|
+
@file.print file_need_to_pus_cache
|
245
|
+
@file.flush
|
246
|
+
end
|
247
|
+
end
|
248
|
+
@last_log_time = Time.now
|
249
|
+
rescue Exception => e
|
250
|
+
puts "\n==========log error !!! err msg is [#{e.message}][stack::#{e.backtrace.join("\n")}] you need to log is \n#{_msg}\n"
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def find_the_project_caller_line(the_caller)
|
255
|
+
line = ''
|
256
|
+
the_caller += 1
|
257
|
+
loop do
|
258
|
+
if caller[the_caller].respond_to? :include?
|
259
|
+
#my_puts "caller::#{the_caller}====#{caller[the_caller]}"
|
260
|
+
if caller[the_caller].include? ConfigManager.project_root and not caller[the_caller].include? __FILE__
|
261
|
+
#my_puts "===get the caller ::#{the_caller} #{caller[the_caller]} "
|
262
|
+
line = caller[the_caller].gsub(ConfigManager.project_root, '')
|
263
|
+
break
|
264
|
+
end
|
265
|
+
the_caller += 1
|
266
|
+
else
|
267
|
+
line = caller[3].gsub(ConfigManager.project_root, '')
|
268
|
+
break
|
269
|
+
end
|
270
|
+
end
|
271
|
+
line
|
272
|
+
end
|
273
|
+
|
274
|
+
def soft(*msg)
|
275
|
+
log('SOFT ', msg.join(','), nil) if true
|
276
|
+
nil
|
277
|
+
end
|
278
|
+
|
279
|
+
def trace(msg, err = nil)
|
280
|
+
log('TRACE ', msg, err) if @level == 0
|
281
|
+
nil
|
282
|
+
end
|
283
|
+
|
284
|
+
def debug(msg, err = nil)
|
285
|
+
log('DEBUG ', msg, err) if @level <= 1
|
286
|
+
nil
|
287
|
+
end
|
288
|
+
|
289
|
+
def info(msg, err = nil)
|
290
|
+
log('INFO ', msg, err) if @level <= 2
|
291
|
+
nil
|
292
|
+
end
|
293
|
+
|
294
|
+
def warn(msg, err = nil)
|
295
|
+
log('WARN ', msg, err) if @level <= 3
|
296
|
+
nil
|
297
|
+
end
|
298
|
+
|
299
|
+
def error(msg, err = nil)
|
300
|
+
log('ERROR ', msg, err) if @level <= 4
|
301
|
+
nil
|
302
|
+
end
|
303
|
+
|
304
|
+
def fatal(msg, err = nil)
|
305
|
+
log('FATAL ', msg, err)
|
306
|
+
nil
|
307
|
+
end
|
308
|
+
|
309
|
+
def debug?
|
310
|
+
@level == 1
|
311
|
+
end
|
312
|
+
|
313
|
+
def info?
|
314
|
+
@level >= 2
|
315
|
+
end
|
316
|
+
|
317
|
+
def add(*)
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
|
322
|
+
class SingleLogger
|
323
|
+
class << self
|
324
|
+
attr_accessor :logger_config, :my_logger
|
325
|
+
end
|
326
|
+
|
327
|
+
def self.get_conf
|
328
|
+
the_cfg_file = File.expand_path(File.join(ConfigManager.project_root, 'config', 'logger.yml'))
|
329
|
+
if SingleLogger.logger_config.blank?
|
330
|
+
if File.exist?(the_cfg_file)
|
331
|
+
SingleLogger.logger_config = YAML.load_file(the_cfg_file)
|
332
|
+
end
|
333
|
+
end
|
334
|
+
return SingleLogger.logger_config if SingleLogger.logger_config
|
335
|
+
{}
|
336
|
+
end
|
337
|
+
|
338
|
+
key = get_conf['default'] || 'dev'
|
339
|
+
log_cfg = get_conf[key] || {}
|
340
|
+
SingleLogger.my_logger ||= MyLogger.new({ :stdout => log_cfg['stdout'] || 1, :name => key,
|
341
|
+
:file => File.join(ConfigManager.project_root, log_cfg['file'] || './log/dev.log'),
|
342
|
+
:roll_type => log_cfg['roll_type'] || 'file_size',
|
343
|
+
:roll_param => log_cfg['roll_param'] || '5M',
|
344
|
+
:max_log_cnt => log_cfg['max_log_cnt'] || 10,
|
345
|
+
:level => log_cfg['level'] || 'debug',
|
346
|
+
:log_cache => log_cfg['log_cache'] || 0,
|
347
|
+
:caller => 2 })
|
348
|
+
|
349
|
+
def self.new(*args)
|
350
|
+
SingleLogger.my_logger || super(*args)
|
351
|
+
end
|
352
|
+
|
353
|
+
def self.get_logger
|
354
|
+
SingleLogger.my_logger || self.new()
|
355
|
+
end
|
356
|
+
|
357
|
+
def initialize(*args)
|
358
|
+
SingleLogger.my_logger || super(*args)
|
359
|
+
end
|
360
|
+
|
361
|
+
|
362
|
+
def self.method_missing(*arg)
|
363
|
+
SingleLogger.my_logger.send(*arg)
|
364
|
+
end
|
365
|
+
end
|
366
|
+
module ToolLogger
|
367
|
+
def log(*arg)
|
368
|
+
SingleLogger.get_logger.log(*arg)
|
369
|
+
end
|
370
|
+
|
371
|
+
def trace(*arg)
|
372
|
+
SingleLogger.get_logger.trace(*arg)
|
373
|
+
end
|
374
|
+
|
375
|
+
def debug(*arg)
|
376
|
+
SingleLogger.get_logger.debug(*arg)
|
377
|
+
end
|
378
|
+
|
379
|
+
def info(*arg)
|
380
|
+
SingleLogger.get_logger.info(*arg)
|
381
|
+
end
|
382
|
+
|
383
|
+
def warn(*arg)
|
384
|
+
SingleLogger.get_logger.warn(*arg)
|
385
|
+
end
|
386
|
+
|
387
|
+
def error(*arg)
|
388
|
+
SingleLogger.get_logger.error(*arg)
|
389
|
+
end
|
390
|
+
|
391
|
+
def fatal(*arg)
|
392
|
+
SingleLogger.get_logger.fatal(*arg)
|
393
|
+
raise arg[1]
|
394
|
+
end
|
395
|
+
|
396
|
+
def debug?
|
397
|
+
SingleLogger.get_logger.debug?
|
398
|
+
end
|
399
|
+
|
400
|
+
def info?
|
401
|
+
SingleLogger.get_logger.info?
|
402
|
+
end
|
403
|
+
|
404
|
+
module_function :log, :trace, :debug, :info, :warn, :error, :fatal, :debug?, :info?
|
405
|
+
|
406
|
+
def self.included(base)
|
407
|
+
base.extend self
|
408
|
+
end
|
409
|
+
|
410
|
+
#def method_missing(*arg)
|
411
|
+
# SingleLogger.get_logger.send(*arg)
|
412
|
+
#end
|
413
|
+
|
414
|
+
def self.method_missing(*arg)
|
415
|
+
SingleLogger.get_logger.send(*arg)
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
419
|
+
#TestTool::ToolLogger.error("123")
|