Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change log statements to crystal stdlib logging #6

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/rosegold.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require "./rosegold/client"
module Rosegold
VERSION = "0.1.0"

Log.setup_from_env

# TODO: Put your code here
end

Expand Down
10 changes: 1 addition & 9 deletions src/rosegold/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ class Rosegold::Client
end
end

def log_info(&build_msg : -> String)
STDERR.puts build_msg.call
end

def log_debug(&build_msg : -> String)
# STDERR.puts build_msg.call
end

def compress?
compression_threshold.positive?
end
Expand Down Expand Up @@ -113,7 +105,7 @@ class Rosegold::Client
Minecraft::IO::Memory.new(pkt_bytes).try do |pkt_io|
pkt_type = current_state[pkt_io.read_var_int]
if pkt_type
log_debug { "rx <- #{pkt_type}".gsub "Rosegold::Clientbound::", "" }
Log.debug { "rx <- #{pkt_type}".gsub "Rosegold::Clientbound::", "" }
pkt_type.read(pkt_io).tap &.callback(self)
else
nil # packet not parsed
Expand Down
4 changes: 3 additions & 1 deletion src/rosegold/packets/clientbound/chat.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Rosegold::Clientbound::Chat < Rosegold::Clientbound::Packet
Log = ::Log.for(self)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this gets its own log instance but the other packets don't? Just curious.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you added the [Chat] prefix, this gives the chat a prefix based on the class so we know it's chat (and I deleted [Chat])

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll also be able to add formatters for specific ones if we chose, but this was the only one that needed it's own instance for now based on your existing messages


property \
message : JSON::Any,
position : UInt8,
Expand All @@ -16,6 +18,6 @@ class Rosegold::Clientbound::Chat < Rosegold::Clientbound::Packet
end

def callback(client)
client.log_info { "[Chat] #{message}" }
Log.info { message }
end
end
2 changes: 1 addition & 1 deletion src/rosegold/packets/clientbound/disconnect.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Rosegold::Clientbound::Disconnect < Rosegold::Clientbound::Packet
end

def callback(client)
client.log_info { "Disconnected: #{reason}" }
Log.info { "Disconnected: #{reason}" }
exit 0
end
end
2 changes: 1 addition & 1 deletion src/rosegold/packets/clientbound/join_game.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class Rosegold::Clientbound::JoinGame < Rosegold::Clientbound::Packet
end

def callback(client)
client.log_debug { "Ingame. gamemode=#{gamemode} entity_id=#{entity_id}" }
Log.debug { "Ingame. gamemode=#{gamemode} entity_id=#{entity_id}" }
end
end
2 changes: 1 addition & 1 deletion src/rosegold/packets/clientbound/login_success.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class Rosegold::Clientbound::LoginSuccess < Rosegold::Clientbound::Packet

def callback(client)
client.state = State::Play.new
client.log_info { "Logged in as #{username} #{uuid}" }
Log.info { "Logged in as #{username} #{uuid}" }
end
end
2 changes: 1 addition & 1 deletion src/rosegold/packets/clientbound/update_health.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Rosegold::Clientbound::UpdateHealth < Rosegold::Clientbound::Packet
end

def callback(client)
client.log_debug { "health=#{health/2}❤ food=#{food*5}% saturation=#{saturation}" }
Log.debug { "health=#{health/2}❤ food=#{food*5}% saturation=#{saturation}" }
# TODO: update health/food/saturation
# TODO: check death
end
Expand Down