C++实现控制台输出具有颜色类
2016-09-27 22:43
465 查看
直接贴代码:.h文件
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>
如你有更好办法,请指教。
这个 颜色设置主要是在开发项目时候经常使用到的。里面还有要添加写日志到文件中的功能还没写......
未完待续。。。
#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>
如你有更好办法,请指教。
这个 颜色设置主要是在开发项目时候经常使用到的。里面还有要添加写日志到文件中的功能还没写......
未完待续。。。
相关文章推荐
- C/C++实现控制台输出不同颜色字体的方法
- C/C++控制台输出时设置字体及背景颜色
- C/C++控制台输出时设置字体及背景颜色
- C++更改控制台输出颜色
- C++控制台输出字体颜色字体背景色
- C/C++控制台带颜色功能输出
- 改变C++控制台输出颜色
- C/C++控制台输出不同颜色的字体
- Windows下更改C/C++控制台程序文本输出颜色
- C/C++控制台输出时设置字体及背景颜色
- C++向控制台输出带颜色的字体
- C语言实现控制台输出背景颜色字体颜色变化
- C/C++控制台输出时设置字体及背景颜色
- C++ 设置控制台输出颜色
- C/C++控制台带颜色功能输出
- C/C++控制台输出时设置字体及背景颜色
- (百度空间)C/C++控制台输出时设置字体及背景颜色
- [转]C/C++控制台输出时设置字体及背景颜色
- C/C++控制台输出时设置字体及背景颜色
- 【C++程序】[新手向]控制台输出颜色变换特效