您好,欢迎来到九壹网。
搜索
您的当前位置:首页自定义日志库

自定义日志库

来源:九壹网

        暂时设计成这样,后续持续优化中:

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<fstream>
#include<string>
#include<mutex>
#include<direct.h>
#include<time.h>
#include<io.h>
#include<algorithm>
#include<condition_variable>
#include<cstdio>
#include "macro_define.h"
#include<stdarg.h>
#include<sstream>

typedef enum LOG_LEVEL {
	VERBOSE,
	DEBUG,
	INFO,
	WARN,
	ERROR
} LogLevel;

class LogWriter {
private:
	const int MAX_CACHE_FILE_COUNT = 1;
	static LogWriter *Writer; // 引用性声明
	std::mutex Mutex;
	std::string FileDir = "E:/fsi/log/";
	std::string FileName = "";
	std::string CurrTime = "";
	std::string FilePath = "";

	

public:
 	LogWriter() {
		CreateDirectory();
		CreateFileName();
		CreateFilePath();
	}

public:
	~LogWriter() {
		Close();
	}

public:
	const char* LogLevelToString(LogLevel l) {
		switch (l) {
		case VERBOSE:
			return "VERBOSE";
		case DEBUG:
			return "DEBUG";
		case INFO:
			return "INFO";
		case WARN:
			return "WARN";
		case ERROR:
			return "ERROR";
		default:
			return "UNKNOWN";
		}
	}

	static LogWriter* GetInstance() {
		if (Writer == nullptr) {
			Writer = new LogWriter;
		}
		return Writer;
	}

	LogWriter* SetFileDirectory(std::string fileDriectory) {
		FileDir.assign(fileDriectory);
		CreateDirectory();
		CreateFilePath();
		return this;
	}

	void Write(LogLevel level, std::string message, int cnt, ...) {
		Mutex.lock();
		if (GetLogFileCount() > MAX_CACHE_FILE_COUNT) {
			DeleteDirectoryFile();
		}

		std::ofstream file_out(FilePath, std::ios::out | std::ios::app);

		if (file_out.is_open()) {
			va_list args;
			// std::string arg;
			std::string arg;
			va_start(args, cnt);
			int len = 0;
			std::cout << args << std::endl;
			while (len < cnt) {
				arg = va_arg(args, std::string);
				len++;
				file_out << "[" << arg << "]";
				
			}
			va_end(args);
			file_out << GetWriteTime();
			file_out << message << std::endl;
			file_out.close();
		}
		
		Mutex.unlock();
	}

	void CreateFileName() {
		char now[255];
		time_t Today;
		tm* Time;

		time(&Today);
		Time = localtime(&Today);

		strftime(now, 255, "%Y%m%d", Time);
		if (CurrTime.empty() || CurrTime != now) {
			FileName.assign("log").append(now).append(".txt");
		}
	}

	std::string GetWriteTime() {
		char now[255];
		time_t Today;
		tm* Time;

		time(&Today);
		Time = localtime(&Today);

		strftime(now, 255, "[%Y/%m/%d %H:%M:%S]", Time);
		return now;
	}


	void CreateDirectory() {
		// 判断目录是否存在  
		if (_chdir(FileDir.c_str()) == 0) {
			_mkdir(FileDir.c_str());
		}
	}

	void CreateFilePath() {
		FilePath = FileDir + FileName;
	}

	int GetLogFileCount()
	{
		//文件句柄
		long   hFile = 0;
		//文件信息
		struct _finddata_t fileinfo;
		int file_count = 0;

		if ((hFile = _findfirst((FileDir + "*").c_str(), &fileinfo)) != -1) {
			while (_findnext(hFile, &fileinfo) == 0) {
				if (fileinfo.name[0] == '.' && fileinfo.name[1] == '.') {
					continue;
				}
				file_count++;
			}
			_findclose(hFile);
		}
		return file_count;
	}

	void DeleteDirectoryFile() {
		//文件句柄
		long   hFile = 0;
		//文件信息
		struct _finddata_t fileinfo;
		int file_count = 0;

		if ((hFile = _findfirst((FileDir + "*").c_str(), &fileinfo)) != -1) {
			while (_findnext(hFile, &fileinfo) == 0) {
				if (fileinfo.name[0] == '.' && fileinfo.name[1] == '.') {
					continue;
				}
				remove((FileDir + fileinfo.name).c_str());
			}
			_findclose(hFile);
		}
	}

	void Close() {
		delete Writer;
		Writer = nullptr;
	}
};

// 定义性声明
LogWriter * LogWriter::Writer; 
std::string Int_to_String(int n){
	std::ostringstream stream;
	// n为int类型
	stream << n;  
	return stream.str();
}

void main() {
	std::string auto_file = __FILE__;
	std::cout << __FILE__ << std::endl;
	LogWriter::GetInstance()->Write(LogLevel::VERBOSE, "wode未来不是梦1", 2, auto_file, "Line" + Int_to_String(__LINE__));
	LogWriter::GetInstance()->Write(LogLevel::DEBUG, "wode未来不是梦2", 2, auto_file, Int_to_String(__LINE__));
	LogWriter::GetInstance()->Write(LogLevel::INFO, "wode未来不是梦3", 2, auto_file, Int_to_String(__LINE__));
	LogWriter::GetInstance()->Write(LogLevel::WARN, "wode未来不是梦4", 2, auto_file, Int_to_String(__LINE__));
	LogWriter::GetInstance()->Write(LogLevel::ERROR, "wode未来不是梦5", 2, auto_file, Int_to_String(__LINE__));
	LogWriter::GetInstance()->Write(LogLevel::VERBOSE, "wode未来不是梦6", 2, auto_file, Int_to_String(__LINE__));
	LogWriter::GetInstance()->Write(LogLevel::DEBUG, "wode未来不是梦7", 2, auto_file, Int_to_String(__LINE__));

	system("pause");
	return;
}



因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 91gzw.com 版权所有 湘ICP备2023023988号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务