bwmodel
A modeling environment for Minecraft Bedwars
Loading...
Searching...
No Matches
logger.h
1// Copyright (C) 2023, 2024 Ethan Uppal. All rights reserved.
2
3#pragma once
4
5#include <iostream>
6#include <string>
7#include <filesystem>
8#include <cstdbool>
9
11class Logger {
12public:
13 struct LocationInfo {
14 std::string file;
15 int line;
16 std::string func;
17
18 LocationInfo() {}
19
20 LocationInfo(const std::string& file, int line, const std::string& func)
21 : file(file), line(line), func(func) {}
22 };
23
24private:
26 std::ostream& out;
27
29 bool has_location_info;
30
32 LocationInfo location_info;
33
34public:
36 static bool main_enabled;
37
39 static Logger& main() {
40 static Logger instance(std::cerr);
41 return instance;
42 }
43
45 Logger(std::ostream& out): out(out), has_location_info(false) {}
46
48 template<typename T>
49 inline Logger& operator<<(T val) {
50#if !defined(RELEASE_BUILD) && !defined(NO_LOGGING)
51 if (main_enabled) {
52 if (has_location_info) {
53 out << "[" << location_info.func << "] ";
54 has_location_info = false;
55 }
56 out << val;
57 }
58#endif
59 return *this;
60 }
61
63 inline Logger& operator<<(const LocationInfo& location_info) {
64 this->location_info = location_info;
65 this->location_info.file =
66 std::filesystem::path(location_info.file).filename().string();
67 has_location_info = true;
68 return *this;
69 }
70};
71
72// See Logger::main
73#define BW_Log \
74 Logger::main() << Logger::LocationInfo(__FILE__, __LINE__, __func__)
Definition logger.h:11
Logger & operator<<(const LocationInfo &location_info)
Definition logger.h:63
Logger(std::ostream &out)
Definition logger.h:45
static Logger & main()
Definition logger.h:39
static bool main_enabled
Definition logger.h:36
Logger & operator<<(T val)
Definition logger.h:49
Definition logger.h:13