zlog学习笔记(zc_profile)
zc_profile.h
#ifndef __zlog_profile_h
#define __zlog_profile_h #define EMPTY() #define zc_assert(expr, rc) \
if(!(expr)){ \
zc_error(#expr"is null or 0"); \
return rc; \
} /**
* 枚举类型,列举出所有profile支持的flag
*/
enum zc_profile_flag{
ZC_DEBUG = ,
ZC_WARN = ,
ZC_ERROR =
}; enum test {
A = ,
B = ,
C =
}; #define zc_debug(...) \
zc_profile_inner(ZC_DEBUG, __FILE__, __LINE__, __VA_ARGS__) #define zc_warn(...) \
zc_profile_inner(ZC_WARN, __FILE__, __LINE__, __VA_ARGS__) #define zc_error(...) \
zc_profile_inner(ZC_ERROR, __FILE__, __LINE__, __VA_ARGS__) int zc_profile_inner(int flag,
const char *file, const long line,
const char *fmt, ...); #endif
zc_profile.c
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h> #include "zc_profile.h" /**
* 获取当前时间字符串
*/
static void zc_time(char *time_str, size_t time_str_size){
time_t tt;
struct tm local_time; //获取当前系统时间戳
time(&tt);
//使用当前时间戳填充tm结构体, 相比localtime,线程安全
localtime_r(&tt, &local_time);
strftime(time_str, time_str_size, "%m-%d %T", &local_time); return ;
} int zc_profile_inner(int flag, const char *file, const long line, const char *fmt, ...){
va_list args;
char time_str[ + ];
FILE *fp = NULL; static char *debug_log = NULL;
static char *error_log = NULL;
//记录debug、error是否被初始化过
static size_t init_flag = ; if(!init_flag){
init_flag = ;
//stdlib getenv 获取环境变量
debug_log = getenv("ZLOG_PROFILE_DEBUG");
error_log = getenv("ZLOG_PROFILE_ERROR");
} switch(flag){
case ZC_DEBUG:
if(debug_log == NULL){
return ;
}
//以附加的方式打开只写文件
fp = fopen(debug_log, "a");
if(!fp){
return -;
}
zc_time(time_str, sizeof(time_str));
//将格式化的字符写入文件
/**
* 将格式化的字符写入文件
* sys/types.h
* unistd.h
* pid_t getpid(void)
*/
fprintf(fp, "%s DEBUG (%d:%s:%ld) ", time_str, getpid(), file, line);
break;
case ZC_WARN:
if(error_log == NULL){
return ;
}
fp = fopen(debug_log, "a");
if(!fp){
return -;
}
zc_time(time_str, sizeof(time_str));
fprintf(fp, "%s WARN (%d:%s:%ld) ", time_str, getpid(), file, line);
break;
case ZC_ERROR:
if(error_log == NULL){
return ;
}
fp = fopen(debug_log, "a");
if(!fp){
return -;
}
zc_time(time_str, sizeof(time_str));
fprintf(fp, "%s ERROR (%d:%s:%ld) ", time_str, getpid(), file, line);
break;
} va_start(args, fmt);
vfprintf(fp, fmt, args);
va_end(args);
fprintf(fp, "\n"); fclose(fp);
return ;
}
zlog学习笔记(zc_profile)的更多相关文章
- zlog学习笔记(mdc)
mdc.h #ifndef __zlog_mdc_h #define __zlog_mdc_h #include "zc_defs.h" typedef struct zlog_m ...
- zlog学习笔记(level_list)
level_list.h /** * */ #ifndef __zlog_level_list_h #define __zlog_level_list_h zc_arraylist_t *zlog_l ...
- zlog学习笔记(level)
level.h /** * */ #ifndef __zlog_level_h #define __zlog_level_h #include "stdio.h" #include ...
- zlog学习笔记(zc_hashtable)
zc_hashtable.h /** * hashtable */ #ifndef __zc_hashtable_h #define __zc_hashtable_h typedef struct z ...
- zlog学习笔记(zc_arraylist)
zc_arraylist.h /** * 实现类似列表的功能 * */ #ifndef __zc_arraylist_h #define __zc_arraylist_h #define ARRAY_ ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
随机推荐
- iOS 图片加载导致内存警告
虽然UITableView和UICollectionView都有cell复用机制,但是如果利用SDWebImage加载的图片本身过大且cell复用池中的个数比较多(cell的Size越小,复用池中的c ...
- 深入.net(多态一)
代码优化技术: 当您在 编写一个类时,如果您发现你需要编写的“属性”和“方法”曾经在已有的类中实现,则,您可以将其共用的“属性”和“方法”剪切到一个新的“类”中,然后,让两个类共同继承这个“新类”.( ...
- 【代码笔记】iOS-调用系统震动和声音
代码: RootViewController.m #import "RootViewController.h" #import <AudioToolbox/AudioTool ...
- UI入门指引
1. iOS学习路线: C语言:数据类型.流程控制.函数.指针.字符串.结构体.枚举.预处理: OC:面向对象.内存管理.分类.协议.Block.KVC/KVO.Foundation框架: iOS基础 ...
- iOS 9 使用HTTP的方法
问题 在ios 9中使用HTTP请求,Xcode就会抛出下面的Exception: App Transport Security has blocked a cleartext HTTP (http: ...
- ruby + watir 自动化上传图片文件解决方案
watir自动化捕获上传图片元素: require 'watir' include Watir require 'test/unit' class TC_recorded < Test::Uni ...
- get请求报文
GET /02_WEB_HTTP/index.html?username=admin&password=123123 HTTP/1.1 Accept: application/x-ms-app ...
- Team Leader炖完石头汤后干嘛
在万众创业的互联网年代,挖人组建全明星团队过于奢侈.面对水平参差不齐的团队咋办? 命运真是捉弄,半年前在大美团打工时准备做个NABC的教学项目 ,结果自己就被挖到"Competitors 竞 ...
- 1、开篇:PMO定义 - PMO项目管理办公室
PMO,Project Management Office,项目管理办公室,笔者不按照项目管理知识体系指南PMBOK(Project Management Body Of Knowledge)上的定义 ...
- JMS(Java消息服务)入门教程
什么是Java消息服务 Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持JAVA应用程序开发.在J2EE中 ...