myLog.h
#ifndef __myLog_H_ #define __myLog_H_ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <iostream> #include <ratio> #include <chrono> #include <mutex> std::string GetName(char * fileName); std::string getCurrentSystemTime(); long long getCurrentMs(); // 初始化路徑 int LOGINIT(char *path); // 結束時候調用 int LOGEnd(); // 設置最大日志文件,默認5M, mSize單位是字節 int LOGSetMaxSpace(int mSize); int logWrite(char * s); #define LOGE(...) {\ char temp[8192] = {0}; \ sprintf(temp, "%s", getCurrentSystemTime().c_str()); \ sprintf(temp+strlen(temp), " %s %d ", GetName(__FILE__).c_str(), __LINE__); \ sprintf(temp+strlen(temp), __VA_ARGS__); \ sprintf(temp + strlen(temp), "\n"); \ printf("%s", temp); \ logWrite(temp); \ } #endif
myLog.cpp
#include "myLog.h" std::mutex mux; static FILE * fp = NULL; int MaxLenChar = 1024 * 1024 * 5; // 5 M 數據 int LOGINIT(char *path) { fp = fopen(path, "w+"); if (fp == NULL) { return -1; } return 0; } int LOGSetMaxSpace(int mSize) { MaxLenChar = mSize; return 0; } int logWrite(char * s) { if (fp) { int len = ftell(fp); if (len > MaxLenChar) { LOGEnd(); return 0; } mux.lock(); fprintf(fp, "%s", s); fflush(fp); mux.unlock(); } return 0; } int LOGEnd() { if (fp) { fflush(fp); fclose(fp); fp = 0; } return 0; } std::string getCurrentSystemTime() { auto time_now = std::chrono::system_clock::now(); auto tt = std::chrono::system_clock::to_time_t(time_now); auto duration_in_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_now.time_since_epoch()); auto duration_in_s = std::chrono::duration_cast<std::chrono::seconds>(time_now.time_since_epoch()); int theMs = duration_in_ms.count() - duration_in_s.count() * 1000; struct tm* ptm = localtime(&tt); char date[60] = { 0 }; sprintf(date, "%d-%02d-%02d-%02d.%02d.%02d.%03d%", (int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday, (int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec, theMs); return std::string(date); } long long getCurrentMs() { auto time_now = std::chrono::system_clock::now(); auto duration_in_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_now.time_since_epoch()); return duration_in_ms.count(); } std::string GetName(char * fileName) { char temp[256] = { 0 }; memcpy(temp, fileName, strlen(fileName) + 1); std::string sName = temp; #ifdef WIN32 int startP = sName.find_last_of("\\"); if (startP == -1) { startP = 0; } std::string theLastName = sName.substr(startP + 1, sName.size() - startP); #else int startP = sName.find_last_of("/"); if (startP == -1) { startP = 0; } std::string theLastName = sName.substr(startP + 1, sName.size() - startP); #endif return theLastName; }
mian.cpp
#include<stdio.h> #include "myLog.h" LOGINIT("mylog.log") int main() { int age=18; char name[10]="fly"; LOGE("My name is %s,I am %d years old.", name,age); LOGEnd(); return 0; }