GitHub上面下载的代码,挺好用,权当学习
#ifndef YLOG_YLOG_H_ #define YLOG_YLOG_H_ #include <string> #include <fstream> #include <cassert> #include <ctime> class YLog{ private: std::ofstream of_; int minlevel_; public: enum Type { ADD = 0, OVER }; enum Level { INFO = 0, ERR }; YLog(const int level, const std::string &logfile, const int type = YLog::OVER) : minlevel_(level) { assert((this->ERR == level || this->INFO == level) && "Logfile create failed, please check the level(YLog::ERR or YLog::INFO."); if (type == this->ADD) { this->of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::app); } else if (type == this->OVER) { this->of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::trunc); } else { assert(0 && "Logfile create failed, please check the type(YLog::OVER or YLog::ADD)."); } assert(this->of_.is_open() && "Logfile create failed, please check the logfile's name and path."); return; } ~YLog(){ if (this->of_.is_open()) { this->of_.close(); } return; } template<typename T> void W(const std::string &codefile, const std::string &functionName, const int codeline, const int level, const std::string &info, const T &value) { assert(this->of_.is_open() && "Logfile write failed."); if (this->ERR == level) { this->of_ << "[ERROR] "; } else if (this->INFO == level) { if (this->INFO == this->minlevel_) { this->of_ << "[INFO] "; } else { return; } } else { assert(0 && "Log write failed, please check the level(YLog::ERR or YLog::INFO."); } time_t sectime = time(NULL); tm tmtime; #ifdef _WIN32 #if _MSC_VER<1600 tmtime = *localtime(§ime); #else localtime_s(&tmtime, §ime); #endif #else localtime_r(§ime, &tmtime); #endif this->of_ << tmtime.tm_year + 1900 << '-' << tmtime.tm_mon + 1 << '-' << tmtime.tm_mday << ' ' << tmtime.tm_hour << ':' << tmtime.tm_min << ':' << tmtime.tm_sec << ' ' << codefile << "(L:" << codeline<<')'<<"-<" <<functionName << "> " << info << ":" << value << std::endl; return; } template<typename T> void W(const std::string &codefile, const int codeline, const int level, const std::string &info, const T &value) { assert(this->of_.is_open() && "Logfile write failed."); if (this->ERR == level) { this->of_ << "[ERROR] "; } else if (this->INFO == level) { if (this->INFO == this->minlevel_) { this->of_ << "[INFO] "; } else { return; } } else { assert(0 && "Log write failed, please check the level(YLog::ERR or YLog::INFO."); } time_t sectime = time(NULL); tm tmtime; #ifdef _WIN32 #if _MSC_VER<1600 tmtime = *localtime(§ime); #else localtime_s(&tmtime, §ime); #endif #else localtime_r(§ime, &tmtime); #endif this->of_ << tmtime.tm_year + 1900 << '-' << tmtime.tm_mon + 1 << '-' << tmtime.tm_mday << ' ' << tmtime.tm_hour << ':' << tmtime.tm_min << ':' << tmtime.tm_sec << ' ' << codefile << '(' << codeline << ") " << info << ":\n" << value << std::endl; return; } }; #endif // YLOG_YLOG_H_