New log system.
parent
7fc1632d17
commit
be32df0d46
|
@ -11,6 +11,8 @@ require "base64"
|
||||||
require "digest"
|
require "digest"
|
||||||
require "./utils"
|
require "./utils"
|
||||||
|
|
||||||
|
require "colorize"
|
||||||
|
|
||||||
# All modifications to standard libraries go there.
|
# All modifications to standard libraries go there.
|
||||||
require "./lib_modifications.cr"
|
require "./lib_modifications.cr"
|
||||||
|
|
||||||
|
@ -24,19 +26,21 @@ class Context
|
||||||
class_property verbosity = 1
|
class_property verbosity = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def debug(message : String)
|
class Log
|
||||||
puts "#{CGREEN}#{message}#{CRESET}" if Context.verbosity > 2
|
def self.debug(message)
|
||||||
|
STDOUT << ":: ".colorize(:green) << message.colorize(:white) << "\n" if ::Context.verbosity > 2
|
||||||
|
end
|
||||||
|
def self.info(message)
|
||||||
|
STDOUT << ":: ".colorize(:blue) << message.colorize(:white) << "\n" if ::Context.verbosity > 1
|
||||||
|
end
|
||||||
|
def self.warning(message)
|
||||||
|
STDERR << "?? ".colorize(:yellow) << message.colorize(:yellow) << "\n" if ::Context.verbosity > 0
|
||||||
|
end
|
||||||
|
def self.error(message)
|
||||||
|
STDERR << "!! ".colorize(:red) << message.colorize(:red) << "\n" if ::Context.verbosity > 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def info(message : String)
|
|
||||||
puts "#{CBLUE}#{message}#{CRESET}" if Context.verbosity > 1
|
|
||||||
end
|
|
||||||
|
|
||||||
def danger(message : String)
|
|
||||||
puts "#{CORANGE}#{message}#{CRESET}" if Context.verbosity > 0
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
OptionParser.parse do |parser|
|
OptionParser.parse do |parser|
|
||||||
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
||||||
Context.host = h
|
Context.host = h
|
||||||
|
@ -86,7 +90,7 @@ class InstanceStorage
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_fd (fdclient : Int32)
|
def remove_fd (fdclient : Int32)
|
||||||
info "closing the client:#{CRESET} #{fdclient}"
|
Log.info "closing the client:#{CRESET} #{fdclient}"
|
||||||
# 1. closing both the client and the service
|
# 1. closing both the client and the service
|
||||||
fdservice = @switchtable[fdclient]?
|
fdservice = @switchtable[fdclient]?
|
||||||
tcpfdc = @fd_to_tcpsocket[fdclient]
|
tcpfdc = @fd_to_tcpsocket[fdclient]
|
||||||
|
@ -193,7 +197,7 @@ def websocket_client_connection(client)
|
||||||
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
|
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
|
||||||
|
|
||||||
sfd = Context.context.switchtable[client.fd]
|
sfd = Context.context.switchtable[client.fd]
|
||||||
info "trackingd - sending the IP address #{real_ip_address} to fd #{sfd}"
|
Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{sfd}"
|
||||||
# message = IPC::Message.from_json(JSON).to_packet
|
# message = IPC::Message.from_json(JSON).to_packet
|
||||||
# => JSON has to include these attributes: mtype, utype, payload
|
# => JSON has to include these attributes: mtype, utype, payload
|
||||||
# message = IPC::Message.new mtype, utype, payload
|
# message = IPC::Message.new mtype, utype, payload
|
||||||
|
@ -208,7 +212,7 @@ def websocket_client_connection(client)
|
||||||
|
|
||||||
wsclient = WebSocket.new client
|
wsclient = WebSocket.new client
|
||||||
wsclient.on_pong do |m|
|
wsclient.on_pong do |m|
|
||||||
debug "pong #{m}"
|
Log.debug "pong #{m}"
|
||||||
end
|
end
|
||||||
Context.context.is_client[client.fd] = true
|
Context.context.is_client[client.fd] = true
|
||||||
|
|
||||||
|
@ -380,25 +384,25 @@ Context.service.base_timer = Context.timer_delay
|
||||||
Context.service.loop do |event|
|
Context.service.loop do |event|
|
||||||
case event
|
case event
|
||||||
when IPC::Event::Timer
|
when IPC::Event::Timer
|
||||||
info "IPC::Event::Timer"
|
Log.info "IPC::Event::Timer"
|
||||||
sending_ping_messages
|
sending_ping_messages
|
||||||
when IPC::Event::Connection
|
when IPC::Event::Connection
|
||||||
debug "IPC::Event::Connection: #{event.fd}"
|
Log.debug "IPC::Event::Connection: #{event.fd}"
|
||||||
|
|
||||||
when IPC::Event::Disconnection
|
when IPC::Event::Disconnection
|
||||||
debug "IPC::Event::Disconnection: #{event.fd}"
|
Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
||||||
|
|
||||||
when IPC::Event::ExtraSocket
|
when IPC::Event::ExtraSocket
|
||||||
debug "IPC::Event::ExtraSocket: #{event.fd}"
|
Log.debug "IPC::Event::ExtraSocket: #{event.fd}"
|
||||||
|
|
||||||
# 1. accept new websocket clients
|
# 1. accept new websocket clients
|
||||||
if server.fd == event.fd
|
if server.fd == event.fd
|
||||||
client = server.accept
|
client = server.accept
|
||||||
begin
|
begin
|
||||||
websocket_client_connection client
|
websocket_client_connection client
|
||||||
info "new client: #{client.fd}"
|
Log.info "new client: #{client.fd}"
|
||||||
rescue e
|
rescue e
|
||||||
danger "Exception: #{e}"
|
Log.error "Exception: #{e}"
|
||||||
client.close
|
client.close
|
||||||
end
|
end
|
||||||
next
|
next
|
||||||
|
@ -407,21 +411,21 @@ Context.service.loop do |event|
|
||||||
# 2. active fd != server fd
|
# 2. active fd != server fd
|
||||||
activefd = event.fd
|
activefd = event.fd
|
||||||
if activefd <= 0
|
if activefd <= 0
|
||||||
danger "faulty activefd: #{activefd}"
|
Log.error "faulty activefd: #{activefd}"
|
||||||
end
|
end
|
||||||
websocket_switching_procedure activefd
|
websocket_switching_procedure activefd
|
||||||
|
|
||||||
when IPC::Event::Switch
|
when IPC::Event::Switch
|
||||||
debug "IPC::Event::Switch: from fd #{event.fd}"
|
Log.debug "IPC::Event::Switch: from fd #{event.fd}"
|
||||||
raise "Not implemented."
|
raise "Not implemented."
|
||||||
|
|
||||||
# IPC::Event::Message has to be the last entry
|
# IPC::Event::Message has to be the last entry
|
||||||
# because ExtraSocket and Switch inherit from Message class
|
# because ExtraSocket and Switch inherit from Message class
|
||||||
when IPC::Event::MessageSent
|
when IPC::Event::MessageSent
|
||||||
danger "IPC::Event::MessageSent: #{event.fd}"
|
Log.error "IPC::Event::MessageSent: #{event.fd}"
|
||||||
|
|
||||||
when IPC::Event::MessageReceived
|
when IPC::Event::MessageReceived
|
||||||
debug "IPC::Event::MessageReceived: #{event.fd}"
|
Log.debug "IPC::Event::MessageReceived: #{event.fd}"
|
||||||
raise "Not implemented."
|
raise "Not implemented."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Reference in New Issue