1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-07 23:29:06 +00:00
CLK/Outputs/Log.hpp

79 lines
1.5 KiB
C++
Raw Normal View History

//
// Log.hpp
// Clock Signal
//
// Created by Thomas Harte on 18/06/2018.
// Copyright © 2018 Thomas Harte. All rights reserved.
//
#pragma once
namespace Log {
2024-01-18 21:37:07 +00:00
// TODO: if adopting C++20, std::format would be a better model to apply below.
// But I prefer C files to C++ streams, so here it is for now.
enum class Source {
WDFDC,
SCSI,
2024-01-18 21:37:07 +00:00
};
constexpr bool is_enabled(Source source) {
#ifdef NDEBUG
return false;
#endif
// Allow for compile-time source-level enabling and disabling of different sources.
switch(source) {
default: return true;
}
}
constexpr const char *prefix(Source source) {
switch(source) {
case Source::WDFDC: return "WD FDC";
case Source::SCSI: return "SCSI";
}
}
2024-01-18 21:37:07 +00:00
#include <cstdio>
#include <cstdarg>
template <Source source>
2024-01-18 21:37:07 +00:00
class Logger {
public:
Logger() {}
2018-06-21 23:27:54 +00:00
2024-01-18 21:37:07 +00:00
struct LogLine {
public:
LogLine(FILE *stream) : stream_(stream) {
if constexpr (!is_enabled(source)) return;
const auto source_prefix = prefix(source);
if(source_prefix) {
fprintf(stream_, "[%s] ", source_prefix);
2024-01-18 21:37:07 +00:00
}
}
2024-01-18 21:37:07 +00:00
~LogLine() {
if constexpr (!is_enabled(source)) return;
2024-01-18 21:37:07 +00:00
fprintf(stream_, "\n");
}
2024-01-18 21:37:07 +00:00
void append(const char *format, ...) {
if constexpr (!is_enabled(source)) return;
2024-01-18 21:37:07 +00:00
va_list args;
va_start(args, format);
vfprintf(stream_, format, args);
va_end(args);
}
2024-01-18 21:37:07 +00:00
private:
FILE *stream_;
};
LogLine info() { return LogLine(stdout); }
LogLine error() { return LogLine(stderr); }
2024-01-18 21:37:07 +00:00
};
}