Skip to content

Commit

Permalink
Overhaul the log stuff to actually work again (better).
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgiven committed Oct 13, 2024
1 parent c537348 commit a006e0b
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 72 deletions.
14 changes: 12 additions & 2 deletions lib/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ static const std::vector<ImageConstructor> imageConstructors = {
{".xdf", IMAGETYPE_IMG, MODE_RW},
};

struct OptionLogMessage
{
std::string message;
};

void renderLogMessage(LogRenderer& r, std::shared_ptr<const OptionLogMessage> m)
{
r.newline().add("OPTION:").add(m->message).newline();
}

Config& globalConfig()
{
return config;
Expand Down Expand Up @@ -442,8 +452,8 @@ bool Config::isOptionValid(std::string option)

void Config::applyOption(const OptionProto& option)
{
log("OPTION: {}",
option.has_message() ? option.message() : option.comment());
log(OptionLogMessage{
option.has_message() ? option.message() : option.comment()});

_appliedOptions.insert(option.name());
}
Expand Down
19 changes: 5 additions & 14 deletions lib/core/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ static bool indented = false;
static std::function<void(const AnyLogMessage&)> loggerImpl =
[](const auto& message)
{
std::cout << Logger::toString(message) << std::flush;
static auto r = LogRenderer::create(std::cout);
r->add(message);
};

void log(const char* m)
Expand Down Expand Up @@ -42,24 +43,14 @@ void renderLogMessage(LogRenderer& r, std::shared_ptr<const std::string> msg)
r.newline().add(*msg).newline();
}

std::string Logger::toString(const AnyLogMessage& message)
LogRenderer& LogRenderer::add(const AnyLogMessage& message)
{
std::stringstream stream;

auto indent = [&]()
{
if (!indented)
stream << " ";
indented = false;
};

auto r = LogRenderer::create();
std::visit(
[&](const auto& arg)
{
renderLogMessage(*r, arg);
renderLogMessage(*this, arg);
},
message);

return stream.str();
return *this;
}
37 changes: 21 additions & 16 deletions lib/core/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,7 @@ class DiskFlux;
class TrackDataFlux;
class TrackFlux;
class Sector;

class LogRenderer
{
public:
static std::unique_ptr<LogRenderer> create();

public:
virtual LogRenderer& add(std::string m) = 0;
virtual LogRenderer& newsection() = 0;
virtual LogRenderer& newline() = 0;

virtual void renderTo(std::ostream& stream) = 0;
};
class LogRenderer;

struct ErrorLogMessage;
struct EmergencyStopMessage;
Expand All @@ -35,6 +23,7 @@ struct EndWriteOperationLogMessage;
struct BeginOperationLogMessage;
struct EndOperationLogMessage;
struct OperationProgressLogMessage;
struct OptionLogMessage;

struct ErrorLogMessage
{
Expand Down Expand Up @@ -71,6 +60,8 @@ extern void renderLogMessage(
LogRenderer& r, std::shared_ptr<const EndOperationLogMessage> m);
extern void renderLogMessage(
LogRenderer& r, std::shared_ptr<const OperationProgressLogMessage> m);
extern void renderLogMessage(
LogRenderer& r, std::shared_ptr<const OptionLogMessage> m);

typedef std::variant<std::shared_ptr<const std::string>,
std::shared_ptr<const ErrorLogMessage>,
Expand All @@ -85,7 +76,8 @@ typedef std::variant<std::shared_ptr<const std::string>,
std::shared_ptr<const EndWriteOperationLogMessage>,
std::shared_ptr<const BeginOperationLogMessage>,
std::shared_ptr<const EndOperationLogMessage>,
std::shared_ptr<const OperationProgressLogMessage>>
std::shared_ptr<const OperationProgressLogMessage>,
std::shared_ptr<const OptionLogMessage>>
AnyLogMessage;

extern void log(const char* ptr);
Expand All @@ -103,11 +95,24 @@ inline void log(fmt::string_view fstr, const Args&... args)
log(fmt::format(fstr, args...));
}

class LogRenderer
{
public:
static std::unique_ptr<LogRenderer> create(std::ostream& stream);

public:
LogRenderer& add(const AnyLogMessage& message);

public:
virtual LogRenderer& add(const std::string& m) = 0;
virtual LogRenderer& comma() = 0;
virtual LogRenderer& header(const std::string& h) = 0;
virtual LogRenderer& newline() = 0;
};

namespace Logger
{
extern void setLogger(std::function<void(const AnyLogMessage&)> cb);

extern std::string toString(const AnyLogMessage&);
}

#endif
80 changes: 57 additions & 23 deletions lib/core/logrenderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,89 @@ namespace
class LogRendererImpl : public LogRenderer
{
public:
LogRenderer& add(std::string m) override
LogRendererImpl(std::ostream& stream): _stream(stream) {}

private:
void indent()
{
_stream << " ";
_lineLen = 7;
_space = true;
}

public:
LogRenderer& add(const std::string& m) override
{
if (_atNewline && _indented)
if (_newline && !_header)
indent();

if (!_space)
{
_stream << " ";
_lineLen = 4;
_indented = true;
_stream << ' ';
_lineLen++;
}

if ((m.size() + _lineLen) > 80)
_newline = false;
_header = false;

_lineLen += m.size();
if (_lineLen >= 80)
{
_stream << "\n ";
_lineLen = 4;
_indented = true;
_stream << '\n';
indent();
}
_stream << m;
_space = !m.empty() && isspace(m[m.size() - 1]);
return *this;
}

LogRenderer& header(const std::string& m) override
{
if (!_newline)
_stream << '\n';
_stream << m;
_lineLen = m.size();
_header = true;
_newline = true;
_space = !m.empty() && isspace(m[m.size() - 1]);
return *this;
}

LogRenderer& newsection() override
LogRenderer& comma() override
{
newline();
_indented = false;
if (!_newline || _header)
{
_stream << ';';
_space = false;
}
return *this;
}

LogRenderer& newline() override
{
if (!_atNewline)
if (!_header)
{
_stream << '\n';
_atNewline = true;
if (!_newline)
_stream << '\n';

_lineLen = 0;
_header = false;
_newline = true;
_space = true;
}
_lineLen = 0;
return *this;
}

void renderTo(std::ostream& stream) override {}

private:
bool _atNewline = false;
bool _indented = false;
bool _header = false;
bool _newline = false;
bool _space = false;
int _lineLen = 0;
std::stringstream _stream;
std::ostream& _stream;
};
}

std::unique_ptr<LogRenderer> LogRenderer::create()
std::unique_ptr<LogRenderer> LogRenderer::create(std::ostream& stream)
{
return std::make_unique<LogRendererImpl>();
return std::make_unique<LogRendererImpl>(stream);
}
17 changes: 9 additions & 8 deletions lib/readerwriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void renderLogMessage(
void renderLogMessage(
LogRenderer& r, std::shared_ptr<const BeginWriteOperationLogMessage> m)
{
r.newsection().add(fmt::format("{:2}.{}: ", m->track, m->head));
r.header(fmt::format("W{:2}.{}: ", m->track, m->head));
}

/* Indicates that we're finishing a write operation. */
Expand All @@ -70,7 +70,7 @@ void renderLogMessage(
void renderLogMessage(
LogRenderer& r, std::shared_ptr<const BeginReadOperationLogMessage> m)
{
r.newsection().add(fmt::format("{:2}.{}: ", m->track, m->head));
r.header(fmt::format("R{:2}.{}: ", m->track, m->head));
}

/* Indicates that we're finishing a read operation. */
Expand Down Expand Up @@ -102,13 +102,13 @@ void renderLogMessage(
if (!rawSectors.empty())
clock /= rawSectors.size();

r.add(fmt::format("{} raw records, {} raw sectors",
r.comma().add(fmt::format("{} raw records, {} raw sectors",
rawRecords.size(),
rawSectors.size()));
if (clock != 0)
{
r.add(fmt::format(
"; {:.2f}us clock ({:.0f}kHz)", clock / 1000.0, 1000000.0 / clock));
r.comma().add(fmt::format(
"{:.2f}us clock ({:.0f}kHz)", clock / 1000.0, 1000000.0 / clock));
}

r.newline().add("sectors:");
Expand All @@ -118,7 +118,7 @@ void renderLogMessage(
std::sort(sectors.begin(), sectors.end(), sectorPointerSortPredicate);

for (const auto& sector : sectors)
r.add(fmt::format(" {}.{}.{}{}",
r.add(fmt::format("{}.{}.{}{}",
sector->logicalTrack,
sector->logicalSide,
sector->logicalSector,
Expand Down Expand Up @@ -349,13 +349,14 @@ ReadResult readGroup(FluxSourceIteratorHolder& fluxSourceIteratorHolder,
for (unsigned offset = 0; offset < trackInfo->groupSize;
offset += Layout::getHeadWidth())
{
log(BeginReadOperationLogMessage{
trackInfo->physicalTrack + offset, trackInfo->physicalSide});

auto& fluxSourceIterator = fluxSourceIteratorHolder.getIterator(
trackInfo->physicalTrack + offset, trackInfo->physicalSide);
if (!fluxSourceIterator.hasNext())
continue;

log(BeginReadOperationLogMessage{
trackInfo->physicalTrack + offset, trackInfo->physicalSide});
std::shared_ptr<const Fluxmap> fluxmap = fluxSourceIterator.next();
// ->rescale(
// 1.0 / globalConfig()->flux_source().rescale());
Expand Down
1 change: 1 addition & 0 deletions src/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dep/stb",
"+lib",
"lib/core",
"lib/data",
"lib/vfs",
"lib/config",
"lib/fluxsource+proto_lib",
Expand Down
1 change: 1 addition & 0 deletions src/gui/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"extras+icons",
"+lib",
"lib/core",
"lib/data",
"lib/vfs",
"lib/config",
"lib/fluxsource+proto_lib",
Expand Down
2 changes: 1 addition & 1 deletion src/gui/idlepanel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class IdlePanelImpl : public IdlePanelGen, public IdlePanel
if (formatSelection == wxNOT_FOUND)
error("no format selected");

ClearLog();
globalConfig().clear();
auto formatName = _formatNames[formatChoice->GetSelection()];
globalConfig().readBaseConfigFile(formatName);
Expand Down Expand Up @@ -292,7 +293,6 @@ class IdlePanelImpl : public IdlePanelGen, public IdlePanel
globalConfig().set("usb.serial", serial);

globalConfig().validateAndThrow();
ClearLog();
}

const wxBitmap GetBitmap() override
Expand Down
Loading

0 comments on commit a006e0b

Please sign in to comment.