New logging system.
This commit is contained in:
parent
abd8561308
commit
0b8fea6689
@ -21,13 +21,13 @@ dependencies:
|
|||||||
|
|
||||||
|
|
||||||
targets:
|
targets:
|
||||||
common-tests:
|
|
||||||
main: src/tests/common-tests.cr
|
|
||||||
json-tests:
|
|
||||||
main: src/tests/json_tests.cr
|
|
||||||
filestoraged:
|
filestoraged:
|
||||||
main: src/server/main.cr
|
main: src/server/main.cr
|
||||||
client:
|
client:
|
||||||
main: src/client/main.cr
|
main: src/client/main.cr
|
||||||
|
common-tests:
|
||||||
|
main: src/tests/common-tests.cr
|
||||||
|
json-tests:
|
||||||
|
main: src/tests/json_tests.cr
|
||||||
|
|
||||||
license: MIT
|
license: MIT
|
||||||
|
@ -27,7 +27,7 @@ class FileStorage::Request
|
|||||||
|
|
||||||
user_data = filestoraged.get_user_data user.uid
|
user_data = filestoraged.get_user_data user.uid
|
||||||
|
|
||||||
puts "PutChunk request: #{@mid}, file #{@filedigest}, chunk: n=#{@chunk.n}, on=#{@chunk.on}, digest=#{@chunk.digest}"
|
Log.info "PutChunk request: #{@mid}, file #{@filedigest}, chunk: n=#{@chunk.n}, on=#{@chunk.on}, digest=#{@chunk.digest}"
|
||||||
|
|
||||||
filestoraged.storage.write_chunk self, user_data
|
filestoraged.storage.write_chunk self, user_data
|
||||||
rescue e
|
rescue e
|
||||||
|
@ -29,6 +29,26 @@ require "./network.cr"
|
|||||||
|
|
||||||
require "dodb"
|
require "dodb"
|
||||||
|
|
||||||
|
class Context
|
||||||
|
class_property verbosity = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
class FileStorage::Service < IPC::Server
|
class FileStorage::Service < IPC::Server
|
||||||
# List of connected users (fd => uid).
|
# List of connected users (fd => uid).
|
||||||
property connected_users = Hash(Int32, Int32).new
|
property connected_users = Hash(Int32, Int32).new
|
||||||
@ -65,16 +85,6 @@ class FileStorage::Service < IPC::Server
|
|||||||
@logged_users[fd]?
|
@logged_users[fd]?
|
||||||
end
|
end
|
||||||
|
|
||||||
def info(message)
|
|
||||||
STDOUT << ":: ".colorize(:green) << message.colorize(:white) << "\n"
|
|
||||||
end
|
|
||||||
def warning(message)
|
|
||||||
STDERR << "?? ".colorize(:yellow) << message.colorize(:yellow) << "\n"
|
|
||||||
end
|
|
||||||
def error(message)
|
|
||||||
STDERR << "!! ".colorize(:red) << message.colorize(:red) << "\n"
|
|
||||||
end
|
|
||||||
|
|
||||||
def decode_token(token : String)
|
def decode_token(token : String)
|
||||||
@auth.decode_token token
|
@auth.decode_token token
|
||||||
end
|
end
|
||||||
@ -101,21 +111,21 @@ class FileStorage::Service < IPC::Server
|
|||||||
#end
|
#end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
info "Starting filestoraged"
|
Log.info "Starting filestoraged"
|
||||||
|
|
||||||
self.loop do |event|
|
self.loop do |event|
|
||||||
begin
|
begin
|
||||||
|
|
||||||
case event
|
case event
|
||||||
when IPC::Event::Timer
|
when IPC::Event::Timer
|
||||||
puts "#{CORANGE}IPC::Event::Timer#{CRESET}"
|
Log.debug "IPC::Event::Timer"
|
||||||
|
|
||||||
when IPC::Event::Connection
|
when IPC::Event::Connection
|
||||||
puts "#{CBLUE}IPC::Event::Connection: #{event.fd}#{CRESET}"
|
Log.debug "IPC::Event::Connection: #{event.fd}"
|
||||||
@all_connections << event.fd
|
@all_connections << event.fd
|
||||||
|
|
||||||
when IPC::Event::Disconnection
|
when IPC::Event::Disconnection
|
||||||
puts "#{CBLUE}IPC::Event::Disconnection: #{event.fd}#{CRESET}"
|
Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
||||||
fd = event.fd
|
fd = event.fd
|
||||||
|
|
||||||
@logged_users.delete fd
|
@logged_users.delete fd
|
||||||
@ -126,15 +136,15 @@ class FileStorage::Service < IPC::Server
|
|||||||
end
|
end
|
||||||
|
|
||||||
when IPC::Event::ExtraSocket
|
when IPC::Event::ExtraSocket
|
||||||
puts "#{CRED}IPC::Event::ExtraSocket: should not happen in this service#{CRESET}"
|
Log.warning "IPC::Event::ExtraSocket: should not happen in this service"
|
||||||
|
|
||||||
when IPC::Event::Switch
|
when IPC::Event::Switch
|
||||||
puts "#{CRED}IPC::Event::Switch: should not happen in this service#{CRESET}"
|
Log.warning "IPC::Event::Switch: should not happen in this service"
|
||||||
|
|
||||||
# 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::MessageReceived
|
when IPC::Event::MessageReceived
|
||||||
puts "#{CBLUE}IPC::Event::Message#{CRESET}: #{event.fd}"
|
Log.debug "IPC::Event::Message: #{event.fd}"
|
||||||
|
|
||||||
request_start = Time.utc
|
request_start = Time.utc
|
||||||
|
|
||||||
@ -144,15 +154,15 @@ class FileStorage::Service < IPC::Server
|
|||||||
raise "unknown request type"
|
raise "unknown request type"
|
||||||
end
|
end
|
||||||
|
|
||||||
info "<< #{request.class.name.sub /^FileStorage::Request::/, ""}"
|
Log.info "<< #{request.class.name.sub /^FileStorage::Request::/, ""}"
|
||||||
|
|
||||||
response = request.handle self, event
|
response = request.handle self, event
|
||||||
response_type = response.class.name
|
response_type = response.class.name
|
||||||
|
|
||||||
if response.responds_to?(:reason)
|
if response.responds_to?(:reason)
|
||||||
warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})"
|
Log.warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})"
|
||||||
else
|
else
|
||||||
info ">> #{response_type.sub /^FileStorage::Response::/, ""}"
|
Log.info ">> #{response_type.sub /^FileStorage::Response::/, ""}"
|
||||||
end
|
end
|
||||||
|
|
||||||
#################################################################
|
#################################################################
|
||||||
@ -180,18 +190,18 @@ class FileStorage::Service < IPC::Server
|
|||||||
#
|
#
|
||||||
# case mtype
|
# case mtype
|
||||||
# when .authentication?
|
# when .authentication?
|
||||||
# puts "Receiving an authentication message"
|
# Log.debug "Receiving an authentication message"
|
||||||
# # Test if the client is already authenticated.
|
# # Test if the client is already authenticated.
|
||||||
# if userid
|
# if userid
|
||||||
# user = Context.users_status[userid]
|
# user = Context.users_status[userid]
|
||||||
# raise "Authentication message while the user was already connected: this should not happen"
|
# raise "Authentication message while the user was already connected: this should not happen"
|
||||||
# else
|
# else
|
||||||
# puts "User is not currently connected"
|
# Log.debug "User is not currently connected"
|
||||||
# hdl_authentication event
|
# hdl_authentication event
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# when .upload_request?
|
# when .upload_request?
|
||||||
# puts "Upload request"
|
# Log.debug "Upload request"
|
||||||
# request = FileStorage::UploadRequest.from_json(
|
# request = FileStorage::UploadRequest.from_json(
|
||||||
# String.new event.message.payload
|
# String.new event.message.payload
|
||||||
# )
|
# )
|
||||||
@ -199,7 +209,7 @@ class FileStorage::Service < IPC::Server
|
|||||||
# do_response event, response
|
# do_response event, response
|
||||||
#
|
#
|
||||||
# when .download_request?
|
# when .download_request?
|
||||||
# puts "Download request"
|
# Log.debug "Download request"
|
||||||
# request = FileStorage::DownloadRequest.from_json(
|
# request = FileStorage::DownloadRequest.from_json(
|
||||||
# String.new event.message.payload
|
# String.new event.message.payload
|
||||||
# )
|
# )
|
||||||
@ -232,15 +242,15 @@ class FileStorage::Service < IPC::Server
|
|||||||
send event.fd, response
|
send event.fd, response
|
||||||
|
|
||||||
duration = Time.utc - request_start
|
duration = Time.utc - request_start
|
||||||
puts "request took: #{duration}"
|
Log.debug "request took: #{duration}"
|
||||||
when IPC::Event::MessageSent
|
when IPC::Event::MessageSent
|
||||||
puts "#{CBLUE}IPC::Event::MessageSent#{CRESET}: #{event.fd}"
|
Log.debug "IPC::Event::MessageSent: #{event.fd}"
|
||||||
else
|
else
|
||||||
warning "unhandled IPC event: #{event.class}"
|
Log.warning "unhandled IPC event: #{event.class}"
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue exception
|
rescue exception
|
||||||
error "exception: #{typeof(exception)} - #{exception.message}"
|
Log.error "exception: #{typeof(exception)} - #{exception.message}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -265,6 +275,13 @@ class FileStorage::Service < IPC::Server
|
|||||||
timer = t.to_i
|
timer = t.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
parser.on "-v verbosity",
|
||||||
|
"--verbosity level",
|
||||||
|
"Verbosity level. From 0 to 3. Default: 1" do |v|
|
||||||
|
Context.verbosity = v.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
parser.on "-h",
|
parser.on "-h",
|
||||||
"--help",
|
"--help",
|
||||||
"Displays this help and exits." do
|
"Displays this help and exits." do
|
||||||
|
@ -116,7 +116,7 @@ class FileStorage::Storage
|
|||||||
# In case the file was completely uploaded already.
|
# In case the file was completely uploaded already.
|
||||||
return FileStorage::Errors::FileFullyUploaded.new mid, path
|
return FileStorage::Errors::FileFullyUploaded.new mid, path
|
||||||
rescue e
|
rescue e
|
||||||
puts "error during transfer_info.chunks.sort.first"
|
Log.error "error during transfer_info.chunks.sort.first"
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -128,7 +128,7 @@ class FileStorage::Storage
|
|||||||
digest = transfer_info.file_info.digest
|
digest = transfer_info.file_info.digest
|
||||||
FileStorage::Response::PutChunk.new mid, digest, chunk_number
|
FileStorage::Response::PutChunk.new mid, digest, chunk_number
|
||||||
rescue e
|
rescue e
|
||||||
puts "Error handling write_chunk: #{e.message}"
|
Log.error "Error handling write_chunk: #{e.message}"
|
||||||
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error: #{e.message}"
|
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error: #{e.message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -167,7 +167,7 @@ class FileStorage::Storage
|
|||||||
|
|
||||||
FileStorage::Response::GetChunk.new mid, digest, chunk, b64_encoded_data
|
FileStorage::Response::GetChunk.new mid, digest, chunk, b64_encoded_data
|
||||||
rescue e
|
rescue e
|
||||||
puts "Error handling read_chunk: #{e.message}"
|
Log.error "Error handling read_chunk: #{e.message}"
|
||||||
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error: #{e.message}"
|
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error: #{e.message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ class FileStorage::Storage
|
|||||||
mid = request.mid
|
mid = request.mid
|
||||||
mid ||= "no message id"
|
mid ||= "no message id"
|
||||||
|
|
||||||
puts "hdl upload: mid=#{request.mid}"
|
Log.debug "hdl upload: mid=#{request.mid}"
|
||||||
pp! request
|
pp! request
|
||||||
|
|
||||||
# The final path of the file.
|
# The final path of the file.
|
||||||
@ -190,13 +190,13 @@ class FileStorage::Storage
|
|||||||
# First: check if the file already exists.
|
# First: check if the file already exists.
|
||||||
transfer_info = @db_by_filedigest.get? file_digest
|
transfer_info = @db_by_filedigest.get? file_digest
|
||||||
if transfer_info.nil?
|
if transfer_info.nil?
|
||||||
puts "new file: #{file_digest}"
|
Log.debug "new file: #{file_digest}"
|
||||||
|
|
||||||
# In case file informations aren't already registered
|
# In case file informations aren't already registered
|
||||||
# which is normal at this point.
|
# which is normal at this point.
|
||||||
@db << TransferInfo.new user.uid, request.file
|
@db << TransferInfo.new user.uid, request.file
|
||||||
else
|
else
|
||||||
puts "file already upload (at least partially): #{file_digest}"
|
Log.debug "file already upload (at least partially): #{file_digest}"
|
||||||
# File information already exists, request may be duplicated,
|
# File information already exists, request may be duplicated,
|
||||||
# in this case: ignore the upload request.
|
# in this case: ignore the upload request.
|
||||||
begin
|
begin
|
||||||
@ -208,7 +208,7 @@ class FileStorage::Storage
|
|||||||
# In case the file was completely uploaded already.
|
# In case the file was completely uploaded already.
|
||||||
return FileStorage::Errors::FileFullyUploaded.new mid, path
|
return FileStorage::Errors::FileFullyUploaded.new mid, path
|
||||||
rescue e
|
rescue e
|
||||||
puts "error at transfer_info.chunks.sort.first in upload"
|
Log.error "error at transfer_info.chunks.sort.first in upload"
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -217,7 +217,7 @@ class FileStorage::Storage
|
|||||||
|
|
||||||
FileStorage::Response::Upload.new request.mid, path
|
FileStorage::Response::Upload.new request.mid, path
|
||||||
rescue e
|
rescue e
|
||||||
puts "Error handling upload request: #{e.message}"
|
Log.error "Error handling upload request: #{e.message}"
|
||||||
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error in upload request: #{e.message}"
|
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error in upload request: #{e.message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -226,7 +226,7 @@ class FileStorage::Storage
|
|||||||
|
|
||||||
mid = request.mid
|
mid = request.mid
|
||||||
mid ||= "no message id"
|
mid ||= "no message id"
|
||||||
puts "hdl download: mid=#{mid}"
|
Log.debug "hdl download: mid=#{mid}"
|
||||||
|
|
||||||
unless (file_digest = request.filedigest).nil?
|
unless (file_digest = request.filedigest).nil?
|
||||||
unless (file_transfer = @db_by_filedigest.get? file_digest).nil?
|
unless (file_transfer = @db_by_filedigest.get? file_digest).nil?
|
||||||
@ -248,7 +248,7 @@ class FileStorage::Storage
|
|||||||
# Should have returned by now: file wasn't found.
|
# Should have returned by now: file wasn't found.
|
||||||
FileStorage::Errors::GenericError.new mid, "File not found with provided parameters."
|
FileStorage::Errors::GenericError.new mid, "File not found with provided parameters."
|
||||||
rescue e
|
rescue e
|
||||||
puts "Error handling download request: #{e.message}"
|
Log.error "Error handling download request: #{e.message}"
|
||||||
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error in download request: #{e.message}"
|
FileStorage::Errors::GenericError.new mid.not_nil!, "Unexpected error in download request: #{e.message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -259,7 +259,7 @@ class FileStorage::Storage
|
|||||||
# user : UserData,
|
# user : UserData,
|
||||||
# event : IPC::Event::Message) : Array(FileStorage::Response)
|
# event : IPC::Event::Message) : Array(FileStorage::Response)
|
||||||
#
|
#
|
||||||
# puts "hdl request"
|
# Log.debug "hdl request"
|
||||||
# responses = Array(FileStorage::Response | FileStorage::Errors).new
|
# responses = Array(FileStorage::Response | FileStorage::Errors).new
|
||||||
#
|
#
|
||||||
# requests.each do |request|
|
# requests.each do |request|
|
||||||
@ -272,7 +272,6 @@ class FileStorage::Storage
|
|||||||
# raise "request not understood"
|
# raise "request not understood"
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# puts
|
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# responses
|
# responses
|
||||||
|
Loading…
Reference in New Issue
Block a user