New log system.

master
Karchnu 2020-08-24 16:55:35 +02:00
parent 7fc1632d17
commit be32df0d46
1 changed files with 28 additions and 24 deletions

View File

@ -11,6 +11,8 @@ require "base64"
require "digest"
require "./utils"
require "colorize"
# All modifications to standard libraries go there.
require "./lib_modifications.cr"
@ -24,19 +26,21 @@ class Context
class_property verbosity = 1
end
def debug(message : String)
puts "#{CGREEN}#{message}#{CRESET}" if Context.verbosity > 2
class Log
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
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|
parser.on "-l host", "--l host", "IP address to listen on." do |h|
Context.host = h
@ -86,7 +90,7 @@ class InstanceStorage
end
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
fdservice = @switchtable[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
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
# => JSON has to include these attributes: mtype, utype, payload
# message = IPC::Message.new mtype, utype, payload
@ -208,7 +212,7 @@ def websocket_client_connection(client)
wsclient = WebSocket.new client
wsclient.on_pong do |m|
debug "pong #{m}"
Log.debug "pong #{m}"
end
Context.context.is_client[client.fd] = true
@ -380,25 +384,25 @@ Context.service.base_timer = Context.timer_delay
Context.service.loop do |event|
case event
when IPC::Event::Timer
info "IPC::Event::Timer"
Log.info "IPC::Event::Timer"
sending_ping_messages
when IPC::Event::Connection
debug "IPC::Event::Connection: #{event.fd}"
Log.debug "IPC::Event::Connection: #{event.fd}"
when IPC::Event::Disconnection
debug "IPC::Event::Disconnection: #{event.fd}"
Log.debug "IPC::Event::Disconnection: #{event.fd}"
when IPC::Event::ExtraSocket
debug "IPC::Event::ExtraSocket: #{event.fd}"
Log.debug "IPC::Event::ExtraSocket: #{event.fd}"
# 1. accept new websocket clients
if server.fd == event.fd
client = server.accept
begin
websocket_client_connection client
info "new client: #{client.fd}"
Log.info "new client: #{client.fd}"
rescue e
danger "Exception: #{e}"
Log.error "Exception: #{e}"
client.close
end
next
@ -407,21 +411,21 @@ Context.service.loop do |event|
# 2. active fd != server fd
activefd = event.fd
if activefd <= 0
danger "faulty activefd: #{activefd}"
Log.error "faulty activefd: #{activefd}"
end
websocket_switching_procedure activefd
when IPC::Event::Switch
debug "IPC::Event::Switch: from fd #{event.fd}"
Log.debug "IPC::Event::Switch: from fd #{event.fd}"
raise "Not implemented."
# IPC::Event::Message has to be the last entry
# because ExtraSocket and Switch inherit from Message class
when IPC::Event::MessageSent
danger "IPC::Event::MessageSent: #{event.fd}"
Log.error "IPC::Event::MessageSent: #{event.fd}"
when IPC::Event::MessageReceived
debug "IPC::Event::MessageReceived: #{event.fd}"
Log.debug "IPC::Event::MessageReceived: #{event.fd}"
raise "Not implemented."
end
end