Class: Cosmos::RawLogger

Inherits:
Object show all
Defined in:
lib/cosmos/io/raw_logger.rb

Overview

Creates a log file of raw data for either reads or writes. Can automatically cycle the log based on when the log file reaches a predefined size.

Constant Summary collapse

LOG_TYPES =

The allowable log types

[:READ, :WRITE]
CYCLE_TIME_INTERVAL =

The cycle time interval. Cycle times are only checked at this level of granularity.

60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_name, log_type, logging_enabled = false, cycle_size = 2000000000, log_directory = nil) ⇒ RawLogger

Returns a new instance of RawLogger.

Parameters:

  • log_name (String)

    The name of the raw logger. Typically matches the name of the corresponding interface

  • log_type (Symbol)

    The type of log to create. Must be :READ or :WRITE.

  • cycle_time (Integer)

    The amount of time in seconds before creating a new log file. This can be combined with cycle_size but is better used independently.

  • cycle_size (Integer) (defaults to: 2000000000)

    The size in bytes before creating a new log file. This can be combined with cycle_time but is better used independently.

  • log_directory (String) (defaults to: nil)

    The directory to store the log files. Passing nil will use the system default 'LOGS' directory.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cosmos/io/raw_logger.rb', line 52

def initialize(
  log_name,
  log_type,
  logging_enabled = false,
  cycle_size = 2000000000,
  log_directory = nil
)
  raise "log_type must be :READ or :WRITE" unless LOG_TYPES.include? log_type
  @log_type = log_type
  @orig_name = log_name
  @log_name = (log_name.to_s.downcase + '_raw_' + @log_type.to_s.downcase + '_' + self.object_id.to_s).freeze
  @cycle_size = ConfigParser.handle_nil(cycle_size)
  @cycle_size = Integer(@cycle_size) if @cycle_size
  if ConfigParser.handle_nil(log_directory)
    @log_directory = log_directory
  else
    @log_directory = nil
  end
  @mutex = Mutex.new
  @file = nil
  @filename = nil
  @start_time = Time.now
  @logging_enabled = ConfigParser.handle_true_false(logging_enabled)
end

Instance Attribute Details

#filenameString (readonly)

Returns The filename of the log.

Returns:

  • (String)

    The filename of the log



22
23
24
# File 'lib/cosmos/io/raw_logger.rb', line 22

def filename
  @filename
end

#logging_enabledBoolean (readonly)

Returns Is logging enabled?.

Returns:

  • (Boolean)

    Is logging enabled?



28
29
30
# File 'lib/cosmos/io/raw_logger.rb', line 28

def logging_enabled
  @logging_enabled
end

#orig_nameObject (readonly)

Returns the value of attribute orig_name.



31
32
33
# File 'lib/cosmos/io/raw_logger.rb', line 31

def orig_name
  @orig_name
end

#queueQueue (readonly)

Returns Queue for asynchronous logging.

Returns:

  • (Queue)

    Queue for asynchronous logging



25
26
27
# File 'lib/cosmos/io/raw_logger.rb', line 25

def queue
  @queue
end

Instance Method Details

#cloneObject

Create a clone of this object with a new name



125
126
127
128
129
# File 'lib/cosmos/io/raw_logger.rb', line 125

def clone
  raw_logger = super()
  raw_logger.name = raw_logger.orig_name
  raw_logger
end

#name=(log_name) ⇒ Object

Set the raw logger name

Parameters:



79
80
81
82
# File 'lib/cosmos/io/raw_logger.rb', line 79

def name=(log_name)
  @orig_name = log_name
  @log_name = (log_name.to_s.downcase + '_raw_' + @log_type.to_s.downcase + '_' + self.object_id.to_s).freeze
end

#startObject

Starts a new log file by closing the existing log file. New log files are not created until data is written by #write so this does not immediately create a log file on the filesystem.



113
114
115
116
# File 'lib/cosmos/io/raw_logger.rb', line 113

def start
  close_file() unless ((Time.now - @start_time) < 1.0) # Prevent close/open too fast
  @mutex.synchronize { @logging_enabled = true }
end

#stopObject

Stops all logging and closes the current log file.



119
120
121
122
# File 'lib/cosmos/io/raw_logger.rb', line 119

def stop
  @mutex.synchronize { @logging_enabled = false }
  close_file()
end

#write(data) ⇒ Object

Write data to the log file.

If no log file currently exists in the filesystem, a new file will be created.

Writing a log file is a critical operation so the entire method is wrapped with a rescue and handled with handle_critical_exception

Parameters:

  • data (String)

    The data to write to the log file



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cosmos/io/raw_logger.rb', line 93

def write(data)
  if @logging_enabled
    return if !data or data.length <= 0
    need_new_file = false
    @mutex.synchronize do
      if !@file or (@cycle_size and (@file.stat.size + data.length) > @cycle_size)
        need_new_file = true
      end
    end
    start_new_file() if need_new_file
    @mutex.synchronize { @file.write(data) if @file }
  end
rescue => err
  Logger.instance.error "Error writing #{@filename} : #{err.formatted}"
  Cosmos.handle_critical_exception(err)
end