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 // Released under MIT license
#include <fcntl.h> #include <fcntl.h>
@ -9,6 +9,8 @@
#include "log.h" #include "log.h"
static SdFat sd;
disk_backend_esp32::disk_backend_esp32(const std::string & filename) : disk_backend_esp32::disk_backend_esp32(const std::string & filename) :
filename(filename), filename(filename),
fh(new File32()) fh(new File32())
@ -22,10 +24,16 @@ disk_backend_esp32::~disk_backend_esp32()
delete fh; 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() bool disk_backend_esp32::begin()
{ {
if (!fh->open(filename.c_str(), O_RDWR)) { if (!fh->open(filename.c_str(), O_RDWR)) {
DOLOG(ll_error, true, "rk05: cannot open \"%s\"", filename.c_str()); DOLOG(ll_error, true, "rk05: cannot open \"%s\"", filename.c_str());
emit_error();
return false; 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); digitalWrite(LED_BUILTIN, LOW);
#endif #endif
if (!fh->seek(offset)) if (!fh->seek(offset)) {
DOLOG(ll_error, true, "seek error %s", strerror(errno)); DOLOG(ll_error, true, "seek error %s", strerror(errno));
emit_error();
return false;
}
yield(); yield();
if (fh->read(target, n) != size_t(n)) { ssize_t rc = fh->read(target, n);
DOLOG(debug, true, "fread error: %s", strerror(errno)); 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) #if defined(ESP32) && !defined(SHA2017)
digitalWrite(LED_BUILTIN, HIGH); 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); digitalWrite(LED_BUILTIN, LOW);
#endif #endif
if (!fh->seek(offset)) if (!fh->seek(offset)) {
DOLOG(ll_error, true, "seek error %s", strerror(errno)); DOLOG(ll_error, true, "seek error %s", strerror(errno));
emit_error();
return false;
}
yield(); yield();
if (fh->write(from, n) != n) { ssize_t rc = fh->write(from, n);
DOLOG(ll_error, true, "RK05 fwrite error %s", strerror(errno)); 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) #if defined(ESP32) && !defined(SHA2017)
digitalWrite(LED_BUILTIN, HIGH); 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 // Released under MIT license
#include <string> #include <string>
@ -17,6 +17,8 @@ private:
const std::string filename; const std::string filename;
File32 *const fh { nullptr }; File32 *const fh { nullptr };
void emit_error();
public: public:
disk_backend_esp32(const std::string & filename); disk_backend_esp32(const std::string & filename);
virtual ~disk_backend_esp32(); 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 // Released under MIT license
#include <fcntl.h> #include <fcntl.h>
@ -44,7 +44,7 @@ bool disk_backend_file::read(const off_t offset, const size_t n, uint8_t *const
#else #else
ssize_t rc = pread(fd, target, n, offset); ssize_t rc = pread(fd, target, n, offset);
if (rc != ssize_t(n)) { 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; 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); return ::write(fd, from, n) == ssize_t(n);
#else #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 #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 // Released under MIT license
#include <fcntl.h> #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) { if (READ(fd, reinterpret_cast<char *>(&nbd_hello), sizeof nbd_hello) != sizeof nbd_hello) {
close(fd); close(fd);
fd = -1; 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) { if (memcmp(nbd_hello.magic1, "NBDMAGIC", 8) != 0) {
close(fd); close(fd);
fd = -1; 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)); 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 { do {
if (fd == -1 && !connect(true)) { 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); sleep(1);
continue; 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); nbd_request.length = htonl(n);
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) { 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); close(fd);
fd = -1; fd = -1;
sleep(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; } nbd_reply;
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof 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); close(fd);
fd = -1; fd = -1;
sleep(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) { 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); close(fd);
fd = -1; fd = -1;
sleep(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); int error = ntohl(nbd_reply.error);
if (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; return false;
} }
if (READ(fd, reinterpret_cast<char *>(target), n) != ssize_t(n)) { 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); close(fd);
fd = -1; fd = -1;
sleep(1); sleep(1);
@ -217,7 +217,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *
do { do {
if (!connect(true)) { if (!connect(true)) {
DOLOG(debug, false, "disk_backend_nbd::write: (re-)connect"); DOLOG(warning, true, "disk_backend_nbd::write: (re-)connect");
sleep(1); sleep(1);
continue; 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); nbd_request.length = htonl(n);
if (WRITE(fd, reinterpret_cast<const char *>(&nbd_request), sizeof nbd_request) != sizeof nbd_request) { 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); close(fd);
fd = -1; fd = -1;
sleep(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)) { 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); close(fd);
fd = -1; fd = -1;
sleep(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; } nbd_reply;
if (READ(fd, reinterpret_cast<char *>(&nbd_reply), sizeof nbd_reply) != sizeof 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); close(fd);
fd = -1; fd = -1;
sleep(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) { 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); close(fd);
fd = -1; fd = -1;
sleep(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); int error = ntohl(nbd_reply.error);
if (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; return false;
} }
} }