SD logging

This commit is contained in:
folkert van heusden 2024-04-19 21:28:37 +02:00
parent f27de9de50
commit 916232e2f0
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
4 changed files with 52 additions and 26 deletions

View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <fcntl.h>
@ -9,6 +9,8 @@
#include "log.h"
static SdFat sd;
disk_backend_esp32::disk_backend_esp32(const std::string & filename) :
filename(filename),
fh(new File32())
@ -22,10 +24,16 @@ disk_backend_esp32::~disk_backend_esp32()
delete fh;
}
void disk_backend_esp32::emit_error()
{
DOLOG(ll_error, true, "SdFat error: %d/%d", sd.sdErrorCode(), sd.sdErrorData());
}
bool disk_backend_esp32::begin()
{
if (!fh->open(filename.c_str(), O_RDWR)) {
DOLOG(ll_error, true, "rk05: cannot open \"%s\"", filename.c_str());
emit_error();
return false;
}
@ -41,13 +49,18 @@ bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const
digitalWrite(LED_BUILTIN, LOW);
#endif
if (!fh->seek(offset))
if (!fh->seek(offset)) {
DOLOG(ll_error, true, "seek error %s", strerror(errno));
emit_error();
return false;
}
yield();
if (fh->read(target, n) != size_t(n)) {
DOLOG(debug, true, "fread error: %s", strerror(errno));
ssize_t rc = fh->read(target, n);
if (size_t(rc) != n) {
DOLOG(ll_error, true, "fread error: %s (%zd/%zu)", strerror(errno), rc, n);
emit_error();
#if defined(ESP32) && !defined(SHA2017)
digitalWrite(LED_BUILTIN, HIGH);
@ -72,13 +85,18 @@ bool disk_backend_esp32::write(const off_t offset, const size_t n, const uint8_t
digitalWrite(LED_BUILTIN, LOW);
#endif
if (!fh->seek(offset))
if (!fh->seek(offset)) {
DOLOG(ll_error, true, "seek error %s", strerror(errno));
emit_error();
return false;
}
yield();
if (fh->write(from, n) != n) {
DOLOG(ll_error, true, "RK05 fwrite error %s", strerror(errno));
ssize_t rc = fh->write(from, n);
if (size_t(rc) != n) {
DOLOG(ll_error, true, "RK05 fwrite error %s (%zd/%zu)", strerror(errno), rc, n);
emit_error();
#if defined(ESP32) && !defined(SHA2017)
digitalWrite(LED_BUILTIN, HIGH);

View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <string>
@ -17,6 +17,8 @@ private:
const std::string filename;
File32 *const fh { nullptr };
void emit_error();
public:
disk_backend_esp32(const std::string & filename);
virtual ~disk_backend_esp32();

View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <fcntl.h>
@ -44,7 +44,7 @@ bool disk_backend_file::read(const off_t offset, const size_t n, uint8_t *const
#else
ssize_t rc = pread(fd, target, n, offset);
if (rc != ssize_t(n)) {
DOLOG(debug, false, "disk_backend_file::read: read failure. expected %zu bytes, got %zd", n, rc);
DOLOG(warning, false, "disk_backend_file::read: read failure. expected %zu bytes, got %zd", n, rc);
return false;
}
@ -62,6 +62,12 @@ bool disk_backend_file::write(const off_t offset, const size_t n, const uint8_t
return ::write(fd, from, n) == ssize_t(n);
#else
return pwrite(fd, from, n, offset) == ssize_t(n);
ssize_t rc = pwrite(fd, from, n, offset);
if (rc != ssize_t(n)) {
DOLOG(warning, false, "disk_backend_file::write: write failure. expected %zu bytes, got %zd", n, rc);
return false;
}
return true;
#endif
}

View file

@ -1,4 +1,4 @@
// (C) 2018-2023 by Folkert van Heusden
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <fcntl.h>
@ -115,14 +115,14 @@ bool disk_backend_nbd::connect(const bool retry)
if (READ(fd, reinterpret_cast<char *>(&nbd_hello), sizeof nbd_hello) != sizeof nbd_hello) {
close(fd);
fd = -1;
DOLOG(debug, false, "disk_backend_nbd::connect: connect short read");
DOLOG(warning, true, "disk_backend_nbd::connect: connect short read");
}
}
if (memcmp(nbd_hello.magic1, "NBDMAGIC", 8) != 0) {
close(fd);
fd = -1;
DOLOG(debug, false, "disk_backend_nbd::connect: magic invalid");
DOLOG(warning, true, "disk_backend_nbd::connect: magic invalid");
}
DOLOG(info, false, "NBD size: %u", NTOHLL(nbd_hello.size));
@ -141,7 +141,7 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t
do {
if (fd == -1 && !connect(true)) {
DOLOG(debug, false, "disk_backend_nbd::read: (re-)connect");
DOLOG(warning, true, "disk_backend_nbd::read: (re-)connect");
sleep(1);
continue;
}
@ -160,7 +160,7 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t
nbd_request.length = htonl(n);
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(debug, false, "disk_backend_nbd::read: problem sending request");
DOLOG(warning, true, "disk_backend_nbd::read: problem sending request");
close(fd);
fd = -1;
sleep(1);
@ -174,7 +174,7 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t
} nbd_reply;
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof nbd_reply) {
DOLOG(debug, false, "disk_backend_nbd::read: problem receiving reply header");
DOLOG(warning, true, "disk_backend_nbd::read: problem receiving reply header");
close(fd);
fd = -1;
sleep(1);
@ -182,7 +182,7 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t
}
if (ntohl(nbd_reply.magic) != 0x67446698) {
DOLOG(debug, false, "disk_backend_nbd::read: bad reply header %08x", nbd_reply.magic);
DOLOG(warning, true, "disk_backend_nbd::read: bad reply header %08x", nbd_reply.magic);
close(fd);
fd = -1;
sleep(1);
@ -191,12 +191,12 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t
int error = ntohl(nbd_reply.error);
if (error) {
DOLOG(debug, false, "disk_backend_nbd::read: NBD server indicated error: %d", error);
DOLOG(warning, true, "disk_backend_nbd::read: NBD server indicated error: %d", error);
return false;
}
if (READ(fd, reinterpret_cast<char *>(target), n) != ssize_t(n)) {
DOLOG(debug, false, "disk_backend_nbd::read: problem receiving payload");
DOLOG(warning, true, "disk_backend_nbd::read: problem receiving payload");
close(fd);
fd = -1;
sleep(1);
@ -217,7 +217,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
do {
if (!connect(true)) {
DOLOG(debug, false, "disk_backend_nbd::write: (re-)connect");
DOLOG(warning, true, "disk_backend_nbd::write: (re-)connect");
sleep(1);
continue;
}
@ -236,7 +236,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
nbd_request.length = htonl(n);
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) {
DOLOG(debug, false, "disk_backend_nbd::write: problem sending request");
DOLOG(warning, true, "disk_backend_nbd::write: problem sending request");
close(fd);
fd = -1;
sleep(1);
@ -244,7 +244,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
}
if (WRITE(fd, reinterpret_cast<const char *>(from), n) != ssize_t(n)) {
DOLOG(debug, false, "disk_backend_nbd::write: problem sending payload");
DOLOG(warning, true, "disk_backend_nbd::write: problem sending payload");
close(fd);
fd = -1;
sleep(1);
@ -258,7 +258,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
} nbd_reply;
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof nbd_reply) {
DOLOG(debug, false, "disk_backend_nbd::write: problem receiving reply header");
DOLOG(warning, true, "disk_backend_nbd::write: problem receiving reply header");
close(fd);
fd = -1;
sleep(1);
@ -266,7 +266,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
}
if (ntohl(nbd_reply.magic) != 0x67446698) {
DOLOG(debug, false, "disk_backend_nbd::write: bad reply header %08x", nbd_reply.magic);
DOLOG(warning, true, "disk_backend_nbd::write: bad reply header %08x", nbd_reply.magic);
close(fd);
fd = -1;
sleep(1);
@ -275,7 +275,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
int error = ntohl(nbd_reply.error);
if (error) {
DOLOG(debug, false, "disk_backend_nbd::write: NBD server indicated error: %d", error);
DOLOG(warning, true, "disk_backend_nbd::write: NBD server indicated error: %d", error);
return false;
}
}