Skip to content

Instantly share code, notes, and snippets.

@darvil82
Last active September 29, 2023 18:58
Show Gist options
  • Save darvil82/244fa90822ab716a6bc45b9010998bf4 to your computer and use it in GitHub Desktop.
Save darvil82/244fa90822ab716a6bc45b9010998bf4 to your computer and use it in GitHub Desktop.
Stupid logger thingamabob
#include "logger.hpp"
int bye() {
logger log("bye");
log << "byeee";
return 345;
}
int hihi() {
logger log("hi");
log << "hi";
auto b = bye() + 12;
log << "hi2";
return b;
}
int main() {
logger log("main");
log << "am main";
int x = hihi();
log << "back at main";
return 0;
}
[main] am main
[main->hi] hi
[main->hi->bye] byeee
[main->hi] hi2
[main] back at main
#pragma once
#include <string>
#include <deque>
#include <iostream>
class logger {
inline static std::deque<std::string> prefix_stack;
public:
logger(std::string&& prefix) {
logger::prefix_stack.push_back(prefix);
}
~logger() {
logger::prefix_stack.pop_back();
}
void print(std::string&& stuff) const {
std::cout << logger::get_stack_repr() << " " << stuff << std::endl;
}
void operator<<(std::string&& stuff) const {
this->print(std::move(stuff));
}
static const std::string get_stack_repr() {
std::string tmp;
const auto stack_size = logger::prefix_stack.size();
tmp.append("[");
for (size_t i = 0; i < stack_size; i++) {
tmp.append(logger::prefix_stack[i]);
if (i != stack_size - 1) {
tmp.append("->");
}
}
tmp.append("]");
return tmp;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment