mirror of
https://github.com/TomHarte/CLK.git
synced 2024-09-29 16:55:59 +00:00
Sketch out saner logging interface.
This commit is contained in:
parent
e8917cd294
commit
07fe219148
@ -8,39 +8,66 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// 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 Category {
|
||||||
|
Info,
|
||||||
|
Error,
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
|
|
||||||
#define LOG(x) while(false) {}
|
class Logger {
|
||||||
#define LOGNBR(x) while(false) {}
|
public:
|
||||||
|
Logger(const char *) {}
|
||||||
|
|
||||||
#define ERROR(x) while(false) {}
|
struct LogLine {
|
||||||
#define ERRORNBR(x) while(false) {}
|
void append(const char *, ...) {}
|
||||||
|
};
|
||||||
|
template <Category category> LogLine line() {
|
||||||
|
return LogLine();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#include <iostream>
|
#include <cstdio>
|
||||||
#include <ios>
|
#include <cstdarg>
|
||||||
#include <iomanip>
|
|
||||||
|
|
||||||
#define PADHEX(n) std::hex << std::setfill('0') << std::setw(n)
|
class Logger {
|
||||||
#define PADDEC(n) std::dec << std::setfill('0') << std::setw(n)
|
public:
|
||||||
|
Logger(const char *prefix) : prefix_(prefix) {}
|
||||||
|
|
||||||
#ifdef LOG_PREFIX
|
struct LogLine {
|
||||||
|
public:
|
||||||
|
LogLine(const char *prefix, FILE *stream) : stream_(stream) {
|
||||||
|
if(prefix) {
|
||||||
|
fprintf(stream_, "%s ", prefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#define LOG(x) std::cout << LOG_PREFIX << x << std::endl
|
~LogLine() {
|
||||||
#define LOGNBR(x) std::cout << LOG_PREFIX << x
|
fprintf(stream_, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
#define ERROR(x) std::cerr << LOG_PREFIX << x << std::endl
|
void append(const char *format, ...) {
|
||||||
#define ERRORNBR(x) std::cerr << LOG_PREFIX << x
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
vfprintf(stream_, format, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
private:
|
||||||
|
FILE *stream_;
|
||||||
|
};
|
||||||
|
|
||||||
#define LOG(x) std::cout << x << std::endl
|
template <Category category> LogLine line() {
|
||||||
#define LOGNBR(x) std::cout << x
|
return LogLine(prefix_, category == Info ? stdout : stderr);
|
||||||
|
}
|
||||||
|
|
||||||
#define ERROR(x) std::cerr << x << std::endl
|
private:
|
||||||
#define ERRORNBR(x) std::cerr << x
|
const char *prefix_;
|
||||||
|
};
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user