1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-22 12:33:29 +00:00

Promote source to a template paramter, to unify cases.

This commit is contained in:
Thomas Harte 2024-01-19 10:41:47 -05:00
parent 1d6537c3a9
commit bdfbf779fa

View File

@ -14,9 +14,14 @@ namespace Log {
enum class Source { enum class Source {
WDFDC, WDFDC,
SCSI,
}; };
constexpr bool is_enabled(Source source) { constexpr bool is_enabled(Source source) {
#ifdef NDEBUG
return false;
#endif
// Allow for compile-time source-level enabling and disabling of different sources. // Allow for compile-time source-level enabling and disabling of different sources.
switch(source) { switch(source) {
default: return true; default: return true;
@ -25,50 +30,37 @@ constexpr bool is_enabled(Source source) {
constexpr const char *prefix(Source source) { constexpr const char *prefix(Source source) {
switch(source) { switch(source) {
case Source::WDFDC: return "[WD FDC]"; case Source::WDFDC: return "WD FDC";
case Source::SCSI: return "SCSI";
} }
} }
#ifdef NDEBUG
class Logger {
public:
Logger(Source) {}
struct LogLine {
void append(const char *, ...) {}
};
LogLine info() { return LogLine(); }
LogLine error() { return LogLine(); }
};
#else
#include <cstdio> #include <cstdio>
#include <cstdarg> #include <cstdarg>
template <Source source>
class Logger { class Logger {
public: public:
Logger(Source source) : source_(source) {} Logger() {}
struct LogLine { struct LogLine {
public: public:
LogLine(Source source, FILE *stream) : source_(source), stream_(stream) { LogLine(FILE *stream) : stream_(stream) {
if(!is_enabled(source_)) return; if constexpr (!is_enabled(source)) return;
const auto source_prefix = prefix(source); const auto source_prefix = prefix(source);
if(source_prefix) { if(source_prefix) {
fprintf(stream_, "%s ", source_prefix); fprintf(stream_, "[%s] ", source_prefix);
} }
} }
~LogLine() { ~LogLine() {
if(!is_enabled(source_)) return; if constexpr (!is_enabled(source)) return;
fprintf(stream_, "\n"); fprintf(stream_, "\n");
} }
void append(const char *format, ...) { void append(const char *format, ...) {
if(!is_enabled(source_)) return; if constexpr (!is_enabled(source)) return;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
vfprintf(stream_, format, args); vfprintf(stream_, format, args);
@ -76,17 +68,11 @@ class Logger {
} }
private: private:
Source source_;
FILE *stream_; FILE *stream_;
}; };
LogLine info() { return LogLine(source_, stdout); } LogLine info() { return LogLine(stdout); }
LogLine error() { return LogLine(source_, stderr); } LogLine error() { return LogLine(stderr); }
private:
Source source_;
}; };
#endif
} }