Skip to content

LogTransport

class nmeasim::io::LogTransport · Library API

Records sentences to a log file: a # header when the file is empty, then one line per sentence prefixed with the wall-clock UTC time.

#include <nmeasim/io/transports/log_transport.hpp>

Inherits nmeasim::io::Transport

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp (line 34)

A new or empty file starts with nmeasim::core::log::kHeaderLine, a # recorded: line with the UTC time of the open call and, when a profile name is set, a # profile: line. Each sentence then becomes one line: the ISO 8601 UTC time of the write with millisecond resolution, a space and the sentence without its terminator, ended by a single line feed. The file is flushed after every sentence so that it can be tailed.

See also

Summary

Public member functions

Name Description
LogTransport() Creates a closed recorder for the file at path; nothing is opened until open.
~LogTransport() Destroys the transport, closing the file first.
description() Returns Log followed by the path.
open() Opens the file and writes the header when the file is empty.
close() Closes the file, flushing what is still buffered, and moves to State::Closed.
write() Records line with the current wall-clock UTC time and flushes the file.
path() Returns the path of the log file.
set_profile_name() Sets the profile name written to the # profile: header line.
lines_written() Returns the number of sentences recorded since construction.

Private data members

Name Description
file_ The log file; also holds the path while closed.
append_ True to open in append mode, false to truncate.
profile_name_ Value of the # profile: header line; empty omits it.
lines_written_ Sentences recorded since construction.

Public member functions

LogTransport()

explicit LogTransport(QString path, bool append = true, QObject* parent = nullptr)

Creates a closed recorder for the file at path; nothing is opened until open.

Parameters

Name Type Description
path QString Path of the log file, absolute or relative to the working directory of the process. The directory must exist; the file is created when it does not.
append bool True to continue an existing file, false to truncate it. Only the first successful open truncates; later opens of the same transport continue the recording, so stopping and restarting the simulation does not lose it.
parent QObject* Qt parent that owns the transport; null leaves ownership to the caller.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:46 · defined in src/io/src/transports/log_transport.cpp:30

~LogTransport()

~LogTransport() override

Destroys the transport, closing the file first.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:48 · defined in src/io/src/transports/log_transport.cpp:33

description()

QString description() const override

Returns Log followed by the path.

Returns QString: For example Log /tmp/record.log.

Overrides description · Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:53 · defined in src/io/src/transports/log_transport.cpp:37

open()

bool open() override

Opens the file and writes the header when the file is empty.

Returns true at once when already open. The header is written after truncation, to a new file, and to an existing file of size zero; a non-empty file is continued without a second header. Fails with Cannot open <path> for writing and the system's reason when the file cannot be opened.

Returns bool: True when the file is open, false when opening failed.

Overrides open · Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:62 · defined in src/io/src/transports/log_transport.cpp:41

close()

void close() override

Closes the file, flushing what is still buffered, and moves to State::Closed.

Overrides close · Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:64 · defined in src/io/src/transports/log_transport.cpp:73

write()

void write(const QByteArray& line) override

Records line with the current wall-clock UTC time and flushes the file.

Trailing carriage returns and line feeds are removed and replaced by a single line feed; the rest of the line is written unchanged. Write errors are not reported; they leave bytes_written unchanged but still count in lines_written.

Parameters

Name Type Description
line const QByteArray& One sentence, terminator included or not.

Overrides write · Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:72 · defined in src/io/src/transports/log_transport.cpp:80

path()

QString path() const

Returns the path of the log file.

Returns QString: The path as passed to the constructor.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:77

set_profile_name()

void set_profile_name(const QString& name)

Sets the profile name written to the # profile: header line.

Takes effect at the next open that writes a header.

Parameters

Name Type Description
name const QString& Name of the profile being recorded; empty omits the line.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:83

lines_written()

qint64 lines_written() const noexcept

Returns the number of sentences recorded since construction.

Returns qint64: Calls of write while open, over every open of this transport; header lines are not counted.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:88

Private data members

file_

QFile file_

The log file; also holds the path while closed.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:92

append_

bool append_

True to open in append mode, false to truncate.

Set to true by the first successful open.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:95

profile_name_

QString profile_name_

Value of the # profile: header line; empty omits it.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:97

lines_written_

qint64 lines_written_{0}

Sentences recorded since construction.

Declared in src/io/include/nmeasim/io/transports/log_transport.hpp:99

Inherited members

From nmeasim::io::LogTransport: bytes_written(), client_count(), client_count_changed(), count_bytes(), error_occurred(), fail(), is_open(), last_error(), set_state(), State, state(), state_changed(), Transport()