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-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 {
|
|
|
|
WDFDC,
|
2024-01-18 21:37:07 +00:00
|
|
|
};
|
|
|
|
|
2024-01-19 15:35:34 +00:00
|
|
|
constexpr bool is_enabled(Source source) {
|
|
|
|
// 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]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 02:37:19 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
class Logger {
|
|
|
|
public:
|
2024-01-19 15:35:34 +00:00
|
|
|
Logger(Source) {}
|
2018-06-21 23:27:54 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
struct LogLine {
|
|
|
|
void append(const char *, ...) {}
|
|
|
|
};
|
2024-01-19 15:35:34 +00:00
|
|
|
LogLine info() { return LogLine(); }
|
|
|
|
LogLine error() { return LogLine(); }
|
2024-01-18 21:37:07 +00:00
|
|
|
};
|
2018-06-19 02:37:19 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
2018-06-19 02:37:19 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
class Logger {
|
|
|
|
public:
|
2024-01-19 15:35:34 +00:00
|
|
|
Logger(Source source) : source_(source) {}
|
2018-06-21 23:27:54 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
struct LogLine {
|
|
|
|
public:
|
2024-01-19 15:35:34 +00:00
|
|
|
LogLine(Source source, FILE *stream) : source_(source), stream_(stream) {
|
|
|
|
if(!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
|
|
|
}
|
|
|
|
}
|
2019-10-11 02:45:03 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
~LogLine() {
|
2024-01-19 15:35:34 +00:00
|
|
|
if(!is_enabled(source_)) 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 15:35:34 +00:00
|
|
|
if(!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);
|
|
|
|
}
|
2019-10-11 02:45:03 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
private:
|
2024-01-19 15:35:34 +00:00
|
|
|
Source source_;
|
2024-01-18 21:37:07 +00:00
|
|
|
FILE *stream_;
|
|
|
|
};
|
2019-10-11 02:45:03 +00:00
|
|
|
|
2024-01-19 15:35:34 +00:00
|
|
|
LogLine info() { return LogLine(source_, stdout); }
|
|
|
|
LogLine error() { return LogLine(source_, stderr); }
|
2018-06-21 23:27:54 +00:00
|
|
|
|
2024-01-18 21:37:07 +00:00
|
|
|
private:
|
2024-01-19 15:35:34 +00:00
|
|
|
Source source_;
|
2024-01-18 21:37:07 +00:00
|
|
|
};
|
2018-06-19 02:37:19 +00:00
|
|
|
|
2019-10-11 02:45:03 +00:00
|
|
|
#endif
|
2024-01-19 15:35:34 +00:00
|
|
|
|
|
|
|
}
|