跳到正文

个人记录

日志类

MineCraftLittlePea约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月5日
篇幅
约 3 分钟

版本

  1. v1当前
C++
/*
ABOUT logger.h
this headfile includes:
class Logger { // helps you to logging!
	Logger(int m); Init the logger with m(ode),if you don't write this,it will be 0.

	void setmode(int m); Set the mode to m(ode),it will throw an error if m out of the range.

	int getmode(); Returns the id of the current mode.

	void switchLogging(); Switch the logging sign(yes to no,no to yes).

	bool isLogging(); Returns if it is logging.

	void sendMessage(string msg); Send a message likes XXX:XXX.

	void sendMessage(string msg,string moreInfo); Send a message with more information likes XXX:XXX(XXX).
}
*/
#include <bits/stdc++.h>
class Logger {
	public :
		//constructor
		Logger(int);

		//settings
		void setmode(int m) {
			if (0 <= m && m < ModeCnt) _mode = m;
			else throw std::runtime_error("Invaild mode id");
		}
		int getmode() const {
			return _mode;
		}
		void switchLogging() {
			_openLogging = !_openLogging;
		}
		bool isLogging() {
			return _openLogging;
		}

		//logging
		void sendMessage(std::string msg) {
			if (!_openLogging) return ;
			std::cout << _nams[_mode] << ":" << msg << "\n";
		}
		void sendMessage(std::string msg,std::string moreInfo) {
			if (!_openLogging) return ;
			std::cout << _nams[_mode] << ":" << msg << "(" << moreInfo << ")" << "\n";
		}
	protected:
		const int ModeCnt = 4;
		const std::vector<std::string> _nams = {"info","tip","warning","dangerous"};
	private:
		int _mode;
		/*
		id for _nams
		*/
		bool _openLogging;
		/*
		true:logging
		false:do not logging
		*/
};
Logger::Logger(int m = 0) {
	this->_mode = m;
	this->_openLogging = true;
}
原文