Baguette::Log.
This commit is contained in:
parent
0b8fea6689
commit
2f649b6b15
@ -15,8 +15,8 @@ dependencies:
|
|||||||
authd:
|
authd:
|
||||||
git: https://git.baguette.netlib.re/Baguette/authd.git
|
git: https://git.baguette.netlib.re/Baguette/authd.git
|
||||||
branch: master
|
branch: master
|
||||||
weird-crystal-base:
|
baguette-crystal-base:
|
||||||
git: https://git.baguette.netlib.re/Baguette/weird-crystal-base
|
git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base
|
||||||
branch: master
|
branch: master
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ class FileStorage::Request
|
|||||||
|
|
||||||
user_data = filestoraged.get_user_data user.uid
|
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
|
filestoraged.storage.write_chunk self, user_data
|
||||||
rescue e
|
rescue e
|
||||||
|
@ -4,7 +4,7 @@ require "authd"
|
|||||||
|
|
||||||
require "colorize"
|
require "colorize"
|
||||||
|
|
||||||
require "weird-crystal-base"
|
require "baguette-crystal-base"
|
||||||
|
|
||||||
require "../common/colors"
|
require "../common/colors"
|
||||||
# require "../common/filestorage.cr"
|
# require "../common/filestorage.cr"
|
||||||
@ -29,25 +29,6 @@ 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).
|
||||||
@ -111,21 +92,21 @@ class FileStorage::Service < IPC::Server
|
|||||||
#end
|
#end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
Log.info "Starting filestoraged"
|
Baguette::Log.title "Starting filestoraged"
|
||||||
|
|
||||||
self.loop do |event|
|
self.loop do |event|
|
||||||
begin
|
begin
|
||||||
|
|
||||||
case event
|
case event
|
||||||
when IPC::Event::Timer
|
when IPC::Event::Timer
|
||||||
Log.debug "IPC::Event::Timer"
|
Baguette::Log.debug "IPC::Event::Timer"
|
||||||
|
|
||||||
when IPC::Event::Connection
|
when IPC::Event::Connection
|
||||||
Log.debug "IPC::Event::Connection: #{event.fd}"
|
Baguette::Log.debug "IPC::Event::Connection: #{event.fd}"
|
||||||
@all_connections << event.fd
|
@all_connections << event.fd
|
||||||
|
|
||||||
when IPC::Event::Disconnection
|
when IPC::Event::Disconnection
|
||||||
Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
Baguette::Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
||||||
fd = event.fd
|
fd = event.fd
|
||||||
|
|
||||||
@logged_users.delete fd
|
@logged_users.delete fd
|
||||||
@ -136,15 +117,15 @@ class FileStorage::Service < IPC::Server
|
|||||||
end
|
end
|
||||||
|
|
||||||
when IPC::Event::ExtraSocket
|
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
|
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
|
# 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
|
||||||
Log.debug "IPC::Event::Message: #{event.fd}"
|
Baguette::Log.debug "IPC::Event::Message: #{event.fd}"
|
||||||
|
|
||||||
request_start = Time.utc
|
request_start = Time.utc
|
||||||
|
|
||||||
@ -154,15 +135,15 @@ class FileStorage::Service < IPC::Server
|
|||||||
raise "unknown request type"
|
raise "unknown request type"
|
||||||
end
|
end
|
||||||
|
|
||||||
Log.info "<< #{request.class.name.sub /^FileStorage::Request::/, ""}"
|
Baguette::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)
|
||||||
Log.warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})"
|
Baguette::Log.warning ">> #{response_type.sub /^FileStorage::Errors::/, ""} (#{response.reason})"
|
||||||
else
|
else
|
||||||
Log.info ">> #{response_type.sub /^FileStorage::Response::/, ""}"
|
Baguette::Log.info ">> #{response_type.sub /^FileStorage::Response::/, ""}"
|
||||||
end
|
end
|
||||||
|
|
||||||
#################################################################
|
#################################################################
|
||||||
@ -190,18 +171,18 @@ class FileStorage::Service < IPC::Server
|
|||||||
#
|
#
|
||||||
# case mtype
|
# case mtype
|
||||||
# when .authentication?
|
# when .authentication?
|
||||||
# Log.debug "Receiving an authentication message"
|
# Baguette::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
|
||||||
# Log.debug "User is not currently connected"
|
# Baguette::Log.debug "User is not currently connected"
|
||||||
# hdl_authentication event
|
# hdl_authentication event
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# when .upload_request?
|
# when .upload_request?
|
||||||
# Log.debug "Upload request"
|
# Baguette::Log.debug "Upload request"
|
||||||
# request = FileStorage::UploadRequest.from_json(
|
# request = FileStorage::UploadRequest.from_json(
|
||||||
# String.new event.message.payload
|
# String.new event.message.payload
|
||||||
# )
|
# )
|
||||||
@ -209,7 +190,7 @@ class FileStorage::Service < IPC::Server
|
|||||||
# do_response event, response
|
# do_response event, response
|
||||||
#
|
#
|
||||||
# when .download_request?
|
# when .download_request?
|
||||||
# Log.debug "Download request"
|
# Baguette::Log.debug "Download request"
|
||||||
# request = FileStorage::DownloadRequest.from_json(
|
# request = FileStorage::DownloadRequest.from_json(
|
||||||
# String.new event.message.payload
|
# String.new event.message.payload
|
||||||
# )
|
# )
|
||||||
@ -242,15 +223,15 @@ class FileStorage::Service < IPC::Server
|
|||||||
send event.fd, response
|
send event.fd, response
|
||||||
|
|
||||||
duration = Time.utc - request_start
|
duration = Time.utc - request_start
|
||||||
Log.debug "request took: #{duration}"
|
Baguette::Log.debug "request took: #{duration}"
|
||||||
when IPC::Event::MessageSent
|
when IPC::Event::MessageSent
|
||||||
Log.debug "IPC::Event::MessageSent: #{event.fd}"
|
Baguette::Log.debug "IPC::Event::MessageSent: #{event.fd}"
|
||||||
else
|
else
|
||||||
Log.warning "unhandled IPC event: #{event.class}"
|
Baguette::Log.warning "unhandled IPC event: #{event.class}"
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue exception
|
rescue exception
|
||||||
Log.error "exception: #{typeof(exception)} - #{exception.message}"
|
Baguette::Log.error "exception: #{typeof(exception)} - #{exception.message}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -278,7 +259,7 @@ class FileStorage::Service < IPC::Server
|
|||||||
parser.on "-v verbosity",
|
parser.on "-v verbosity",
|
||||||
"--verbosity level",
|
"--verbosity level",
|
||||||
"Verbosity level. From 0 to 3. Default: 1" do |v|
|
"Verbosity level. From 0 to 3. Default: 1" do |v|
|
||||||
Context.verbosity = v.to_i
|
Baguette::Context.verbosity = v.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
Log.error "error during transfer_info.chunks.sort.first"
|
Baguette::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
|
||||||
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}"
|
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
|
||||||
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}"
|
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"
|
||||||
|
|
||||||
Log.debug "hdl upload: mid=#{request.mid}"
|
Baguette::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?
|
||||||
Log.debug "new file: #{file_digest}"
|
Baguette::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
|
||||||
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,
|
# 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
|
||||||
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
|
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
|
||||||
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}"
|
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"
|
||||||
Log.debug "hdl download: mid=#{mid}"
|
Baguette::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
|
||||||
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}"
|
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)
|
||||||
#
|
#
|
||||||
# Log.debug "hdl request"
|
# Baguette::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|
|
||||||
|
Loading…
Reference in New Issue
Block a user