Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit a55e616

Browse files
committed
Add CC::Logger
We have this in other engines and it allows for better handling of debug vs info/warn engine logging.
1 parent 4ca4466 commit a55e616

File tree

17 files changed

+61
-110
lines changed

17 files changed

+61
-110
lines changed

‎bin/duplication‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# frozen_string_literal: true
33

44
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "../lib")))
5+
require "cc/logger"
56
require "cc/engine/duplication"
67

78
engine_config =
@@ -11,6 +12,13 @@ engine_config =
1112
{}
1213
end
1314

15+
CC.logger.level =
16+
if config["debug"]
17+
::Logger::DEBUG
18+
else
19+
::Logger::INFO
20+
end
21+
1422
directory = ARGV[0] || "/code"
1523

1624
CC::Engine::Duplication.new(

‎lib/cc/engine/analyzers/analyzer_base.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ def initialize(engine_config:)
2323

2424
def run(file)
2525
if (skip_reason = skip?(file))
26-
$stderr.puts("Skipping file #{file} because #{skip_reason}")
26+
CC.logger.info("Skipping file #{file} because #{skip_reason}")
27+
nil
2728
else
2829
process_file(file)
2930
end
3031
rescue => ex
3132
if RESCUABLE_ERRORS.map { |klass| ex.instance_of?(klass) }.include?(true)
32-
$stderr.puts("Skipping file #{file} due to exception (#{ex.class}): #{ex.message}\n#{ex.backtrace.join("\n")}")
33+
CC.logger.info("Skipping file #{file} due to exception (#{ex.class}): #{ex.message}\n#{ex.backtrace.join("\n")}")
34+
nil
3335
else
34-
$stderr.puts("#{ex.class} error occurred processing file #{file}: aborting.")
36+
CC.logger.info("#{ex.class} error occurred processing file #{file}: aborting.")
3537
raise ex
3638
end
3739
end

‎lib/cc/engine/analyzers/engine_config.rb‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ def initialize(hash)
1111
@config = normalize(hash)
1212
end
1313

14-
def debug?
15-
config.fetch("config", {}).fetch("debug", "false").to_s.casecmp("true").zero?
16-
end
17-
1814
def include_paths
1915
config.fetch("include_paths", ["./"])
2016
end

‎lib/cc/engine/analyzers/javascript/parser.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def parse
2424

2525
self
2626
rescue Timeout::Error
27-
warn"TIMEOUT parsing #{filename}. Skipping."
27+
CC.logger.warn("TIMEOUT parsing #{filename}. Skipping.")
2828
end
2929

3030
private

‎lib/cc/engine/analyzers/php/parser.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def parse
3333

3434
self
3535
rescue Timeout::Error
36-
warn"TIMEOUT parsing #{filename}. Skipping."
36+
CC.logger.warn("TIMEOUT parsing #{filename}. Skipping.")
3737
end
3838

3939
private

‎lib/cc/engine/analyzers/python/parser.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def parse
2626

2727
self
2828
rescue Timeout::Error
29-
warn"TIMEOUT parsing #{filename}. Skipping."
29+
CC.logger.warn("TIMEOUT parsing #{filename}. Skipping.")
3030
end
3131

3232
private

‎lib/cc/engine/analyzers/reporter.rb‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ def initialize(engine_config, language_strategy, io)
2020
end
2121

2222
def run
23-
debug("Processing #{language_strategy.files.count} #{lang} files concurrency=#{engine_config.concurrency}")
23+
CC.logger.debug("Processing #{language_strategy.files.count} #{lang} files concurrency=#{engine_config.concurrency}")
2424

2525
process_files
2626

2727
if engine_config.dump_ast?
2828
dump_ast
2929
else
3030
report
31-
debug("Reported #{reports.size} violations...")
31+
CC.logger.debug("Reported #{reports.size} violations...")
3232
end
3333
end
3434

@@ -39,11 +39,10 @@ def dump_ast
3939

4040
return if issues.empty?
4141

42-
debug "Sexps for issues:"
43-
debug
42+
CC.logger.debug("Sexps for issues:")
4443

4544
issues.each_with_index do |issue, idx1|
46-
debug(
45+
CC.logger.debug(
4746
format(
4847
"#%2d) %s#%d mass=%d:",
4948
idx1 + 1,
@@ -52,20 +51,15 @@ def dump_ast
5251
issue.mass,
5352
),
5453
)
55-
debug
5654

5755
locs = issue.locations.map.with_index do |loc, idx2|
5856
format("# %d.%d) %s:%s", idx1 + 1, idx2 + 1, loc.file, loc.line)
5957
end
6058

6159
locs.zip(flay.hashes[issue.structural_hash]).each do |loc, sexp|
62-
debug loc
63-
debug
64-
debug sexp.pretty_inspect
65-
debug
60+
CC.logger.debug(loc)
61+
CC.logger.debug(sexp.pretty_inspect)
6662
end
67-
68-
debug
6963
end
7064
end
7165

@@ -78,7 +72,7 @@ def process_files
7872
processed_files_count = Concurrent::AtomicFixnum.new
7973

8074
pool.run do |file|
81-
debug("Processing #{lang} file: #{file}")
75+
CC.logger.debug("Processing #{lang} file: #{file}")
8276

8377
sexp = language_strategy.run(file)
8478

@@ -89,7 +83,7 @@ def process_files
8983

9084
pool.join
9185

92-
debug("Processed #{processed_files_count.value} #{lang} files")
86+
CC.logger.debug("Processed #{processed_files_count.value} #{lang} files")
9387
end
9488

9589
def lang
@@ -102,7 +96,7 @@ def report
10296

10397
violations.each do |violation|
10498
next if (violation.occurrences + 1) < language_strategy.count_threshold
105-
debug("Violation name=#{violation.report_name} mass=#{violation.mass}")
99+
CC.logger.debug("Violation name=#{violation.report_name} mass=#{violation.mass}")
106100

107101
unless reports.include?(violation.report_name)
108102
reports.add(violation.report_name)
@@ -140,12 +134,6 @@ def flay_options
140134

141135
CCFlay.default_options.merge changes
142136
end
143-
144-
def debug(message = "")
145-
IO_M.synchronize do
146-
$stderr.puts(message) if engine_config.debug?
147-
end
148-
end
149137
end
150138
end
151139
end

‎lib/cc/engine/analyzers/ruby/main.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Main < CC::Engine::Analyzers::Base
2727
def process_file(file)
2828
RubyParser.new.process(File.binread(file), file, TIMEOUT)
2929
rescue Timeout::Error
30-
warn"TIMEOUT parsing #{file}. Skipping."
30+
CC.logger.warn("TIMEOUT parsing #{file}. Skipping.")
3131
end
3232
end
3333
end

‎lib/cc/engine/duplication.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def languages
5454

5555
if languages.empty?
5656
message = "Config Error: Unable to run the duplication engine without any languages enabled."
57-
$stderr.putsmessage
57+
CC.logger.info(message)
5858
raise EmptyLanguagesError, message
5959
else
6060
languages

‎lib/cc/logger.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require "logger"
4+
5+
module CC
6+
def self.logger
7+
@logger ||= ::Logger.new(STDERR)
8+
end
9+
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /