个人技术分享

1.使用函数的方法,只能打印

只需要包含:#include <cstdio>头文件即可

#define INFO_LOG(fmt, ...) fprintf(stdout, "[INFO]  " fmt "\n", ##__VA_ARGS__)
#define WARN_LOG(fmt, ...) fprintf(stdout, "[WARN]  " fmt "\n", ##__VA_ARGS__)
#define ERROR_LOG(fmt, ...) fprintf(stdout, "[ERROR] " fmt "\n", ##__VA_ARGS__)

使用方法:

INFO_LOG("open device %d success", deviceId_);

 ERROR_LOG("set op wait time failed");

2.更高级的用法,可以设置日志等级,输出到文件

(1)头文件:Log.h

#ifndef __CLOG__
#define __CLOG__
//#include <windows.h>
#include <string>
#include <fstream>
//#include <tchar.h>
#ifdef _MSC_VER
#include <ctime>
#else
#include <sys/time.h>
#endif

//#define LOGLEVEL 4

enum LogLevel {
    LOG0 = 0, // 0-不输出日志
    LOG1,
    LOG2,
    LOG3,
    LOG4
};

class CLog{

public:  
    void WriteLogFormat(int level, const char* format, ...);
    void initLog(char *versionBuf);
    void close();
private:
    static std::string GetFilePath();
    static std::string GetSystemTimes(