rb_falcon_logger 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d0abbcdd59c340b7fb01541a2d0fe4246a6335e79be9a3ea88644c9859f085d
4
- data.tar.gz: 89cf30440ae1d50e97a8bcf621ab07799eed2d3fbfc0ef742a2ce25801e0d282
3
+ metadata.gz: d580d23f926e2754b3faff8493637860980164d80e84c71d5f0673b032d35253
4
+ data.tar.gz: 3b344b9be5ba10542b22188cabf1db9ddf9bf6c2ffe9a6b79774de1e8fcd0907
5
5
  SHA512:
6
- metadata.gz: 1a57f92c007fbc4115c789c18d2643920b0bdd8a46b8960b29dffc095ba97898c13c93858c5d5be3557edf077044c0f9ccfa75ea37fab6459ec0de7501fe3c95
7
- data.tar.gz: 7669f2137c27f6aeb465aca9fc1c1a6ef2d9dd5018421df9d426c7dc76b8ff98d4e189749dcc971ed749505c8c05409868045911bec0725c1ff67bdb3fcc16fe
6
+ metadata.gz: 4fb74ebd819ca38ddaf44953388e0dd4c9aae95d589a5f46e4241693018e498ec43a5ce7a6f9355f38b18112c965b6df647658b69e5ce7591500191edaa6bd4d
7
+ data.tar.gz: 24f527bd3d2f31989a5ef2066f8dc9241f842450b674dd8f4befe4ce89119f8ccd840d4324ca53cb75bb03e940a99a41b0fd5786f64cc21199c2e38a953e8e6b
data/README.md CHANGED
@@ -12,8 +12,16 @@ see src/logger_tester.rb for a full example
12
12
 
13
13
  Use doit script to run the logger.
14
14
 
15
- TODO logger_tester args
16
- TODO multithreaded_tester
15
+ ```text
16
+ ./doit # (default) normal mode
17
+ ./doit normal
18
+ ./doit immediate
19
+ ./doit ut
20
+ ./doit mock # same as ut mode
21
+ ./doit null # no output
22
+
23
+ ./doit thread # runs multiple loggers in multiple threads
24
+ ```
17
25
 
18
26
  ## parameters and functions
19
27
 
@@ -30,9 +38,10 @@ Use these on the logger creation: ```@log = FalconLogger(path: etc, max_entries:
30
38
  * use ```@log.save``` to force the queue to be saved. Generally not recommended.
31
39
  * use ```@log.term``` to clear out the queue and gracefully end the background thread
32
40
 
33
- There are 3 modes: ```FalconLogger(mode: 'normal')```
41
+ There are 4 modes: ```FalconLogger(mode: 'normal')```
34
42
 
35
43
  * nil or 'normal': (default) writes a line to stdout or the file. See below for outputs
44
+ * 'immediate': does not use a bg thread
36
45
  * 'ut' or 'mock': useful in UTs. These do not write to stdout or to a file.
37
46
  * The lines are saved in an internal list accessible by: ```@log.ut_lines```.
38
47
  * Use ```@log.ut_clear``` or ```@log.ut_lines = []``` to empty it.
@@ -1,9 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require 'json'
3
3
 
4
- # import atexit
5
- # import threading
6
-
7
4
  # -------------------
8
5
  ## Sample gem source
9
6
  class FalconLogger
@@ -118,6 +115,8 @@ class FalconLogger
118
115
  @fp = File.open(@path, 'w', encoding: 'UTF-8')
119
116
  end
120
117
  _init_thread
118
+ elsif mode == 'immediate'
119
+ _set_log_it_immediate_fn
121
120
  elsif %w[ut mock].include?(mode)
122
121
  _set_log_it_ut_fn
123
122
  elsif mode == 'null'
@@ -210,7 +209,20 @@ class FalconLogger
210
209
  raise(ArgumentError, "Unknown format: \"#{form}\", choose \"elapsed\", \"prefix\" or \"none\"")
211
210
  end
212
211
 
213
- _set_log_it_ut_fn if %w[ut mock].include?(@logging_mode)
212
+ if %w[immediate].include?(@logging_mode)
213
+ _set_log_it_immediate_fn
214
+ elsif %w[ut mock].include?(@logging_mode)
215
+ _set_log_it_ut_fn
216
+ end
217
+ end
218
+
219
+ # --------------------
220
+ ## write the line to stdout
221
+ #
222
+ # @return nil
223
+ def _set_log_it_immediate_fn
224
+ @fp = $stdout
225
+ @log_it = method(:_log_it_immediate)
214
226
  end
215
227
 
216
228
  # --------------------
@@ -273,6 +285,21 @@ class FalconLogger
273
285
  #
274
286
  # @return nil
275
287
  def term
288
+ if @logging_mode == 'normal'
289
+ term_normal
290
+ else
291
+ # for immediate do a final flush
292
+ # other modes don't need it, but it won't harm anything
293
+ $stdout.flush
294
+ end
295
+ end
296
+
297
+ # --------------------
298
+ ## terminate in normal mode
299
+ # stop the thread, save any remaining line in the internal queue
300
+ #
301
+ # @return nil
302
+ def term_normal
276
303
  begin
277
304
  # since this will be called during atexit() handling,
278
305
  # stdout and/or file can be closed. Protect against this case.
@@ -282,7 +309,7 @@ class FalconLogger
282
309
  end
283
310
 
284
311
  @finished = true
285
- @thread.join(5.0) unless @thread.nil? || !@thread.alive?
312
+ @thread.join(2.0) unless @thread.nil? || !@thread.alive?
286
313
  end
287
314
 
288
315
  # --------------------
@@ -302,6 +329,17 @@ class FalconLogger
302
329
  @queue.push(info)
303
330
  end
304
331
 
332
+ # --------------------
333
+ ## since it's immediate mode, write the logging info to stdout and flush stdout
334
+ #
335
+ # @param info the line info
336
+ # @return nil
337
+ def _log_it_immediate(info)
338
+ write_line(info)
339
+ # ensure to flush after every line/dot
340
+ $stdout.flush
341
+ end
342
+
305
343
  # --------------------
306
344
  ## since it's ut mode and no prefixes, add the line to ut_lines
307
345
  #
@@ -383,9 +421,9 @@ class FalconLogger
383
421
  # --------------------
384
422
  ## since it's null mode, ignore the line
385
423
  #
386
- # @param info (ignored)
424
+ # @param _info (ignored)
387
425
  # @return nil
388
- def _log_it_null(info)
426
+ def _log_it_null(_info)
389
427
  # logging ignored
390
428
  end
391
429
 
@@ -687,102 +725,107 @@ class FalconLogger
687
725
  # :nocov:
688
726
 
689
727
  # get the next tuple: (always_print, verbose, dts, prefix, args)
690
- tuple = @queue.pop
691
- always_print = tuple[0]
692
- verbose = tuple[1]
693
- dts = tuple[2]
694
- prefix = tuple[3]
695
- args = tuple[4]
696
- count -= 1
697
-
698
- # uncomment to debug
699
- # puts("#{always_print} #{verbose} '#{prefix}' #{dts} '#{args}'")
700
-
701
- # not verbose and ok not to print
702
- next unless verbose || always_print
728
+ tuple = @queue.pop
729
+ count -= 1
730
+ write_line(tuple)
731
+ end
703
732
 
704
- if args[0].nil? && prefix == '.'
705
- # dots used to log waiting periods, so okay to call fn
706
- _handle_dots
707
- next
708
- end
733
+ # uncomment to debug
734
+ # @fp.close
709
735
 
710
- # at this point, not a dot
736
+ # flush lines to stdout/file; protect with except in case
737
+ begin
738
+ @fp.flush
739
+ rescue IOError
740
+ # coverage: rare case: if stdout/file is closed this will throw an exception
741
+ end
742
+ end
711
743
 
712
- # last call was a dot, so reset and print a newline ready for the new log line
713
- if @dots != 0
714
- @dots = 0
715
- @fp.write("\n")
716
- @runner_cfg.copy_from(@backup_cfg)
717
- end
744
+ # -------------------
745
+ def write_line(tuple)
746
+ always_print = tuple[0]
747
+ verbose = tuple[1]
748
+ dts = tuple[2]
749
+ prefix = tuple[3]
750
+ args = tuple[4]
718
751
 
719
- # print the full DTS requested by the user
720
- if args[0].nil? && prefix.nil?
721
- # rare request, so okay to call fn
722
- _handle_full_dts(dts)
723
- next
724
- end
752
+ # uncomment to debug
753
+ # puts("#{always_print} #{verbose} '#{prefix}' #{dts} '#{args}'")
725
754
 
726
- # print with no prefix or elapsed time
727
- if @log_format == LogFormat::NONE
728
- line = args.join(' ')
729
- @fp.write(line)
730
- @fp.write("\n")
731
- next
732
- end
755
+ # not verbose and ok not to print
756
+ return unless verbose || always_print
733
757
 
734
- # print with the prefix, but no elapsed time
735
- if @log_format == LogFormat::PREFIX
736
- line = args.join(' ')
737
- if prefix.nil?
738
- msg = line # raw line
739
- else
740
- msg = format('%-4s %s', prefix, line)
741
- end
742
- @fp.write(msg)
743
- @fp.write("\n")
744
- next
745
- end
758
+ if args[0].nil? && prefix == '.'
759
+ # dots used to log waiting periods, so okay to call fn
760
+ _handle_dots
761
+ return
762
+ end
746
763
 
747
- # at this point, mode is LogFormat.ELAPSED
764
+ # at this point, not a dot
748
765
 
749
- # approximately once an hour, restart the time period
750
- elapsed = Time.at(dts - @start_time)
751
- if elapsed.to_f > 3600.0
752
- # rare, so okay to call fn
753
- # display the time at the moment the log line was saved
754
- _handle_full_dts(dts)
766
+ # last call was a dot, so reset and print a newline ready for the new log line
767
+ if @dots != 0
768
+ @dots = 0
769
+ @fp.write("\n")
770
+ @runner_cfg.copy_from(@backup_cfg)
771
+ end
755
772
 
756
- # recalc the elapsed time, should be 0
757
- # add the small value for round-off issues
758
- elapsed = Time.at(dts - @start_time) # + 0.0001)
759
- end
773
+ # print the full DTS requested by the user
774
+ if args[0].nil? && prefix.nil?
775
+ # rare request, so okay to call fn
776
+ _handle_full_dts(dts)
777
+ return
778
+ end
760
779
 
761
- # print prefix and elapsed time
780
+ # print with no prefix or elapsed time
781
+ if @log_format == LogFormat::NONE
762
782
  line = args.join(' ')
783
+ @fp.write(line)
784
+ @fp.write("\n")
785
+ return
786
+ end
763
787
 
764
- # log the line
788
+ # print with the prefix, but no elapsed time
789
+ if @log_format == LogFormat::PREFIX
790
+ line = args.join(' ')
765
791
  if prefix.nil?
766
- msg = line
792
+ msg = line # raw line
767
793
  else
768
- t_str = _get_elapsed_str(elapsed)
769
- msg = format('%s %-4s %s', t_str, prefix, line)
794
+ msg = format('%-4s %s', prefix, line)
770
795
  end
771
-
772
- # assume @fp is not closed
773
796
  @fp.write(msg)
774
797
  @fp.write("\n")
798
+ return
775
799
  end
776
800
 
777
- # uncomment to debug
778
- # @fp.close
801
+ # at this point, mode is LogFormat.ELAPSED
779
802
 
780
- # flush lines to stdout/file; protect with except in case
781
- begin
782
- @fp.flush
783
- rescue IOError
784
- # coverage: rare case: if stdout/file is closed this will throw an exception
803
+ # approximately once an hour, restart the time period
804
+ elapsed = Time.at(dts - @start_time)
805
+ if elapsed.to_f > 3600.0
806
+ # rare, so okay to call fn
807
+ # display the time at the moment the log line was saved
808
+ _handle_full_dts(dts)
809
+
810
+ # recalc the elapsed time, should be 0
811
+ # add the small value for round-off issues
812
+ elapsed = Time.at(dts - @start_time) # + 0.0001)
813
+ end
814
+
815
+ # print prefix and elapsed time
816
+ line = args.join(' ')
817
+
818
+ # log the line
819
+ if prefix.nil?
820
+ msg = line
821
+ else
822
+ t_str = _get_elapsed_str(elapsed)
823
+ msg = format('%s %-4s %s', t_str, prefix, line)
785
824
  end
825
+
826
+ # assume @fp is not closed
827
+ @fp.write(msg)
828
+ @fp.write("\n")
786
829
  end
787
830
 
788
831
  # --------------------
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
  ## current version
3
- VERSION = '0.1.1'
3
+ VERSION = '0.3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb_falcon_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Arrizza
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-13 00:00:00.000000000 Z
11
+ date: 2025-09-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: fast logger with multiple modes and formats
14
14
  email: cppgent0@gmail.com