diff --git a/README.md b/README.md index 9e42e15..eb6a296 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,10 @@ The repository contains a collection of random scripts and short programs. 1. [7digital.rb](#7digitalrb) 2. [mock\_server.rb](#mock_serverrb) -3. [read\_logs.rb](#read_logsrb) -4. [cross\_toolchain.rb](#cross_toolchainrb) -5. [rename.rb](#renamerb) -6. [pg\_jekyll.rb](#pg_jekyllrb) -7. [tea-cleaner](#tea-cleaner) +3. [cross\_toolchain.rb](#cross_toolchainrb) +4. [rename.rb](#renamerb) +5. [pg\_jekyll.rb](#pg_jekyllrb) +6. [tea-cleaner](#tea-cleaner) ## 7digital.rb @@ -43,31 +42,6 @@ and in another session: curl localhost:8082 ``` -## read\_logs.rb - -`bin/read\_logs.rb` looks in the `log/` directory for files ending with `.log`, -`.log.1`, `.log.2.gz`, `.log.3.gz` and so forth. It filters out lines starting -with a timestamp, `yyyy-mm-ddThh:mm:ss`, followed by random characters and a -custom string provided as the only command line parameter. Finally -it outputs all matched content after the provided string along with the date. - -The log files are read in the order based on the number in the filename. - -For example calling the script as - -```sh -./bin/read_logs.rb 'doctrine.INFO:' -``` - -on a log file containing -`[2025-02-04T19:51:49.356093+01:00] doctrine.INFO: Disconnecting [] []` - -will print: - -``` -2025-02-04 (Disconnecting []) -``` - ## cross\_toolchain.rb `bin/cross_toolchain.rb` builds a cross toolchain for 32-bit RISC-V (G). The diff --git a/bin/read_logs.rb b/bin/read_logs.rb deleted file mode 100755 index cf4011b..0000000 --- a/bin/read_logs.rb +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env ruby -# This Source Code Form is subject to the terms of the Mozilla Public License, -# v. 2.0. If a copy of the MPL was not distributed with this file, You can -# obtain one at https://mozilla.org/MPL/2.0/. - -# frozen_string_literal: true - -require 'date' -require 'zlib' -require 'pathname' - -class Visit - attr_accessor :page, :date - - def initialize(match_data) - @date = Date.iso8601 match_data[1] - @page = match_data[2].strip - end -end - -def numeric_extension(filename) - if filename.extname == '.gz' - filename.sub_ext('').extname[1..-1].to_i - elsif filename.extname == '.log' - 0 - else - filename.extname[1..-1].to_i - end -end - -entries = Dir.new('log') - .entries - .reject { |entry| entry.start_with? '.' } - .map { |entry| Pathname.new entry } - .sort { |a, b| numeric_extension(b) <=> numeric_extension(a) } - -def read_lines(stream) - if ARGV.length > 0 - regex = /([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}).+#{ARGV[0]}(.+) / - else - regex = /([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(.+) / - end - - stream - .map { |line| line.match regex } - .reject { |line| line.nil? } - .map { |match_data| Visit.new match_data } -end - -lines_in_all_files = entries.flat_map do |log| - log_path = Pathname.new('log') + log - - if log_path.extname == '.gz' - File.open log_path.to_s do |file| - read_lines Zlib::GzipReader.new(file).readlines - end - else - read_lines File.readlines(log_path.to_s) - end -end - -lines_in_all_files.each do |visit| - puts "#{visit.date.strftime} (#{visit.page})" -end