Class: TelemetryLogSink

Inherits:
Logger::LogDevice
  • Object
show all
Defined in:
lib/aws_lambda_ric/telemetry_log_sink.rb

Constant Summary collapse

FRAME_BYTES =
[0xa55a0003].pack('L>')

Instance Method Summary collapse

Constructor Details

#initialize(file:) ⇒ TelemetryLogSink

TelemetryLogSink implements the logging contract between runtimes and the platform. It implements a simple framing protocol so message boundaries can be determined. Each frame can be visualized as follows:

----------------------————————---------------------————————-+ | Frame Type - 4 bytes | Length (len) - 4 bytes | Timestamp - 8 bytes | Message - 'len' bytes | ----------------------————————---------------------————————-+

The first 4 bytes indicate the type of the frame - log frames have a type defined as the hex value 0xa55a0003. The second 4 bytes should indicate the message's length. Next, the timestamp in microsecond precision, and finally 'len' bytes contain the message. The byte order is big-endian.



19
20
21
# File 'lib/aws_lambda_ric/telemetry_log_sink.rb', line 19

def initialize(file:)
  @file = file
end

Instance Method Details

#closeObject



43
44
45
# File 'lib/aws_lambda_ric/telemetry_log_sink.rb', line 43

def close
  # do nothing
end

#reopen(log = nil) ⇒ Object



39
40
41
# File 'lib/aws_lambda_ric/telemetry_log_sink.rb', line 39

def reopen(log = nil)
  # do nothing
end

#write(msg) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aws_lambda_ric/telemetry_log_sink.rb', line 25

def write(msg)
  @semaphore ||= Mutex.new
  if @file.nil? || @file.closed?
    $stdout.write(msg)
  else
    @semaphore.synchronize do
      @file.write(FRAME_BYTES)
      @file.write([msg.bytesize].pack('L>'))
      @file.write([(Time.new.to_f*1000000).to_i].pack('Q>'))
      @file.write(msg)
    end
  end
end