Skip to content

Commit

Permalink
给代码添加了些注释,打包成库,添加了redame
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Chan committed Oct 28, 2016
1 parent 2c7d94c commit 2fe79b3
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 1,044 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
# Debug files
*.dSYM/
*.su

*.log
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# plog
利用C封装的一个库,其中有可以用于各系统运行日志的输出,缓冲区是自己分配并管理的
[TOC]
## 库说明
在我们的系统中,可能很多时候需要打印日志每次写一个日志模块,比较麻烦,也犯不着。写个来放在这儿。我这款日志系统可以实现异步打印。主要分为两个模块:<br/>
>1.内存管理<br/>
>2.日志管理<br/>
日志管理中使用了内存管理的接口,依赖于内存管理。我在内存管理的模块中维护了几块内存,每块内存作为一个页面。写日志时将其缓存在页面中,当有页面写满的时候进行通知
## 库使用
#### 接口
```c
int libplog_init ();
int libplog_destrory ();
int libplog_write (const char *file_name, const char *func_name,
const int line, log_level_t level, char *msg, ...);

/*
* @description:调用libplog_write(),传入调试级别码
*/
#define PLOG_DEBUG(msg, args...) \
libplog_write(__FILE__, __FUNCTION__, __LINE__, DEBUG, msg, ##args)

/*
* @description:调用libplog_write(),传入提示级别码
*/
#define PLOG_INFO(msg, args...) \
libplog_write(__FILE__, __FUNCTION__, __LINE__, INFO, msg, ##args)

/*
* @description:调用libplog_write(),传入警告级别码
*/
#define PLOG_WARN(msg, args...) \
libplog_write(__FILE__, __FUNCTION__, __LINE__, WARN, msg, ##args)

/*
* @description:调用libplog_write(),传入错误级别码
*/
#define PLOG_ERROR(msg, args...) \
libplog_write(__FILE__, __FUNCTION__, __LINE__, ERROR, msg, ##args)

/*
* @description:调用libplog_write(),传入崩溃级别码
*/
#define PLOG_FATAL(msg, args...) \
libplog_write(__FILE__, __FUNCTION__, __LINE__, FATAL, msg, ##args)

```
使用起来也特别简单,程序启动后写日志前,必须进行初始化即`libplog_init`操作,然后中间根据需要调用各个宏,也就是间接地调用`libplog_write`函数,最后,使用完后记得销毁,不然会有内存泄漏。
#### 编译
将`libplog.a`放在某个路径下,当前目录的加`-L.`,我这里是放在当前目录下的`lib`中,所以加`-L./lib`。然后在后面添加库名`-lplog`。最后不要忘了加上`-lpthread`,因为我在库中用了多线程。参考指令如下:
```bash
$ gcc main.c -I./include -L./lib -lplog -lpthread
```
1,000 changes: 0 additions & 1,000 deletions gc.log

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
72 changes: 50 additions & 22 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* lib.c
* Copyright (C) 2016 Gavin Chan <jiajun.chen@ww-it.com>
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
Expand All @@ -14,7 +14,7 @@
* 3. Neither the name ``Gavin Chan'' nor the name of any other
* contributor may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
*
* libplog IS PROVIDED BY Gavin Chan ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -41,12 +41,19 @@
/* 最大的字符串长度加一 */
#define MAX_SIZE 200

static int log_fd; /* 日志文件描述符 */
static int log_fd; /* 日志文件描述符 */
static pthread_mutex_t mutex; /* 持久化用锁 */
static pthread_cond_t cond; /* 持久化条件变量 */
static int flag = 0; /* 如果不会再打印日志了,置一 */
static pthread_t thrd;
static int flag = 0; /* 如果不会再打印日志了,置一 */
static pthread_t thrd; /* 打印线程的ID */

/*
* @function:get_level
* @description:根据level返回对应字符串
* @return:对应字符串
* @called by:libplog_write
*
*/
static char *
get_level(log_level_t level)
{
Expand All @@ -64,7 +71,10 @@ get_level(log_level_t level)
/*
* @function:persistence
* @description:将缓冲区数据写入磁盘(线程)
*
* @calls:get_block_string pthread_cond_wait
* release_full_block
* @called by:libplog_init
*
*/
static void *
persistence(void *arg)
Expand All @@ -82,19 +92,23 @@ persistence(void *arg)
break;
}
}

return NULL;
}

/*
*@function:plog_cache()
*@description:缓存日志
*
* @function:plog_cache()
* @description:缓存日志
* @calls:memory_alloc strlen
* memcpy pthread_cond_signal
* @param:log->待缓存的一条log
* @called by:libplog_write
*
*/
int plog_cache(const char *log)
{
void *begin;

cache:
if((begin = memory_alloc (strlen(log))) != NULL)
{
Expand All @@ -105,17 +119,24 @@ int plog_cache(const char *log)
pthread_cond_signal(&cond);
goto cache;
}

return 0;
}

/*
* @function:libplog_write
* @description:日志打印的入口函数,所有的调用最终从这里进入
*
* @calls:strftime localtime strlen
* snprintf va_start vsnprintf
* va_end plog_cache
* @param:file_name->产生日志的文件名
* func_name->产生日志的函数名
* line->产生日志的行数
* level->日志的级别
* msg,...->日志内容的格式化输出形式
*/
int
libplog_write(const char *file_name, const char *func_name,
int
libplog_write(const char *file_name, const char *func_name,
const int line, log_level_t level, char *msg, ...)
{
char log[MAX_SIZE] = {'\0'};
Expand All @@ -124,7 +145,7 @@ libplog_write(const char *file_name, const char *func_name,
/* 获取时间 */
time(&t);
strftime( log, MAX_SIZE, "[%y-%m-%d %H:%M:%S] ",localtime(&t) );

/* snprintf和vsnprintf,如果大于MAX_SIZE将被截断 */
len = strlen(log);
snprintf(log + len, MAX_SIZE - len, "%s [%s:%d] %s ", file_name,
Expand All @@ -136,7 +157,7 @@ libplog_write(const char *file_name, const char *func_name,
va_end(arg_ptr);
/* 缓存 */
plog_cache(log);

return (0);
}

Expand All @@ -147,14 +168,17 @@ libplog_write(const char *file_name, const char *func_name,
* 初始化互斥锁、条件变量
* 打开文件描述符
* 创建日志持久化线程
* @calls:memory_init pthread_mutex_init
* pthread_cond_init open
* pthread_create perror
*/
int
libplog_init()
{
memory_init ();
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);

if((log_fd = open("gc.log",O_RDWR | O_CREAT | O_APPEND, 0777)) == -1)
{
perror("open file gc.log failed.");
Expand All @@ -164,14 +188,18 @@ libplog_init()
{
perror("create thread failed.");
}

return 0;
}

/*
* @function:libplog_destory
* @description:销毁当初所创建的,并且等待线程结束
*
* @calls:update_current_block memory_destory
* pthread_join pthread_mutex_destroy
* pthread_cond_signal pthread_cond_destroy
* close
*
*/
int
libplog_destrory()
Expand All @@ -183,8 +211,8 @@ libplog_destrory()
memory_destory ();
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);

close(log_fd);

return 0;
}
}
Loading

0 comments on commit 2fe79b3

Please sign in to comment.