Commit 24998cd2ab0f1c938038bb9e28cb87619f749ead

Thomas de Grivel 2023-02-24T01:32:11

logging

diff --git a/bin/rbpkg b/bin/rbpkg
index 973b14b..e01bf99 100755
--- a/bin/rbpkg
+++ b/bin/rbpkg
@@ -1,5 +1,4 @@
 #!/usr/bin/env ruby
-load "#{File.dirname(File.dirname(__FILE__))}/lib/rbpkg/log.rb"
 load "#{File.dirname(File.dirname(__FILE__))}/lib/rbpkg.rb"
 
 $ARGS = ARGV
@@ -11,20 +10,20 @@ def shift
   arg
 end
 
-verbose = 2   # print command output
+verbose_level = 2   # print command output
 case $ARGS[0]
 when "-v"
-  verbose = 3 # print all messages
+  verbose_level = 3 # print all messages
   shift
 when "-q"
-  verbose = 1 # print commands
+  verbose_level = 1 # print commands
   shift
 when "-qq"
-  verbose = 0 # print nothing
+  verbose_level = 0 # print nothing
   shift
 end
 
-Rbpkg.init("rbpkg", verbose)
+Rbpkg.init("rbpkg", verbose_level)
 
 def usage()
   STDERR.puts """Usage: #{File.basename($0)} COMMAND REPO ...
@@ -112,3 +111,5 @@ def rbpkg
 end
 
 rbpkg
+
+Rbpkg::Log.ok_all
diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 4fe69d6..6a0968a 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -1,4 +1,5 @@
 require 'time'
+load "#{File.dirname(__FILE__)}/rbpkg/log.rb"
 load "#{File.dirname(__FILE__)}/rbpkg/repos.rb"
 
 module Rbpkg
@@ -129,8 +130,8 @@ module Rbpkg
     ].each do |d|
       ensure_dir(d)
     end
-    Rbpkg::Log.init(name)
     $log = Rbpkg::Log
+    Rbpkg::Log.init(name)
     Rbpkg.lock(name)
   end
 
@@ -160,7 +161,7 @@ module Rbpkg
 
   def self.lock(name)
     path = lock_path(name)
-    #verbose(2, "Locking #{path}")
+    verbose(3, "Locking #{path}")
     while File.file?(path)
       sleep 1000.ms
     end
@@ -348,6 +349,5 @@ def verbose(level, msg)
               raise "unknown verbose level: #{level.inspect}"
             end
   $log.puts colored
-  puts colored if level <= Rbpkg.verbose_level
-
+  STDOUT.puts colored if level <= Rbpkg.verbose_level
 end
diff --git a/lib/rbpkg/log.rb b/lib/rbpkg/log.rb
index e7947ae..12712e9 100644
--- a/lib/rbpkg/log.rb
+++ b/lib/rbpkg/log.rb
@@ -1,34 +1,85 @@
 module Rbpkg
   module Log
 
-    @@logs = []
-
     def self.add(name)
       raise "already logging to #{name.inspect}" if @@logs.include?(name)
       raise "log file exists: #{name.inspect}" if File.exist?(path(name))
       @@logs << name
+      verbose(3, "Logging to #{path(name)}")
+    end
+
+    def self.ansi_to_html(string)
+      substitution = {
+        "&" => "&amp;",
+        "<" => "&lt;",
+        ">" => "&gt;",
+        "\33[0;34m" => "<span style=\"color: blue;\">",
+        "\33[0;35m" => "<span style=\"color: purple;\">",
+        "\33[0m" => "</span>"
+      }
+      s = string.clone
+      substitution.each do |k, v|
+        s.gsub!(k, v)
+      end
+      s
     end
 
     def self.init(name)
       @@logs = []
       add(name)
+      at_exit { ko_all }
+    end
+
+    def self.ko(name)
+      puts_by_name(name, "KO")
+      remove(name)
+    end
+
+    def self.ko_all
+      if @@logs != []
+        verbose(3, "KO")
+        @@logs = []
+      end
+    end
+
+    def self.ok(name)
+      puts_by_name(name, "OK")
+      remove(name)
+    end
+
+    def self.ok_all
+      if @@logs != []
+        verbose(3, "OK")
+        @@logs = []
+      end
     end
 
     def self.path(name)
       "#{Rbpkg.log_dir}/#{name}_#{Rbpkg.date}_#{$$}.log"
     end
 
+    def self.path_html(name)
+      "#{path(name)}.html"
+    end
+
     def self.puts(string)
       @@logs.each do |name|
-        File.open(path(name), "a") do |output|
-          output.puts string
-        end
+        puts_by_name(name, string)
+      end
+    end
+
+    def self.puts_by_name(name, string)
+      File.open(path(name), "a") do |output|
+        output.puts string
+      end
+      File.open(path_html(name), "a") do |output|
+        output.puts ansi_to_html(string)
       end
     end
 
     def self.remove(name)
       raise "not logging to #{name.inspect}" unless @@logs.include?(name)
-      @@logs = @@logs.without(name)
+      @@logs.delete(name)
     end
   end
 end