直接贴代码:.h文件
main调用
因为我使用了(level&m_curColor) 这个来设置颜色值 所以有些颜色不能使用,如果你有需要你可以重新改变下我的设置方式,具体颜色值如下
#pragma once
#include "config.h"
#define BLACK 0
#define GREEN 2
#define RED 4
#define YELLOW 8
#ifdef WIN32
#include<windows.h>
#endif // WIN32
class CLog
{
public:
~CLog();
static void must(const char* msg);
static void must(std::string msg);
static void warn(const char* msg);
static void warn(std::string msg);
static void error(const char* msg);
static void error(std::string msg);
protected:
static inline void setConsoleColor(int level)
{
#ifdef WIN32
(level&m_curColor) || (m_curColor = level, SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), m_curColor | FOREGROUND_INTENSITY));
#endif
}
private:
CLog();
static int m_curColor;
};
cpp文件
#include "log.h"
int CLog::m_curColor = BLACK;
CLog::CLog()
{
}
CLog::~CLog()
{
}
void CLog::must(const char* msg)
{
setConsoleColor(GREEN);
std::cout << msg << std::endl;
}
void CLog::must(std::string msg)
{
must(msg.c_str());
}
void CLog::warn(const char* msg)
{
setConsoleColor(YELLOW);
std::cout << msg << std::endl;
}
void CLog::warn(std::string msg)
{
warn(msg.c_str());
}
void CLog::error(const char* msg)
{
setConsoleColor(RED);
std::cout << msg << std::endl;
}
void CLog::error(std::string msg)
{
error(msg.c_str());
}
main调用
#include"log.h"
int main()
{
CLog::warn("please attention this is warn");
CLog::warn("please attention this is warn");
CLog::must("please attention this is must");
CLog::error("please attention this is error");
getchar();
return 0;
}
因为我使用了(level&m_curColor) 这个来设置颜色值 所以有些颜色不能使用,如果你有需要你可以重新改变下我的设置方式,具体颜色值如下
0=黑色
1=蓝色
2=绿色
3=湖蓝色
4=红色
5=紫色
6=黄色
7=白色
8=灰色
9=淡蓝色
A=淡绿色
B=淡浅绿色
C=淡红色
D=淡紫色
E=淡黄色
F=亮白色
其中FOREGROUND_INTENSITY代表前景高亮,config.h头文件内容如下
#define WIN32
#include <string>
#include <iostream>
如你有更好办法,请指教。
这个 颜色设置主要是在开发项目时候经常使用到的。里面还有要添加写日志到文件中的功能还没写......
未完待续。。。