Skip to content

Commit

Permalink
Merge pull request #12 from tilfin/fix/socket-gethostname
Browse files Browse the repository at this point in the history
Fix to get hostname that contains UTF-8 chars
  • Loading branch information
tilfin committed May 17, 2017
2 parents b0b501a + 5ef6fc5 commit 3055b5e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/ougai/formatters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ module Ougai
module Formatters
class Base < Logger::Formatter
attr_accessor :trace_indent, :trace_max_lines
attr_reader :app_name, :hostname

def initialize(app_name = nil, hostname = nil)
@app_name = app_name || File.basename($0, ".rb")
@hostname = hostname || Socket.gethostname
@hostname = hostname || Socket.gethostname.force_encoding('UTF-8')
@trace_indent = 2
@trace_max_lines = 100
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ougai/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ougai
VERSION = "0.7.3"
VERSION = "0.7.4"
end
49 changes: 49 additions & 0 deletions spec/formatters/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'spec_helper'

describe Ougai::Formatters::Base do
subject { described_class.new(app_name, hostname) }

context 'without arguments and hostname contains a UTF-8 char' do
let (:app_name) { nil }
let (:hostname) { nil }

it 'has default app_name and default hostname' do
myhostname = "Taro\xE2\x80\x99s-MacBook".force_encoding('ASCII-8BIT')
allow(Socket).to receive(:gethostname).and_return(myhostname)
expect(subject.app_name).to eq('rspec')
expect(subject.hostname).to eq("Taro’s-MacBook")
end
end

context 'with app_name' do
let (:app_name) { 'myapp' }
let (:hostname) { nil }

it 'has specified app_name and default hostname' do
myhostname = "Hanako's PC".encode('ASCII-8BIT')
allow(Socket).to receive(:gethostname).and_return(myhostname)
expect(subject.app_name).to eq('myapp')
expect(subject.hostname).to eq("Hanako's PC")
end
end

context 'with hostname' do
let (:app_name) { nil }
let (:hostname) { 'myhost' }

it 'has default app_name and specified hostname' do
expect(subject.app_name).to eq('rspec')
expect(subject.hostname).to eq('myhost')
end
end

context 'with app_name and hostname' do
let (:app_name) { 'myapp' }
let (:hostname) { 'myhost' }

it 'has specified app_name and specified hostname' do
expect(subject.app_name).to eq('myapp')
expect(subject.hostname).to eq('myhost')
end
end
end

0 comments on commit 3055b5e

Please sign in to comment.