Baguette::Log.

This commit is contained in:
Karchnu 2020-08-28 01:53:52 +02:00
parent 0b8fea6689
commit 2f649b6b15
4 changed files with 34 additions and 53 deletions

View File

@ -15,8 +15,8 @@ dependencies:
authd:
git: https://git.baguette.netlib.re/Baguette/authd.git
branch: master
weird-crystal-base:
git: https://git.baguette.netlib.re/Baguette/weird-crystal-base
baguette-crystal-base:
git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base
branch: master

View File

@ -27,7 +27,7 @@ class FileStorage::Request
user_data = filestoraged.get_user_data user.uid
Log.info "PutChunk request: #{@mid}, file #{@filedigest}, chunk: n=#{@chunk.n}, on=#{@chunk.on}, digest=#{@chunk.digest}"
Baguette::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

View File

@ -4,7 +4,7 @@ require "authd"
require "colorize"
require "weird-crystal-base"
require "baguette-crystal-base"
require "../common/colors"
# require "../common/filestorage.cr"
@ -29,25 +29,6 @@ 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).
@ -111,21 +92,21 @@ class FileStorage::Service < IPC::Server
#end
def run
Log.info "Starting filestoraged"
Baguette::Log.title "Starting filestoraged"
self.loop do |event|
begin
case event
when IPC::Event::Timer
Log.debug "IPC::Event::Timer"
Baguette::Log.debug "IPC::Event::Timer"
when IPC::Event::Connection
Log.debug "IPC::Event::Connection: #{event.fd}"
Baguette::Log.debug "IPC::Event::Connection: #{event.fd}"
@all_connections << event.fd
when IPC::Event::Disconnection
Log.debug "IPC::Event::Disconnection: #{event.fd}"
Baguette::Log.debug "IPC::Event::Disconnection: #{event.fd}"
fd = event.fd
@logged_users.delete fd
@ -136,15 +117,15 @@ class FileStorage::Service < IPC::Server
end
when IPC::Event::ExtraSocket
Log.warning "IPC::Event::ExtraSocket: should not happen in this service"
Baguette::Log.warning "IPC::Event::ExtraSocket: should not happen in this service"
when IPC::Event::Switch
Log.warning "IPC::Event::Switch: should not happen in this service"
Baguette::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
Log.debug "IPC::Event::Message: #{event.fd}"
Baguette::Log.debug "IPC::Event::Message: #{event.fd}"
request_start = Time.utc
@ -154,15 +135,15 @@ class FileStorage::Service < IPC::Server
raise "unknown request type"
end
Log.info "<< #{request.class.name.sub /^FileStorage::Request::/, ""}"
Baguette::Log.info "<< #{request.class.name.sub /^FileStorage::Request::/, ""}"
response = request.handle self, event
response_type = response.class.name
if response.responds_to?(:reason)
Log.warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})"
Baguette::Log.warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})"
else
Log.info ">> #{response_type.sub /^FileStorage::Response::/, ""}"
Baguette::Log.info ">> #{response_type.sub /^FileStorage::Response::/, ""}"
end
#################################################################
@ -190,18 +171,18 @@ class FileStorage::Service < IPC::Server
#
# case mtype
# when .authentication?
# Log.debug "Receiving an authentication message"
# Baguette::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
# Log.debug "User is not currently connected"
# Baguette::Log.debug "User is not currently connected"
# hdl_authentication event
# end
#
# when .upload_request?
# Log.debug "Upload request"
# Baguette::Log.debug "Upload request"
# request = FileStorage::UploadRequest.from_json(
# String.new event.message.payload
# )
@ -209,7 +190,7 @@ class FileStorage::Service < IPC::Server
# do_response event, response
#
# when .download_request?
# Log.debug "Download request"
# Baguette::Log.debug "Download request"
# request = FileStorage::DownloadRequest.from_json(
# String.new event.message.payload
# )
@ -242,15 +223,15 @@ class FileStorage::Service < IPC::Server
send event.fd, response
duration = Time.utc - request_start
Log.debug "request took: #{duration}"
Baguette::Log.debug "request took: #{duration}"
when IPC::Event::MessageSent
Log.debug "IPC::Event::MessageSent: #{event.fd}"
Baguette::Log.debug "IPC::Event::MessageSent: #{event.fd}"
else
Log.warning "unhandled IPC event: #{event.class}"
Baguette::Log.warning "unhandled IPC event: #{event.class}"
end
rescue exception
Log.error "exception: #{typeof(exception)} - #{exception.message}"
Baguette::Log.error "exception: #{typeof(exception)} - #{exception.message}"
end
end
end
@ -278,7 +259,7 @@ class FileStorage::Service < IPC::Server
parser.on "-v verbosity",
"--verbosity level",
"Verbosity level. From 0 to 3. Default: 1" do |v|
Context.verbosity = v.to_i
Baguette::Context.verbosity = v.to_i
end

View File

@ -116,7 +116,7 @@ class FileStorage::Storage
# In case the file was completely uploaded already.
return FileStorage::Errors::FileFullyUploaded.new mid, path
rescue e
Log.error "error during transfer_info.chunks.sort.first"
Baguette::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
Log.error "Error handling write_chunk: #{e.message}"
Baguette::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
Log.error "Error handling read_chunk: #{e.message}"
Baguette::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"
Log.debug "hdl upload: mid=#{request.mid}"
Baguette::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?
Log.debug "new file: #{file_digest}"
Baguette::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
Log.debug "file already upload (at least partially): #{file_digest}"
Baguette::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
Log.error "error at transfer_info.chunks.sort.first in upload"
Baguette::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
Log.error "Error handling upload request: #{e.message}"
Baguette::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"
Log.debug "hdl download: mid=#{mid}"
Baguette::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
Log.error "Error handling download request: #{e.message}"
Baguette::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)
#
# Log.debug "hdl request"
# Baguette::Log.debug "hdl request"
# responses = Array(FileStorage::Response | FileStorage::Errors).new
#
# requests.each do |request|