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, log_directory, logging_enabled = false, cycle_size = 2000000000) ⇒ 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.

  • log_directory (String)

    The directory to store the log files.

  • logging_enabled (Boolean) (defaults to: false)

    Whether to enable raw logging

  • cycle_size (Integer) (defaults to: 2000000000)

    The size in bytes before creating a new log file.



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

def initialize(
  log_name,
  log_type,
  log_directory,
  logging_enabled = false,
  cycle_size = 2000000000
)
  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
  @log_directory = log_directory
  @cycle_size = ConfigParser.handle_nil(cycle_size)
  @cycle_size = Integer(@cycle_size) if @cycle_size
  @mutex = Mutex.new
  @file = nil
  @filename = nil
  @start_time = Time.now.sys
  @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



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

def filename
  @filename
end

#logging_enabledBoolean (readonly)

Returns Is logging enabled?.

Returns:

  • (Boolean)

    Is logging enabled?



35
36
37
# File 'lib/cosmos/io/raw_logger.rb', line 35

def logging_enabled
  @logging_enabled
end

#orig_nameString (readonly)

Returns Original name passed to raw logger.

Returns:

  • (String)

    Original name passed to raw logger



38
39
40
# File 'lib/cosmos/io/raw_logger.rb', line 38

def orig_name
  @orig_name
end

#queueQueue (readonly)

Returns Queue for asynchronous logging.

Returns:

  • (Queue)

    Queue for asynchronous logging



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

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:

  • log_name (String)

    new name



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

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.sys - @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



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

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