代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
打印调试信息的宏
作者:
LSGX
/ 发布于
2015/1/22
/
672
/* ************************************************************************ * Filename: debug_pos.h * Description: Debugging error message positioning * Version: 1.0 * Created: 2014-11-14 22:55:26 * Revision: none * Compiler: gcc * Author: lsgx * Company: lsgx * ************************************************************************/ #ifndef __DEBUG_POS_H__ #define __DEBUG_POS_H__ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <ctype.h> #include <sys/types.h> #include <time.h> enum DEBUG_LEVEL_ENUM { DEBUG_EMERG = 0, DEBUG_ALERT, DEBUG_CRIT, DEBUG_ERR, DEBUG_WARNING, DEBUG_NOTICE, DEBUG_INFO, DEBUG_DEBUG }; #define DEBUG_EMERG_TITLE "DEBUG_EMERG" /* system is unusable */ /* 紧急事件消息,系统崩溃之前提示,表示系统不可用 */ #define DEBUG_ALERT_TITLE "DEBUG_ALERT" /* action must be taken immediately */ /* 报告消息,表示必须立即采取措施 */ #define DEBUG_CRIT_TITLE "DEBUG_CRIT" /* critical conditions */ /* 临界条件,通常涉及严重的硬件或软件操作失败 */ #define DEBUG_ERR_TITLE "DEBUG_ERR" /* error conditions */ /* 错误条件,驱动程序常用KERN_ERR来报告硬件的错误 */ #define DEBUG_WARNING_TITLE "DEBUG_WARNING" /* warning conditions */ /* 警告条件,对可能出现问题的情况进行警告 */ #define DEBUG_NOTICE_TITLE "DEBUG_NOTICE" /* normal but significant condition */ /* 正常但又重要的条件,用于提醒 */ #define DEBUG_INFO_TITLE "DEBUG_INFO" /* informational */ /* 提示信息,如驱动程序启动时,打印硬件信息 */ #define DEBUG_DEBUG_TITLE "DEBUG_DEBUG" /* debug-level messages */ /* 调试级别的消息 */ #define MAX_TITLE_LEN 20 #define MIN_TIME_LEN 20 #define MAX_DEBUG_LEVEL DEBUG_DEBUG #define MIN_DEBUG_LEVEL DEBUG_EMERG #define MAX_DEBUG_COUNT (MAX_DEBUG_LEVEL - MIN_DEBUG_LEVEL + 1) #define DEBUG_POS_LOG "debug_pos.log" #define DEBUG_SER_NAME "DEBUG" #define DEBUG_KILL_STAT 1 #define LIMIT_KILL_TITLE DEBUG_ERR_TITLE #define LIMIT_KILL_LEVEL DEBUG_ERR static struct DEBUG_STR { enum DEBUG_LEVEL_ENUM level; const char title[MAX_TITLE_LEN]; } DEBUG_STR_ITEM[MAX_DEBUG_COUNT] = { {DEBUG_EMERG, DEBUG_EMERG_TITLE}, {DEBUG_ALERT, DEBUG_ALERT_TITLE}, {DEBUG_CRIT, DEBUG_CRIT_TITLE}, {DEBUG_ERR, DEBUG_ERR_TITLE}, {DEBUG_WARNING, DEBUG_WARNING_TITLE}, {DEBUG_NOTICE, DEBUG_NOTICE_TITLE}, {DEBUG_INFO, DEBUG_INFO_TITLE}, {DEBUG_DEBUG, DEBUG_DEBUG_TITLE} }; static int system_digital_time(char * time_str, size_t str_len) { if (str_len < MIN_TIME_LEN) { perror("time_str buffer size to low\n"); return -1; } time_t now = time((time_t *)NULL); struct tm * time_now = localtime(&now); memset(time_str, '\0', str_len); snprintf(time_str, str_len, "%4d-%02d-%02d %02d:%02d:%02d", time_now->tm_year + 1900, time_now->tm_mon + 1, time_now->tm_mday, time_now->tm_hour, time_now->tm_min, time_now->tm_sec); return (strlen(time_str)); } static int get_process_name(char *process_name, size_t name_len) { int ret = -1; FILE *fp = NULL; char cmd[32] = {'\0'}; char *buffer = NULL; long int len = -1; int pid_real = -1; int pid_cmp = -1; pid_real = getpid(); memset(cmd, '\0', sizeof(cmd)); snprintf(cmd, sizeof(cmd)-1, "ps -wax | grep %d", pid_real); fp = popen(cmd, "r"); if (fp == NULL) { perror("popen fail!\n"); ret = -1; goto OUT; } len = 510; buffer = (char *)malloc(sizeof(char) * (len + 2)); if (buffer == NULL) { perror("malloc fail!\n"); ret = -1; goto OUT; } memset(buffer, '\0', len + 2); while(fgets(buffer, len + 2, fp) != NULL) { if (sscanf(buffer, "%d", &pid_cmp) == EOF) { perror("sscanf pid_cmp fail!\n"); ret = -1; goto OUT; } if (pid_real == pid_cmp) { char *temp1 = buffer; char *temp2 = NULL; while((temp2 = strchr(temp1, ' ')) != NULL) { temp1 = temp2 + 1; } if (name_len < strlen(temp1)) { perror("process_name size to low!\n"); ret = -1; goto OUT; } strncpy(process_name, temp1, strlen(temp1) - 1); ret = strlen(process_name); break; } pid_cmp = -1; memset(buffer, '\0', len + 1); } OUT: if (buffer != NULL) { free(buffer); buffer = NULL; } if (fp != NULL) { pclose(fp); fp = NULL; } return ret; } static unsigned long int debug_count = 0; // #define DEBUG_POS_HIDE #ifndef DEBUG_POS_HIDE #define DEBUG_POS(debug_level, ...) do { \ if (debug_level >= MIN_DEBUG_LEVEL && debug_level <= MAX_DEBUG_LEVEL) { \ unsigned int debug_exit_stat = 0; \ FILE * debug_pos_fp = NULL; \ debug_pos_fp = fopen(DEBUG_POS_LOG, "a+"); \ char time_str[20] = {'\0'}; \ system_digital_time(time_str, sizeof(time_str)); \ char process_name[20] = {'\0'}; \ get_process_name(process_name, sizeof(process_name)); \ debug_count++; \ fprintf(debug_pos_fp, "[***|%s|***] [title: %s, time: %s, count: %lu]\n\t[file_name: %s, function_name: %s, line_no: %d]\n\t>>>>", \ process_name, DEBUG_STR_ITEM[debug_level].title, time_str, debug_count, __FILE__, __FUNCTION__, __LINE__); \ fprintf(debug_pos_fp, __VA_ARGS__); \ fputc('\n', debug_pos_fp); \ if (debug_level <= LIMIT_KILL_LEVEL) { \ fprintf(debug_pos_fp, "%s", "\t>>>>******|debug exit abort process|******\n"); \ debug_exit_stat = DEBUG_KILL_STAT; \ } \ fclose(debug_pos_fp); \ if (debug_exit_stat == DEBUG_KILL_STAT) { \ abort(); \ } \ } \ } while(0) #else #define DEBUG_POS(debug_level, ...) NULL #endif // DEBUG_POS_HIDE #define DEBUG_ASSERT(condition, ...) do { \ if (condition) { \ NULL; \ } else { \ DEBUG_POS(DEBUG_ERR, __VA_ARGS__); \ } \ } while(0) #endif // __DEBUG_POS_H__ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> // #define DEBUG_POS_HIDE #include "debug_pos.h" int main(int argc, char *argv[]) { DEBUG_POS(DEBUG_INFO, "调试打印消息"); DEBUG_POS(DEBUG_WARNING, "调试警告消息"); DEBUG_ASSERT(1, "断言成功消息"); DEBUG_ASSERT(0, "断言失败消息"); return 0; }
试试其它关键字
打印
宏
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
LSGX
贡献的其它代码
(
1
)
.
打印调试信息的宏
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3