2018-06-19 02:37:19 +00:00
|
|
|
//
|
|
|
|
// Log.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/06/2018.
|
|
|
|
// Copyright © 2018 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2018-06-19 02:37:19 +00:00
|
|
|
|
2024-01-20 03:02:26 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
|
|
|
|
2024-01-19 15:35:34 +00:00
|
|
|
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.
|
|
|
|
|
2024-01-19 15:35:34 +00:00
|
|
|
enum class Source {
|
2024-01-20 03:02:26 +00:00
|
|
|
ADBDevice,
|
2024-01-19 19:16:13 +00:00
|
|
|
ADBGLU,
|
2024-01-19 16:45:29 +00:00
|
|
|
Amiga,
|
|
|
|
AmigaDisk,
|
|
|
|
AmigaCopper,
|
|
|
|
AmigaChipset,
|
|
|
|
AmigaBlitter,
|
2024-02-15 18:55:46 +00:00
|
|
|
AppleIISCSICard,
|
2024-03-06 14:54:39 +00:00
|
|
|
Archimedes,
|
2024-01-19 16:45:29 +00:00
|
|
|
AtariST,
|
2024-01-20 03:17:35 +00:00
|
|
|
AtariSTDMAController,
|
2024-01-19 19:00:53 +00:00
|
|
|
CommodoreStaticAnalyser,
|
2024-01-19 16:45:29 +00:00
|
|
|
DirectAccessDevice,
|
|
|
|
Enterprise,
|
|
|
|
i8272,
|
|
|
|
IntelligentKeyboard,
|
|
|
|
IWM,
|
|
|
|
M50740,
|
|
|
|
Macintosh,
|
|
|
|
MasterSystem,
|
2024-01-19 19:22:23 +00:00
|
|
|
MultiMachine,
|
2024-01-19 16:45:29 +00:00
|
|
|
MFP68901,
|
2024-02-15 18:32:49 +00:00
|
|
|
MOS6526,
|
2024-01-19 16:45:29 +00:00
|
|
|
MSX,
|
|
|
|
NCR5380,
|
|
|
|
OpenGL,
|
2024-01-19 19:22:23 +00:00
|
|
|
PCMTrack,
|
2024-01-19 15:47:21 +00:00
|
|
|
SCC,
|
2024-01-19 16:45:29 +00:00
|
|
|
SCSI,
|
|
|
|
SZX,
|
|
|
|
TapeUEF,
|
|
|
|
TMS9918,
|
|
|
|
TZX,
|
2024-01-20 03:02:26 +00:00
|
|
|
Vic20,
|
2024-01-19 16:45:29 +00:00
|
|
|
WDFDC,
|
2024-01-18 21:37:07 +00:00
|
|
|
};
|
|
|
|
|
2024-01-19 15:35:34 +00:00
|
|
|
constexpr bool is_enabled(Source source) {
|
2024-01-19 15:41:47 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2024-01-19 15:35:34 +00:00
|
|
|
// Allow for compile-time source-level enabling and disabling of different sources.
|
|
|
|
switch(source) {
|
|
|
|
default: return true;
|
2024-01-19 15:47:21 +00:00
|
|
|
|
2024-01-19 16:45:29 +00:00
|
|
|
// The following are all things I'm not actively working on.
|
2024-01-20 03:14:24 +00:00
|
|
|
case Source::AmigaBlitter:
|
|
|
|
case Source::AmigaChipset:
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::AmigaCopper:
|
|
|
|
case Source::AmigaDisk:
|
2024-02-15 18:55:46 +00:00
|
|
|
case Source::DirectAccessDevice:
|
2024-01-19 20:38:40 +00:00
|
|
|
case Source::IWM:
|
2024-01-19 19:22:23 +00:00
|
|
|
case Source::MFP68901:
|
2024-01-19 16:45:29 +00:00
|
|
|
case Source::NCR5380:
|
2024-02-15 18:55:46 +00:00
|
|
|
case Source::SCC:
|
|
|
|
case Source::SCSI:
|
|
|
|
return false;
|
2024-01-19 15:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const char *prefix(Source source) {
|
|
|
|
switch(source) {
|
2024-01-19 19:00:53 +00:00
|
|
|
default: return nullptr;
|
|
|
|
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::ADBDevice: return "ADB device";
|
2024-01-19 19:16:13 +00:00
|
|
|
case Source::ADBGLU: return "ADB GLU";
|
2024-01-20 03:14:24 +00:00
|
|
|
case Source::AmigaBlitter: return "Blitter";
|
|
|
|
case Source::AmigaChipset: return "Chipset";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::AmigaCopper: return "Copper";
|
|
|
|
case Source::AmigaDisk: return "Disk";
|
2024-02-15 18:55:46 +00:00
|
|
|
case Source::AppleIISCSICard: return "SCSI card";
|
2024-03-06 14:54:39 +00:00
|
|
|
case Source::Archimedes: return "Archimedes";
|
2024-01-19 19:16:13 +00:00
|
|
|
case Source::AtariST: return "AtariST";
|
2024-01-20 03:17:35 +00:00
|
|
|
case Source::AtariSTDMAController: return "DMA";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::CommodoreStaticAnalyser: return "Commodore Static Analyser";
|
2024-02-15 18:55:46 +00:00
|
|
|
case Source::DirectAccessDevice: return "Direct Access Device";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::Enterprise: return "Enterprise";
|
2024-01-19 19:00:53 +00:00
|
|
|
case Source::i8272: return "i8272";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::IntelligentKeyboard: return "IKYB";
|
2024-01-19 20:38:40 +00:00
|
|
|
case Source::IWM: return "IWM";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::M50740: return "M50740";
|
|
|
|
case Source::Macintosh: return "Macintosh";
|
|
|
|
case Source::MasterSystem: return "SMS";
|
2024-02-15 18:32:49 +00:00
|
|
|
case Source::MOS6526: return "MOS6526";
|
2024-01-19 19:22:23 +00:00
|
|
|
case Source::MFP68901: return "MFP68901";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::MultiMachine: return "Multi-machine";
|
|
|
|
case Source::MSX: return "MSX";
|
2024-01-19 19:00:53 +00:00
|
|
|
case Source::NCR5380: return "5380";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::OpenGL: return "OpenGL";
|
2024-01-19 19:22:23 +00:00
|
|
|
case Source::PCMTrack: return "PCM Track";
|
2024-01-19 19:00:53 +00:00
|
|
|
case Source::SCSI: return "SCSI";
|
|
|
|
case Source::SCC: return "SCC";
|
2024-01-19 19:16:13 +00:00
|
|
|
case Source::SZX: return "SZX";
|
|
|
|
case Source::TapeUEF: return "UEF";
|
|
|
|
case Source::TZX: return "TZX";
|
2024-01-20 03:02:26 +00:00
|
|
|
case Source::Vic20: return "Vic20";
|
2024-01-19 19:00:53 +00:00
|
|
|
case Source::WDFDC: return "WD FDC";
|
2024-01-19 15:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 15:41:47 +00:00
|
|
|
template <Source source>
|
2024-01-18 21:37:07 +00:00
|
|
|
class Logger {
|
|
|
|
public:
|
2024-01-19 19:22:23 +00:00
|
|
|
static constexpr bool enabled = is_enabled(source);
|
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
struct LogLine {
|
|
|
|
public:
|
2024-01-19 15:41:47 +00:00
|
|
|
LogLine(FILE *stream) : stream_(stream) {
|
2024-01-19 19:22:23 +00:00
|
|
|
if constexpr (!enabled) return;
|
2024-01-19 15:35:34 +00:00
|
|
|
|
|
|
|
const auto source_prefix = prefix(source);
|
|
|
|
if(source_prefix) {
|
2024-01-19 15:41:47 +00:00
|
|
|
fprintf(stream_, "[%s] ", source_prefix);
|
2024-01-18 21:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-11 02:45:03 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
~LogLine() {
|
2024-01-19 19:22:23 +00:00
|
|
|
if constexpr (!enabled) return;
|
2024-01-18 21:37:07 +00:00
|
|
|
fprintf(stream_, "\n");
|
|
|
|
}
|
2019-10-11 02:45:03 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
void append(const char *format, ...) {
|
2024-01-19 19:22:23 +00:00
|
|
|
if constexpr (!enabled) return;
|
2024-01-18 21:37:07 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(stream_, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2019-10-11 02:45:03 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
private:
|
|
|
|
FILE *stream_;
|
|
|
|
};
|
2019-10-11 02:45:03 +00:00
|
|
|
|
2024-01-19 15:41:47 +00:00
|
|
|
LogLine info() { return LogLine(stdout); }
|
|
|
|
LogLine error() { return LogLine(stderr); }
|
2024-01-18 21:37:07 +00:00
|
|
|
};
|
2018-06-19 02:37:19 +00:00
|
|
|
|
2024-01-19 15:35:34 +00:00
|
|
|
}
|