From 0b8fea6689de082e068957d93b6390ab77db1951 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Mon, 24 Aug 2020 17:15:00 +0200 Subject: [PATCH] New logging system. --- shard.yml | 8 ++-- src/common/requests/transfer.cr | 2 +- src/server/main.cr | 73 ++++++++++++++++++++------------- src/server/storage.cr | 23 +++++------ 4 files changed, 61 insertions(+), 45 deletions(-) diff --git a/shard.yml b/shard.yml index 6786934..b15a045 100644 --- a/shard.yml +++ b/shard.yml @@ -21,13 +21,13 @@ dependencies: targets: - common-tests: - main: src/tests/common-tests.cr - json-tests: - main: src/tests/json_tests.cr filestoraged: main: src/server/main.cr client: main: src/client/main.cr + common-tests: + main: src/tests/common-tests.cr + json-tests: + main: src/tests/json_tests.cr license: MIT diff --git a/src/common/requests/transfer.cr b/src/common/requests/transfer.cr index 81df8ff..810949a 100644 --- a/src/common/requests/transfer.cr +++ b/src/common/requests/transfer.cr @@ -27,7 +27,7 @@ class FileStorage::Request 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 rescue e diff --git a/src/server/main.cr b/src/server/main.cr index f0d49ae..ee2f191 100644 --- a/src/server/main.cr +++ b/src/server/main.cr @@ -29,6 +29,26 @@ require "./network.cr" 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 # List of connected users (fd => uid). property connected_users = Hash(Int32, Int32).new @@ -65,16 +85,6 @@ class FileStorage::Service < IPC::Server @logged_users[fd]? 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) @auth.decode_token token end @@ -101,21 +111,21 @@ class FileStorage::Service < IPC::Server #end def run - info "Starting filestoraged" + Log.info "Starting filestoraged" self.loop do |event| begin case event when IPC::Event::Timer - puts "#{CORANGE}IPC::Event::Timer#{CRESET}" + Log.debug "IPC::Event::Timer" when IPC::Event::Connection - puts "#{CBLUE}IPC::Event::Connection: #{event.fd}#{CRESET}" + Log.debug "IPC::Event::Connection: #{event.fd}" @all_connections << event.fd when IPC::Event::Disconnection - puts "#{CBLUE}IPC::Event::Disconnection: #{event.fd}#{CRESET}" + Log.debug "IPC::Event::Disconnection: #{event.fd}" fd = event.fd @logged_users.delete fd @@ -126,15 +136,15 @@ class FileStorage::Service < IPC::Server end 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 - 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 # because ExtraSocket and Switch inherit from Message class when IPC::Event::MessageReceived - puts "#{CBLUE}IPC::Event::Message#{CRESET}: #{event.fd}" + Log.debug "IPC::Event::Message: #{event.fd}" request_start = Time.utc @@ -144,15 +154,15 @@ class FileStorage::Service < IPC::Server raise "unknown request type" end - info "<< #{request.class.name.sub /^FileStorage::Request::/, ""}" + Log.info "<< #{request.class.name.sub /^FileStorage::Request::/, ""}" response = request.handle self, event response_type = response.class.name if response.responds_to?(:reason) - warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})" + Log.warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})" else - info ">> #{response_type.sub /^FileStorage::Response::/, ""}" + Log.info ">> #{response_type.sub /^FileStorage::Response::/, ""}" end ################################################################# @@ -180,18 +190,18 @@ class FileStorage::Service < IPC::Server # # case mtype # when .authentication? -# puts "Receiving an authentication message" +# Log.debug "Receiving an authentication message" # # Test if the client is already authenticated. # if userid # user = Context.users_status[userid] # raise "Authentication message while the user was already connected: this should not happen" # else -# puts "User is not currently connected" +# Log.debug "User is not currently connected" # hdl_authentication event # end # # when .upload_request? -# puts "Upload request" +# Log.debug "Upload request" # request = FileStorage::UploadRequest.from_json( # String.new event.message.payload # ) @@ -199,7 +209,7 @@ class FileStorage::Service < IPC::Server # do_response event, response # # when .download_request? -# puts "Download request" +# Log.debug "Download request" # request = FileStorage::DownloadRequest.from_json( # String.new event.message.payload # ) @@ -232,15 +242,15 @@ class FileStorage::Service < IPC::Server send event.fd, response duration = Time.utc - request_start - puts "request took: #{duration}" + Log.debug "request took: #{duration}" when IPC::Event::MessageSent - puts "#{CBLUE}IPC::Event::MessageSent#{CRESET}: #{event.fd}" + Log.debug "IPC::Event::MessageSent: #{event.fd}" else - warning "unhandled IPC event: #{event.class}" + Log.warning "unhandled IPC event: #{event.class}" end rescue exception - error "exception: #{typeof(exception)} - #{exception.message}" + Log.error "exception: #{typeof(exception)} - #{exception.message}" end end end @@ -265,6 +275,13 @@ class FileStorage::Service < IPC::Server timer = t.to_i 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", "--help", "Displays this help and exits." do diff --git a/src/server/storage.cr b/src/server/storage.cr index 86a4780..9a0327e 100644 --- a/src/server/storage.cr +++ b/src/server/storage.cr @@ -116,7 +116,7 @@ class FileStorage::Storage # In case the file was completely uploaded already. return FileStorage::Errors::FileFullyUploaded.new mid, path rescue e - puts "error during transfer_info.chunks.sort.first" + Log.error "error during transfer_info.chunks.sort.first" raise e end end @@ -128,7 +128,7 @@ class FileStorage::Storage digest = transfer_info.file_info.digest FileStorage::Response::PutChunk.new mid, digest, chunk_number 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}" end @@ -167,7 +167,7 @@ class FileStorage::Storage FileStorage::Response::GetChunk.new mid, digest, chunk, b64_encoded_data 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}" end @@ -177,7 +177,7 @@ class FileStorage::Storage mid = request.mid mid ||= "no message id" - puts "hdl upload: mid=#{request.mid}" + Log.debug "hdl upload: mid=#{request.mid}" pp! request # The final path of the file. @@ -190,13 +190,13 @@ class FileStorage::Storage # First: check if the file already exists. transfer_info = @db_by_filedigest.get? file_digest if transfer_info.nil? - puts "new file: #{file_digest}" + Log.debug "new file: #{file_digest}" # In case file informations aren't already registered # which is normal at this point. @db << TransferInfo.new user.uid, request.file 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, # in this case: ignore the upload request. begin @@ -208,7 +208,7 @@ class FileStorage::Storage # In case the file was completely uploaded already. return FileStorage::Errors::FileFullyUploaded.new mid, path 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 end end @@ -217,7 +217,7 @@ class FileStorage::Storage FileStorage::Response::Upload.new request.mid, path 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}" end @@ -226,7 +226,7 @@ class FileStorage::Storage mid = request.mid mid ||= "no message id" - puts "hdl download: mid=#{mid}" + Log.debug "hdl download: mid=#{mid}" unless (file_digest = request.filedigest).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. FileStorage::Errors::GenericError.new mid, "File not found with provided parameters." 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}" end @@ -259,7 +259,7 @@ class FileStorage::Storage # user : UserData, # event : IPC::Event::Message) : Array(FileStorage::Response) # -# puts "hdl request" +# Log.debug "hdl request" # responses = Array(FileStorage::Response | FileStorage::Errors).new # # requests.each do |request| @@ -272,7 +272,6 @@ class FileStorage::Storage # raise "request not understood" # end # -# puts # end # # responses