Skip to content

Commit

Permalink
Do not use deprecated Logger class. See crystal-lang/crystal#8847
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Sánchez committed Apr 21, 2020
1 parent 836950d commit c1201c5
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 60 deletions.
12 changes: 3 additions & 9 deletions src/scry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ require "./scry/client"
module Scry
def self.start
client = Client.new(STDOUT)
Log.logger = Log::ClientLogger.new(client)

Log.logger.info("Scry is looking into your code...")
Log.logger.info { "Scry is looking into your code..." }

at_exit do
Log.logger.info("...your session has ended")
Log.logger.info { "...your session has ended" }
end

EnvironmentConfig.run
Expand All @@ -33,11 +31,7 @@ module Scry
end
end
rescue ex
Log.logger.error(
%(#{ex.message || "Unknown error"}\n#{ex.backtrace.join('\n')})
) unless Log.logger.nil?
ensure
Log.logger.close unless Log.logger.nil?
Log.logger.error(exception: ex) { %(#{ex.message || "Unknown error"}\n#{ex.backtrace.join('\n')}) } unless Log.logger.nil?
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/scry/analyzer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module Scry
end
end
rescue ex
Log.logger.error("A error was found while searching diagnostics\n#{ex}\n#{response}")
Log.logger.error { "An error was found while searching diagnostics\n#{ex}\n#{response}" }
nil
end

Expand Down
2 changes: 1 addition & 1 deletion src/scry/completion/dependency_graph.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module Scry::Completion::DependencyGraph
end

def build
Log.logger.debug("Starting to build dependancy graph for these paths: #{@lookup_paths.join("\n")}")
Log.logger.debug { "Starting to build dependancy graph for these paths: #{@lookup_paths.join("\n")}" }
graph = Graph.new
@lookup_paths
.map { |e| File.join(File.expand_path(e), "**", "*.cr") }
Expand Down
2 changes: 1 addition & 1 deletion src/scry/completion/method_db.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Scry::Completion
if res
res
else
Log.logger.debug("Couldn't find type #{e}")
Log.logger.debug { "Couldn't find type #{e}" }
[] of MethodDBEntry
end
end.select(&.name.starts_with? text).to_a
Expand Down
20 changes: 10 additions & 10 deletions src/scry/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ module Scry
# A request message to describe a request between the client and the server.
# Every processed request must send a response back to the sender of the request.
def dispatch(msg : LSP::Protocol::RequestMessage)
Log.logger.debug(msg.method)
Log.logger.debug { msg.method }
dispatch_request(msg.params, msg)
end

def dispatch(msg : LSP::Protocol::NotificationMessage)
Log.logger.debug(msg.method)
Log.logger.debug { msg.method }
dispatch_notification(msg.params, msg)
end

Expand Down Expand Up @@ -64,22 +64,22 @@ module Scry
return ignore_path_response(msg.id, text_document) if text_document.in_memory?
hover = HoverProvider.new(@workspace, text_document)
response = hover.run
Log.logger.debug(response)
Log.logger.debug { response }
response
when "textDocument/definition"
text_document = TextDocument.new(params, msg.id)
return ignore_path_response(msg.id, text_document) if text_document.in_memory?
definitions = Implementations.new(@workspace, text_document)
response = definitions.run
Log.logger.debug(response)
Log.logger.debug { response }
response
when "textDocument/completion"
text_document, method_db = @workspace.get_file(TextDocument.uri_to_filename(params.text_document.uri))
return ignore_path_response(msg.id, text_document) if text_document.in_memory?
completion = CompletionProvider.new(text_document, params.context, params.position, method_db)
results = completion.run
response = LSP::Protocol::ResponseMessage.new(msg.id, results)
Log.logger.debug(response)
Log.logger.debug { response }
response
else
raise UnrecognizedProcedureError.new("Didn't recognize procedure: #{msg.method}")
Expand All @@ -95,7 +95,7 @@ module Scry

formatter = Formatter.new(@workspace, text_document)
response = formatter.run
Log.logger.debug(response)
Log.logger.debug { response }
response
end

Expand All @@ -107,7 +107,7 @@ module Scry
symbol_processor = SymbolProcessor.new(text_document)
symbols = symbol_processor.run
response = LSP::Protocol::ResponseMessage.new(msg.id, symbols)
Log.logger.debug(response)
Log.logger.debug { response }
response
end
end
Expand All @@ -118,7 +118,7 @@ module Scry
workspace_symbol_processor = WorkspaceSymbolProcessor.new(@workspace.root_uri, params.query)
symbols = workspace_symbol_processor.run
response = LSP::Protocol::ResponseMessage.new(msg.id, symbols)
Log.logger.debug(response)
Log.logger.debug { response }
response
end
end
Expand All @@ -129,7 +129,7 @@ module Scry
resolver = CompletionResolver.new(msg.id, params)
results = resolver.run
response = LSP::Protocol::ResponseMessage.new(msg.id, results)
Log.logger.debug(response)
Log.logger.debug { response }
response
end
end
Expand Down Expand Up @@ -200,7 +200,7 @@ module Scry
end

private def ignore_path_response(msg_id : Int32?, text_document : TextDocument) : LSP::Protocol::ResponseMessage?
Log.logger.debug("Ignoring path: #{text_document.filename}")
Log.logger.debug { "Ignoring path: #{text_document.filename}" }
if msg_id
LSP::Protocol::ResponseMessage.new(msg_id, nil)
else # Notification messages don't require a response
Expand Down
2 changes: 1 addition & 1 deletion src/scry/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Scry
LSP::Protocol::ResponseMessage.new(@text_document.id, [LSP::Protocol::TextEdit.new(range, result)])
end
rescue ex
Log.logger.error("A error was found while formatting\n#{ex}\n#{result}")
Log.logger.error { "A error was found while formatting\n#{ex}\n#{result}" }
nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/scry/hover_provider.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module Scry
end
end
rescue ex
Log.logger.error("A error was found while searching contexts\n#{ex}\n#{result}")
Log.logger.error { "A error was found while searching contexts\n#{ex}\n#{result}" }
hover_response
end

Expand Down
4 changes: 2 additions & 2 deletions src/scry/implementations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module Scry

private def analyze(filename, position, scope)
result = crystal_tool(filename, position, scope)
Log.logger.debug("result: #{result}")
Log.logger.debug { "result: #{result}" }
response = (Array(BuildFailure) | ImplementationsResponse).from_json(result)
case response
when Array(BuildFailure)
Expand All @@ -81,7 +81,7 @@ module Scry
end
end
rescue ex
Log.logger.error("A error was found while searching implementations\n#{ex}\n#{result}")
Log.logger.error { "A error was found while searching implementations\n#{ex}\n#{result}" }
implementation_response
end

Expand Down
25 changes: 3 additions & 22 deletions src/scry/log.cr
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
require "logger"
require "log"
require "log/io_backend"
require "./client"

module Scry
module Log
class_property logger : Logger = Logger.new(nil)

class ClientLogger < Logger
def initialize(@client : Client)
super(@client.io)
end

private def write(severity, datetime, progname, message)
message_type = case severity
when INFO
LSP::Protocol::MessageType::Info
when WARN
LSP::Protocol::MessageType::Warning
when ERROR, FATAL
LSP::Protocol::MessageType::Error
else
LSP::Protocol::MessageType::Log
end
@client.send("window/logMessage", LSP::Protocol::LogMessageParams.new(message_type, message.to_s))
end
end
class_property logger : ::Log = ::Log.for(self)
end
end
8 changes: 4 additions & 4 deletions src/scry/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ module Scry

private def read_header
raw_header = @io.gets
Log.logger.debug(raw_header)
Log.logger.debug { raw_header }

if raw_header.nil?
if Scry.shutdown
Log.logger.info("Server has shut down, no more request are accepted")
Log.logger.info { "Server has shut down, no more request are accepted" }
exit(0)
else
Log.logger.warn("Connection with client lost")
Log.logger.warn { "Connection with client lost" }
nil
end
else
Expand All @@ -42,7 +42,7 @@ module Scry

private def read_content
@content = @io.gets(content_length)
Log.logger.debug(@content)
Log.logger.debug { @content }
@content
end

Expand Down
4 changes: 2 additions & 2 deletions src/scry/text_document.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ module Scry
private def read_file : String
File.read(filename)
rescue ex : IO::Error
Log.logger.warn(ex.message)
Log.logger.warn(exception: ex) { ex.message }
""
rescue ex : RuntimeError
Log.logger.warn(ex.message)
Log.logger.warn(exception: ex) { ex.message }
""
end
end
Expand Down
13 changes: 7 additions & 6 deletions src/scry/update_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ require "./settings"
module Scry
struct UpdateConfig
LOG_LEVELS = {
"debug" => Logger::DEBUG,
"info" => Logger::INFO,
"warn" => Logger::WARN,
"error" => Logger::ERROR,
"fatal" => Logger::FATAL,
"debug" => ::Log::Severity::Debug,
"info" => ::Log::Severity::Info,
"warn" => ::Log::Severity::Warning,
"warning" => ::Log::Severity::Warning,
"error" => ::Log::Severity::Error,
"fatal" => ::Log::Severity::Fatal,
}

def initialize(@workspace : Workspace, @settings : LSP::Protocol::DidChangeConfigurationParams)
Expand All @@ -22,7 +23,7 @@ module Scry
def run
@workspace.max_number_of_problems = customizations.max_number_of_problems
Log.logger.level = log_level(customizations.log_level || "error")
Log.logger.info("Scry Configuration Updated:\n #{@settings.to_json}")
Log.logger.info { "Scry Configuration Updated:\n #{@settings.to_json}" }
{@workspace, nil}
end

Expand Down

0 comments on commit c1201c5

Please sign in to comment.