Linux彩色输出

在linux下,可以使用一些宏,加上自定义格式输出,让输出更易于调试:

排版出来可能有些乱,注意do{ }while(0);是在一行里就可以了。

[cpp] view plain copy

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define DEBUG1( fmt,  arg  ... )  \
  4. do{printf("[DEBUG] " fmt ,  ##arg );  }while(0);
  5. #define DEBUG2( fmt,  arg  ... ) \
  6. do{printf("[%s: %s: line %d]" fmt ,\
  7. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  8. #define PrintColor1( fmt,  arg  ... )   \
  9. do{printf("\033[30m""[%s: %s: line %d]" fmt"\033[0m" ,\
  10. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  11. #define PrintColor2( fmt,  arg  ... )   \
  12. do{printf("\033[31m""[%s: %s: line %d]" fmt"\033[0m" ,\
  13. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  14. #define PrintColor3( fmt,  arg  ... )   \
  15. do{printf("\033[32m""[%s: %s: line %d]" fmt"\033[0m" ,\
  16. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  17. #define PrintColor4( fmt,  arg  ... )   \
  18. do{printf("\033[33m""[%s: %s: line %d]" fmt"\033[0m" ,\
  19. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  20. #define PrintColor5( fmt,  arg  ... )   \
  21. do{printf("\033[34m""[%s: %s: line %d]" fmt"\033[0m" ,\
  22. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  23. #define PrintColor6( fmt,  arg  ... )   \
  24. do{printf("\033[35m""[%s: %s: line %d]" fmt"\033[0m" ,\
  25. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  26. #define PrintColor7( fmt,  arg  ... )    \
  27. do{printf("\033[36m""[%s: %s: line %d]" fmt"\033[0m" ,\
  28. __FILE__, __FUNCTION__, __LINE__,  ##arg );  }while(0);
  29. int main()
  30. {
  31. printf("I AM IN macro.c\n");
  32. DEBUG1("I AM IN macro.c\n");
  33. DEBUG2("I AM IN macro.c\n");
  34. PrintColor1("I AM IN macro.c\n");
  35. PrintColor2("I AM IN macro.c\n");
  36. PrintColor3("I AM IN macro.c\n");
  37. PrintColor4("I AM IN macro.c\n");
  38. PrintColor5("I AM IN macro.c\n");
  39. PrintColor6("I AM IN macro.c\n");
  40. PrintColor7("I AM IN macro.c\n");
  41. return 0;
  42. }

输出结果:

http://blog.csdn.net/xiangpingli/article/details/7914133
时间: 2024-08-01 16:51:04

Linux彩色输出的相关文章

SpringBoot之彩色输出

spring.output.ansi.enabled NEVER:禁用ANSI-colored输出(默认项) DETECT:会检查终端是否支持ANSI,是的话就采用彩色输出(推荐项) ALWAYS:总是使用ANSI-colored格式输出,若终端不支持的时候,会有很多干扰信息,不推荐使用   这个属性的值在 AnsiOutput.Enabled 这个的 ENUM 类中,也就是一个内部类,来看看源码: public static enum Enabled {DETECT, ALWAYS, NEVE

通过printf设置Linux终端输出的颜色和显示方式

前言 在Linux终端下调试程序时,有时需要输出大量信息.若能控制字体的颜色和显示方式,可使输出信息对比鲜明,便于调试时观察数据. 终端的字符颜色由转义序列(Escape Sequence)控制,是文本模式下的系统显示功能,与具体语言无关. 本文简要介绍C语言中通过printf改变终端输出的颜色和显示方式.文中涉及的代码运行环境如下: 正文 转义序列以控制字符'ESC'开头.该字符的ASCII码十进制表示为27,十六进制表示为0x1B,八进制表示为033.多数转义序列超过两个字符,故通常以'ES

linux 终端输出颜色

在Linux终端下调试程序时,有时需要输出大量信息.若能控制字体的颜色和显示方式,可使输出信息对比鲜明,便于调试时观察数据. 终端的字符颜色由转义序列(Escape Sequence)控制,是文本模式下的系统显示功能,与具体语言无关. 转义序列以控制字符'ESC'开头.该字符的ASCII码十进制表示为27,十六进制表示为0x1B,八进制表示为033.多数转义序列超过两个字符,故通常以'ESC'和左括号'['开头.该起始序列称为控制序列引导符(CSI,Control Sequence Intro)

c++ linux下输出中文

同样,使用的是VS FOR LINUX进行测试. converting to execution character set: Invalid or incomplete multibyte or wide character 如果编译时候遇到该错误,则可以加上-finput-charset  -fexecute-charset  g++编译选项解决.因为linux下gcc希望源文件是UTF-8格式,所以都改成UTF-8就好了.同时,也可以vs下装个forceUTF8插件. 搜了下,网上有说使用

Python实现Windows CMD命令行彩色输出

#! /usr/bin/env python #coding=utf-8 import ctypes,sys STD_INPUT_HANDLE = -10 STD_OUTPUT_HANDLE = -11 STD_ERROR_HANDLE = -12 # 字体颜色定义 ,关键在于颜色编码,由2位十六进制组成,分别取0~f,前一位指的是背景色,后一位指的是字体色 #由于该函数的限制,应该是只有这16种,可以前景色与背景色组合.也可以几种颜色通过或运算组合,组合后还是在这16种颜色中 # Window

Python实现Windows CMD命令行彩色输出(由网友提供)

# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import ctypes,sys STD_INPUT_HANDLE=-10 STD_OUTPUT_HANDLE=-11 STD_ERRIR_HANDLE=-12 #定义字体颜色,由于颜色编码,是由2位十六进制组成,分别娶0~f,前一位是指背景色,后一位是字体色 #因为由于函数限制,只有16种颜色,可以进行把前和后景色组合,也可以把其他颜色混合 # Windows 

linux 输入--输出--重定向 stdin/stdout/stderr

重定向 shell编辑文本 图形化工具编辑文件   1.重定向     标准输入     标准输出     错误输出 [[email protected] kkk]# cat aaa.sh #!/bin/bash a=1 while [ $a -le 5 ] do useradd user$a let a++ done [[email protected] kkk]# ./aaa.sh [[email protected] kkk]# tail /etc/passwd tcpdump:x:72:

[Linux]Linux printf 输出重定向【转】

转自:http://www.cnblogs.com/aaronLinux/p/6765145.html?utm_source=itdadao&utm_medium=referral 方法一 #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> int main() { fflush(stdout

【linux】输出重定向

“>”:把正确结果输出到一个文件 [[email protected] ~]# ls > 1 [[email protected] ~]# cat 1 1 anaconda-ks.cfg install.log install.log.syslog shell “>>”:把正确结果追加输出到一个文件 [[email protected] ~]# ls >>1 “&”把错误和正确结果都输出 [[email protected] ~]# lsd &>&