daiki8は片付けができない

片付けしないと

ログクラスを作る その2

ファイル出力できるようにする

main.cpp

#include "log.h"

int main() {
	const std::wstring path = L".\\";
	const std::wstring name = L"test.txt";

	static Log &log = Log::GetInstance(path, name);

	log.OutFile(L"Hello!");

	return 0;
}

Log.h

#include <string>

class Log
{
public:
	static Log &GetInstance(
		const std::wstring path,
		const std::wstring filename
	);

	void OutFile(
		const std::wstring msg
	);

private: // コンストラクタデストラクタコピーコンストラクタ代入はプライベートで定義
	Log(
		const std::wstring path,
		const std::wstring filename
	);
	~Log(){};
	Log(const Log &other) {};
	Log& operator=(const Log &other) {};

private:
	std::wstring m_file_location;

};

Log.cpp

#include "Log.h"
#include <fstream>

Log &Log::GetInstance(
	const std::wstring path,
	const std::wstring name
) {
	static Log instance(path, name);  // 一度だけLogインスタンスを生成する
	return instance;
}

Log::Log(
	const std::wstring path,
	const std::wstring name
) {
	m_file_location = path;
	m_file_location += name;
}

void Log::OutFile(
	const std::wstring msg
) {
	std::wofstream wfs; // wstring用filestreamの型
	wfs.open(m_file_location, std::ios::app);
	wfs << msg << std::endl;
}