Converge File object with std::fstream. Simplify LogFile.

LogFile no longer watches SD card presence and tries to open/close files dynamically.
This commit is contained in:
Jared Boone 2016-04-30 15:09:34 -07:00
parent 4d6fccd8ea
commit 0d6103916d
11 changed files with 59 additions and 88 deletions

View File

@ -136,7 +136,7 @@ AISLogger::AISLogger(
void AISLogger::on_packet(const ais::Packet& packet) {
// TODO: Unstuff here, not in baseband!
if( log_file.is_ready() ) {
if( log_file.is_open() ) {
std::string entry;
entry.reserve((packet.length() + 3) / 4);

View File

@ -110,7 +110,6 @@ public:
private:
CaptureConfig config;
const std::string file_path;
File file;
static Thread* thread;
static msg_t static_fn(void* arg) {
@ -119,7 +118,8 @@ private:
}
msg_t run() {
if( !file.open_for_writing(file_path) ) {
File file { file_path, File::openmode::out | File::openmode::binary | File::openmode::trunc };
if( !file.is_open() ) {
return false;
}
@ -133,7 +133,7 @@ private:
while( !chThdShouldTerminate() ) {
if( stream.available() >= write_size ) {
if( !transfer(stream, write_buffer.get(), write_size) ) {
if( !transfer(stream, file, write_buffer.get(), write_size) ) {
return false;
}
} else {
@ -144,7 +144,7 @@ private:
return true;
}
bool transfer(StreamOutput& stream, uint8_t* const write_buffer, const size_t write_size) {
bool transfer(StreamOutput& stream, File& file, uint8_t* const write_buffer, const size_t write_size) {
bool success = false;
led_usb.on();

View File

@ -66,7 +66,7 @@ ERTLogger::ERTLogger(
}
void ERTLogger::on_packet(const ert::Packet& packet) {
if( log_file.is_ready() ) {
if( log_file.is_open() ) {
const auto formatted = packet.symbols_formatted();
log_file.write_entry(packet.received_at(), formatted.data + "/" + formatted.errors);
}

View File

@ -23,40 +23,32 @@
#include <algorithm>
File::~File() {
close();
}
bool File::open_for_writing(const std::string& file_path) {
const auto open_result = f_open(&f, file_path.c_str(), FA_WRITE | FA_OPEN_ALWAYS);
return (open_result == FR_OK);
}
bool File::open_for_reading(const std::string& file_path) {
const auto open_result = f_open(&f, file_path.c_str(), FA_READ | FA_OPEN_EXISTING);
return (open_result == FR_OK);
}
bool File::open_for_append(const std::string& file_path) {
if( open_for_writing(file_path) ) {
const auto seek_result = f_lseek(&f, f_size(&f));
if( seek_result == FR_OK ) {
return true;
} else {
close();
}
File::File(const std::string& filename, openmode mode) {
BYTE fatfs_mode = 0;
if( mode & openmode::in ) {
fatfs_mode |= FA_READ;
}
if( mode & openmode::out ) {
fatfs_mode |= FA_WRITE;
}
if( mode & openmode::trunc ) {
fatfs_mode |= FA_CREATE_ALWAYS;
}
if( mode & openmode::ate ) {
fatfs_mode |= FA_OPEN_ALWAYS;
}
return false;
if( f_open(&f, filename.c_str(), fatfs_mode) == FR_OK ) {
if( mode & openmode::ate ) {
if( f_lseek(&f, f_size(&f)) != FR_OK ) {
f_close(&f);
}
}
}
}
bool File::close() {
File::~File() {
f_close(&f);
return true;
}
bool File::is_ready() {
return f_error(&f) == 0;
}
bool File::read(void* const data, const size_t bytes_to_read) {

View File

@ -32,14 +32,21 @@
class File {
public:
enum openmode {
app = 0x100,
binary = 0x200,
in = FA_READ,
out = FA_WRITE,
trunc = FA_CREATE_ALWAYS,
ate = FA_OPEN_ALWAYS,
};
File(const std::string& filename, openmode mode);
~File();
bool open_for_writing(const std::string& file_path);
bool open_for_reading(const std::string& file_path);
bool open_for_append(const std::string& file_path);
bool close();
bool is_ready();
bool is_open() const {
return f_error(&f) == 0;
}
bool read(void* const data, const size_t bytes_to_read);
bool write(const void* const data, const size_t bytes_to_write);
@ -57,6 +64,10 @@ private:
FIL f;
};
inline constexpr File::openmode operator|(File::openmode a, File::openmode b) {
return File::openmode(static_cast<int>(a) | static_cast<int>(b));
}
std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern);
namespace std {

View File

@ -23,28 +23,14 @@
#include "string_format.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
LogFile::LogFile(
const std::string& file_path
) : file_path { file_path }
) : file { file_path, File::openmode::out | File::openmode::ate }
{
file.open_for_append(file_path);
sd_card_status_signal_token = sd_card::status_signal += [this](const sd_card::Status status) {
this->on_sd_card_status(status);
};
}
LogFile::~LogFile() {
sd_card::status_signal -= sd_card_status_signal_token;
file.close();
}
bool LogFile::is_ready() {
return file.is_ready();
bool LogFile::is_open() const {
return file.is_open();
}
bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) {
@ -55,11 +41,3 @@ bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) {
bool LogFile::write(const std::string& message) {
return file.puts(message) && file.sync();
}
void LogFile::on_sd_card_status(const sd_card::Status status) {
if( status == sd_card::Status::Mounted ) {
file.open_for_append(file_path);
} else {
file.close();
}
}

View File

@ -25,7 +25,6 @@
#include <string>
#include "file.hpp"
#include "sd_card.hpp"
#include "lpc43xx_cpp.hpp"
using namespace lpc43xx;
@ -33,22 +32,15 @@ using namespace lpc43xx;
class LogFile {
public:
LogFile(const std::string& file_path);
~LogFile();
bool is_ready();
bool is_open() const;
bool write_entry(const rtc::RTC& datetime, const std::string& entry);
private:
const std::string file_path;
File file;
SignalToken sd_card_status_signal_token;
bool write(const std::string& message);
void on_sd_card_status(const sd_card::Status status);
};
#endif/*__LOG_FILE_H__*/

View File

@ -62,7 +62,7 @@ TPMSLogger::TPMSLogger(
void TPMSLogger::on_packet(const tpms::Packet& packet, const uint32_t target_frequency) {
const auto hex_formatted = packet.symbols_formatted();
if( log_file.is_ready() ) {
if( log_file.is_open() ) {
// TODO: function doesn't take uint64_t, so when >= 1<<32, weirdness will ensue!
const auto tuning_frequency_str = to_string_dec_uint(target_frequency, 10);

View File

@ -107,8 +107,7 @@ void RecordView::stop() {
}
void RecordView::write_metadata_file(const std::string& filename) {
File file;
file.open_for_writing(filename);
File file { filename, File::openmode::out | File::openmode::trunc };
file.puts("sample_rate=" + to_string_dec_uint(sampling_rate) + "\n");
file.puts("center_frequency=" + to_string_dec_uint(receiver_model.tuning_frequency()) + "\n");
}

View File

@ -133,15 +133,15 @@ private:
return Result::FailHeap;
}
File file;
if( !file.open_for_writing(filename) ) {
File file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc };
if( !file.is_open() ) {
return Result::FailFileOpenWrite;
}
lfsr_word_t v = 1;
const halrtcnt_t test_start = halGetCounterValue();
while( !chThdShouldTerminate() && file.is_ready() && (_stats.write_bytes < bytes_to_write) ) {
while( !chThdShouldTerminate() && file.is_open() && (_stats.write_bytes < bytes_to_write) ) {
lfsr_fill(v,
reinterpret_cast<lfsr_word_t*>(buffer->data()),
sizeof(*buffer.get()) / sizeof(lfsr_word_t)
@ -164,7 +164,7 @@ private:
}
}
file.close();
file.sync();
const halrtcnt_t test_end = halGetCounterValue();
_stats.write_test_duration = test_end - test_start;
@ -178,15 +178,15 @@ private:
return Result::FailHeap;
}
File file;
if( !file.open_for_reading(filename) ) {
File file { filename, File::openmode::in | File::openmode::binary };
if( !file.is_open() ) {
return Result::FailFileOpenRead;
}
lfsr_word_t v = 1;
const halrtcnt_t test_start = halGetCounterValue();
while( !chThdShouldTerminate() && file.is_ready() && (_stats.read_bytes < bytes_to_read) ) {
while( !chThdShouldTerminate() && file.is_open() && (_stats.read_bytes < bytes_to_read) ) {
const halrtcnt_t read_start = halGetCounterValue();
if( !file.read(buffer->data(), buffer->size()) ) {
break;
@ -211,7 +211,7 @@ private:
}
}
file.close();
file.sync();
const halrtcnt_t test_end = halGetCounterValue();
_stats.read_test_duration = test_end - test_start;

View File

@ -51,9 +51,8 @@ static constexpr std::array<uint8_t, 12> png_iend { {
PNGWriter::PNGWriter(
const std::string& filename
)
) : file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc }
{
file.open_for_writing(filename);
file.write(png_file_header);
file.write(png_ihdr_screen_capture);