Skip to content

Commit

Permalink
Update logger (#39)
Browse files Browse the repository at this point in the history
- Switch to using relaton-logger to log messages
  • Loading branch information
andrew2net authored Jul 3, 2024
1 parent 950b91f commit 9c325d3
Show file tree
Hide file tree
Showing 28 changed files with 2,215 additions and 3,023 deletions.
17 changes: 5 additions & 12 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,13 @@ Or install it yourself as:

== Usage

=== Configuration

Configuration is optional. The available option is `logger` which is a `Logger` instance. By default, the logger is `Logger.new($stderr)` with `Logger::WARN` level. To change the logger level, use `RelatonIeee.configure` block.
=== Search for a standard using keywords

[source,ruby]
----
require 'relaton_ieee'
=> true
RelatonIeee.configure do |config|
config.logger.level = Logger::DEBUG
end
----

=== Search for a standard using keywords

[source,ruby]
----
item = RelatonIeee::IeeeBibliography.search("IEEE 528-2019")
=> #<RelatonIeee::IeeeBibliographicItem:0x00007fe12ebc8cb8
...
Expand Down Expand Up @@ -139,6 +128,10 @@ Done in: 143 sec.
=> nil
----

=== Logging

RelatonIeee uses the relaton-logger gem for logging. By default, it logs to STDOUT. To change the log levels and add other loggers, read the https://github.com/relaton/relaton-logger#usage[relaton-logger] documentation.

== Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
3 changes: 3 additions & 0 deletions grammars/basicdoc.rng
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@
<data type="ID"/>
</attribute>
<attribute name="reviewer"/>
<optional>
<attribute name="type"/>
</optional>
<optional>
<attribute name="date">
<data type="dateTime"/>
Expand Down
1 change: 0 additions & 1 deletion lib/relaton_ieee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require "relaton/index"
require "relaton_bib"
require "relaton_ieee/version"
require "relaton_ieee/config"
require "relaton_ieee/util"
require "relaton_ieee/document_type"
require "relaton_ieee/document_status"
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_ieee/balloting_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BallotingGroup
#
def initialize(type:, content:)
unless TYPES.include?(type)
Util.warn "WARNING: type of Balloting group must be one of `#{TYPES.join('`, `')}`"
Util.warn "type of Balloting group must be one of `#{TYPES.join('`, `')}`"
end

@type = type
Expand Down
10 changes: 0 additions & 10 deletions lib/relaton_ieee/config.rb

This file was deleted.

14 changes: 6 additions & 8 deletions lib/relaton_ieee/data_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def fetch # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
end
fetch_doc xml, f
rescue StandardError => e
warn "File: #{f}"
warn e.message
warn e.backtrace
Util.error "File: #{f}\n#{e.message}\n#{e.backtrace}"
end
# File.write "normtitles.txt", @normtitles.join("\n")
update_relations
Expand Down Expand Up @@ -92,7 +90,7 @@ def read_zip(file)
def fetch_doc(xml, filename) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
doc = Nokogiri::XML(xml).at("/publication")
unless doc
warn "Empty file: #{filename}"
Util.warn "Empty file: `#{filename}`"
return
end
stdid = doc.at("./publicationinfo/standard_id")&.text
Expand All @@ -102,14 +100,14 @@ def fetch_doc(xml, filename) # rubocop:disable Metrics/AbcSize,Metrics/MethodLen
bib = fetcher.parse
if bib.docnumber.nil?
nt = doc&.at("./normtitle")&.text
warn "PubID parse error. Normtitle: #{nt}, file: #{filename}"
Util.warn "PubID parse error. Normtitle: `#{nt}`, file: `#{filename}`"
return
end
amsid = doc.at("./publicationinfo/amsid").text
if backrefs.value?(bib.docidentifier[0].id) && /updates\.\d+/ !~ filename
oamsid = backrefs.key bib.docidentifier[0].id
warn "Document exists ID: \"#{bib.docidentifier[0].id}\" AMSID: " \
"\"#{amsid}\" source: \"#{filename}\". Other AMSID: \"#{oamsid}\""
Util.warn "Document exists ID: `#{bib.docidentifier[0].id}` AMSID: " \
"`#{amsid}` source: `#{filename}`. Other AMSID: `#{oamsid}`"
if bib.docidentifier[0].id.include?(doc.at("./publicationinfo/stdnumber").text)
save_doc bib # rewrite file if the PubID matches to the stdnumber
backrefs[amsid] = bib.docidentifier[0].id
Expand Down Expand Up @@ -177,7 +175,7 @@ def update_relations # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
save_doc bib
end
else
warn "Unresolved relation: '#{rf[:amsid]}' type: '#{rf[:type]}' for '#{dnum}'"
Util.warn "Unresolved relation: '#{rf[:amsid]}' type: '#{rf[:type]}' for '#{dnum}'"
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/relaton_ieee/document_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def initialize(type:, abbreviation: nil)

def check_type(type)
unless DOCTYPES.include? type
Util.warn "Invalid doctype: `#{type}`. " \
"It should be one of: `#{DOCTYPES.join('`, `')}`."
Util.warn "Invalid doctype: `#{type}`. It should be one of: `#{DOCTYPES.join('`, `')}`."
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/relaton_ieee/ieee_bibliography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def search(code) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# if document is found else returns NilClass
#
def get(code, _year = nil, _opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
Util.warn "(#{code}) Fetching from Relaton repository ..."
Util.info "Fetching from Relaton repository ...", key: code
item = search(code)
if item
Util.warn "(#{code}) Found: `#{item.docidentifier.first.id}`"
Util.info "Found: `#{item.docidentifier.first.id}`", key: code
item
else
Util.warn "(#{code}) Not found."
Util.info "Not found.", key: code
nil
end
end
Expand Down
5 changes: 1 addition & 4 deletions lib/relaton_ieee/util.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module RelatonIeee
module Util
extend RelatonBib::Util

def self.logger
RelatonIeee.configuration.logger
end
PROGNAME = "relaton-ieee".freeze
end
end
2 changes: 1 addition & 1 deletion lib/relaton_ieee/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RelatonIeee
VERSION = "1.18.0".freeze
VERSION = "1.19.0".freeze
end
4 changes: 4 additions & 0 deletions lib/relaton_ieee/xml_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def parse_balloting_group(editorialgroup)

BallotingGroup.new type: bg[:type], content: bg.text
end

def create_doctype(type)
DocumentType.new type: type.text, abbreviation: type[:abbreviation]
end
end
end
end
2 changes: 1 addition & 1 deletion relaton_ieee.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "faraday", "~> 2.7.0"
spec.add_dependency "mini_portile2", "~> 2.8.0"
spec.add_dependency "relaton-bib", "~> 1.18.0"
spec.add_dependency "relaton-bib", "~> 1.19.0"
spec.add_dependency "relaton-index", "~> 0.2.0"
spec.add_dependency "rubyzip", "~> 2.3.0"
end
2 changes: 1 addition & 1 deletion spec/fixtures/ieee-std.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<bibdata type="standard" schema-version="v1.2.8">
<bibdata type="standard" schema-version="v1.2.9">
<title type="main" format="text/plain">IEEE Draft Supplement to Information technology Telecommunications and information exchange between systems Local and metropolitan area networks Part 5: Token Ring access method and physical layer specifications 100 Mbit/s Dedicated Token Ring Operation (Supplement to ISO/IEC 8802-5:1998 and ISO/IEC 8802-5:1998/Amd.1:1998)</title>
<uri type="src">https://ieeexplore.ieee.org/document/4039945</uri>
<docidentifier type="IEEE" primary="true">IEEE P802.5t/D-2.5</docidentifier>
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/ieee-std.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
schema-version: v1.2.8
schema-version: v1.2.9
id: IEEEP802.5t-D-2.5
title:
- content: 'IEEE Draft Supplement to Information technology Telecommunications and
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/ieee_528_2019.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<bibdata type="standard" schema-version="v1.2.8">
<bibdata type="standard" schema-version="v1.2.9">
<fetched>2023-05-16</fetched>
<title type="main" format="text/plain">IEEE Standard for Inertial Sensor Terminology</title>
<uri type="src">https://ieeexplore.ieee.org/document/8863799</uri>
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/ieee_528_2019.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
id: IEEE528-2019
schema-version: v1.2.8
schema-version: v1.2.9
title:
- type: main
content: IEEE Standard for Inertial Sensor Terminology
Expand Down
4 changes: 3 additions & 1 deletion spec/relaton_ieee/balloting_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
it "warn when type is invalid" do
expect do
described_class.new type: "invalid", content: "Group"
end.to output("[relaton-ieee] WARNING: type of Balloting group must be one of `individual`, `entity`\n").to_stderr
end.to output(
"[relaton-ieee] WARN: type of Balloting group must be one of `individual`, `entity`\n"
).to_stderr_from_any_process
end
end

Expand Down
10 changes: 0 additions & 10 deletions spec/relaton_ieee/config_spec.rb

This file was deleted.

16 changes: 9 additions & 7 deletions spec/relaton_ieee/data_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
files = Dir["spec/fixtures/rawbib/**/*.{xml,zip}"]
expect(Dir).to receive(:[]).with("ieee-rawbib/**/*.{xml,zip}").and_return files
expect(df).to receive(:fetch_doc).and_raise(StandardError).twice
expect { df.fetch }.to output(/File: spec\/fixtures\/rawbib/).to_stderr
expect { df.fetch }.to output(/File: spec\/fixtures\/rawbib/).to_stderr_from_any_process
end

it "handle empty file" do
expect do
expect(df.fetch_doc("", "file.xml")).to be_nil
end.to output(/Empty file: file\.xml/).to_stderr
end.to output(/WARN: Empty file: `file\.xml`/).to_stderr_from_any_process
end

it "create relation" do
Expand Down Expand Up @@ -62,8 +62,8 @@
XML
bib.instance_variable_set :@docnumber, "3412"
expect { df.fetch_doc(doc, "file.xml") }.to output(
/Document exists ID: "IEEE 5678" AMSID: "1234" source: "file\.xml"\. Other AMSID: "4321"/,
).to_stderr
/WARN: Document exists ID: `IEEE 5678` AMSID: `1234` source: `file\.xml`\. Other AMSID: `4321`/,
).to_stderr_from_any_process
end

it "rewrite file if PubID includes a docnumber" do
Expand All @@ -79,8 +79,8 @@
XML
expect(File).to receive(:write).with("data/5678.yaml", kind_of(String), encoding: "UTF-8")
expect { df.fetch_doc(doc, "file.xml") }.to output(
/Document exists ID: "IEEE 5678" AMSID: "1234" source: "file\.xml"\. Other AMSID: "4321"/,
).to_stderr
/WARN: Document exists ID: `IEEE 5678` AMSID: `1234` source: `file\.xml`\. Other AMSID: `4321`/,
).to_stderr_from_any_process
end
end

Expand Down Expand Up @@ -168,7 +168,9 @@
expect(RelatonIeee::DataParser).to receive(:new).with(kind_of(Nokogiri::XML::Element), df).and_return dp
expect do
expect(df.fetch_doc(xml, "filename")).to be_nil
end.to output("PubID parse error. Normtitle: Title, file: filename\n").to_stderr
end.to output(
"[relaton-ieee] WARN: PubID parse error. Normtitle: `Title`, file: `filename`\n"
).to_stderr_from_any_process
end

context "save document" do
Expand Down
6 changes: 2 additions & 4 deletions spec/relaton_ieee/document_status_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
describe RelatonIeee::DocumentStatus do
before { RelatonIeee.instance_variable_set :@configuration, nil }

it "warn when stage is invalid" do
expect do
described_class::Stage.new value: "invalid"
end.to output(
"[relaton-ieee] Stage value must be one of: `draft`, `approved`, `superseded`, `withdrawn`\n",
).to_stderr
"[relaton-ieee] WARN: Stage value must be one of: `draft`, `approved`, `superseded`, `withdrawn`\n",
).to_stderr_from_any_process
end
end
4 changes: 1 addition & 3 deletions spec/relaton_ieee/document_type_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
describe RelatonIeee::DocumentType do
before { RelatonIeee.instance_variable_set :@configuration, nil }

it "warn when doctype is invalid" do
expect do
described_class.new type: "invalid"
end.to output(
"[relaton-ieee] Invalid doctype: `invalid`. It should be one of: " \
"[relaton-ieee] WARN: Invalid doctype: `invalid`. It should be one of: " \
"`guide`, `recommended-practice`, `standard`, `witepaper`, `redline`, `other`.\n",
).to_stderr_from_any_process
end
Expand Down
6 changes: 2 additions & 4 deletions spec/relaton_ieee/ieee_bibliographic_item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
RSpec.describe RelatonIeee::IeeeBibliographicItem do
before { RelatonIeee.instance_variable_set :@configuration, nil }

it "returns AsciiBib" do
input = "spec/fixtures/ieee_528_2019.yaml"
hash = YAML.safe_load File.read(input, encoding: "UTF-8")
Expand All @@ -16,8 +14,8 @@
expect do
described_class.new docsubtype: "invalid"
end.to output(
"[relaton-ieee] Invalid docsubtype: `invalid`. It should be one of: " \
"[relaton-ieee] WARN: Invalid docsubtype: `invalid`. It should be one of: " \
"`amendment`, `corrigendum`, `erratum`.\n",
).to_stderr
).to_stderr_from_any_process
end
end
5 changes: 0 additions & 5 deletions spec/relaton_ieee/util_spec.rb

This file was deleted.

8 changes: 8 additions & 0 deletions spec/relaton_ieee/xml_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@
expect(eg.working_group).to eq "Working group"
expect(eg.committee).to eq ["Committee1", "Committee2"]
end

it "create_doctype" do
elm = Nokogiri::XML("<doctype abbreviation='GD'>guide</doctype>").root
dt = described_class.send :create_doctype, elm
expect(dt).to be_instance_of RelatonIeee::DocumentType
expect(dt.type).to eq "guide"
expect(dt.abbreviation).to eq "GD"
end
end
4 changes: 1 addition & 3 deletions spec/relaton_ieee_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
RSpec.describe RelatonIeee do
before { RelatonIeee.instance_variable_set :@configuration, nil }

it "has a version number" do
expect(RelatonIeee::VERSION).not_to be nil
end
Expand Down Expand Up @@ -65,7 +63,7 @@
expect do
result = RelatonIeee::IeeeBibliography.get "IEEE 528-2018"
expect(result).to be_nil
end.to output(/\[relaton-ieee\] \(IEEE 528-2018\) Not found\./).to_stderr
end.to output(/\[relaton-ieee\] INFO: \(IEEE 528-2018\) Not found\./).to_stderr_from_any_process
end
end

Expand Down
Loading

0 comments on commit 9c325d3

Please sign in to comment.