debug打印,后台跑,telnet上去也能看
2016-05-23 11:36
183 查看
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <errno.h> #define NONE "\033[m" #define RED "\033[0;32;31m" #define LIGHT_RED "\033[1;31m" #define GREEN "\033[0;32;32m" #define LIGHT_GREEN "\033[1;32m" #define BLUE "\033[0;32;34m" #define LIGHT_BLUE "\033[1;34m" #define DARY_GRAY "\033[1;30m" #define CYAN "\033[0;36m" #define LIGHT_CYAN "\033[1;36m" #define PURPLE "\033[0;35m" #define LIGHT_PURPLE "\033[1;35m" #define BROWN "\033[0;33m" #define YELLOW "\033[1;33m" #define LIGHT_GRAY "\033[0;37m" #define WHITE "\033[1;37m" #define SIZE (1024) typedef struct { long int type; char buffer[SIZE]; }Message; int debug_print(const char* format, ...); int debug_recv(Message* recv); char* debug_time_str(); #define DBG(fmt, ...) do\ {\ debug_print(GREEN"%s %d debug %s: %d: "NONE""fmt"", debug_time_str(), getpid(), __FUNCTION__, __LINE__, ##__VA_ARGS__);\ }while(0) #define ERR(fmt, ...) do\ {\ debug_print(RED"%s %d error %s: %d: "NONE""fmt"", debug_time_str(), getpid(), __FUNCTION__, __LINE__, ##__VA_ARGS__);\ }while(0) #define INFO(fmt, ...) do\ {\ debug_print(YELLOW"%s %d info %s: %d: "NONE""fmt"", debug_time_str(), getpid(), __FUNCTION__, __LINE__, ##__VA_ARGS__);\ }while(0) #define CHECK(exp, ret, fmt...) do\ {\ if (!(exp))\ {\ ERR(fmt);\ return ret;\ }\ }while(0)
#include <sys/time.h> #include <time.h> #include <sys/types.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/msg.h> #include "debug.h" #define KEY (0x23581321) #define TYPE (1) #define MAXSENDSIZE (sizeof(Message)-sizeof(long int)) typedef struct { int inited; int msgid; long int type; pthread_mutex_t mutex; }msg_s; static msg_s msg; char* debug_time_str() { static char time[32] = ""; struct timeval tv = {0, 0}; gettimeofday(&tv, NULL); strftime(time, sizeof(time), "%F %H:%M:%S", localtime(&tv.tv_sec)); return time; } static int debug_init() { memset(&msg, 0, sizeof(msg)); msg.type = TYPE; pthread_mutex_init(&msg.mutex, NULL); msg.msgid = msgget(KEY, 0666|IPC_CREAT); if (msg.msgid == -1) { perror("create_msg failed!\n"); return -1; } msg.inited = 1; return 0; } int debug_print(const char* format, ...) { if (!msg.inited) { debug_init(); } pthread_mutex_lock(&msg.mutex); static Message send_buf; memset(&send_buf, 0, sizeof(send_buf)); send_buf.type = msg.type; va_list ap; va_start(ap, format); vsnprintf(send_buf.buffer, sizeof(send_buf.buffer), format, ap); va_end(ap); fprintf(stdout, "%s", send_buf.buffer); msgsnd(msg.msgid, &send_buf, MAXSENDSIZE, IPC_NOWAIT); pthread_mutex_unlock(&msg.mutex); return 0; } int debug_recv(Message* recv) { if (!msg.inited) { debug_init(); } pthread_mutex_lock(&msg.mutex); memset(recv, 0, sizeof(*recv)); msgrcv(msg.msgid, recv, MAXSENDSIZE, msg.type, 0); pthread_mutex_unlock(&msg.mutex); return 0; }
#include "debug.h" #include <pthread.h> void* proc(void* arg) { int count = 1000; while(1) { INFO("test count: %d\n", count++); usleep(100*1000); } return NULL; } int main() { int count = 0; pthread_t pid = 0; pthread_create(&pid, NULL, proc, NULL); while(1) { DBG("test count: %d\n", count++); ERR("test count: %d\n", count++); //INFO("test count: %d\n", count++); usleep(100*1000); } return 0; }
#include "debug.h" int main(int argc, char** argv) { if (argc > 2) { printf("invalid args.argc=%d\n", argc); return 0; } Message recv; while(1) { debug_recv(&recv); if (argc == 2) { if (strstr(recv.buffer, argv[1])) { fprintf(stdout, "%s", recv.buffer); } } else { fprintf(stdout, "%s", recv.buffer); } } return 0; }
相关文章推荐
- 运用PowerPoint的3种视图
- 第一篇博客,留作Markdown编辑器的使用指南吧:D
- Rsync常见问题解决方法
- linux 获得当前进程对应的可执行文件的绝对路径
- tomcat添加登录用户名密码
- ScrollView禁用滑动
- Android进阶2之Activity之间数据交流(onActivityResult的用法)
- 常用的C语言定义
- 异步任务+JSON解析+ListView分页
- 为什么Redis内存不宜过大
- 死锁(deadlocks)
- View坐标分析汇总
- 仿照天猫搜索栏
- linux基础(23)--LVM快照--RHEL6.5
- 学习方法反思
- 浅谈Session
- Android Studio apk 打包流程
- Retrofit2的get请求和post请求
- Uboot启动过程详解
- Linux查看日志相关的命令