#cat log.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h> #include "log.h" /* Log */
#define IDLE_MEM_CFG_PRINT_LOG 1 void ilog(const char *func, int line, char *fmt, ... )
{
char buf[LINE_MAX] = {0};
time_t timep;
va_list va;
int pos = 0; /* Bail if we don't have logging functionality enabled */
if (!IDLE_MEM_CFG_PRINT_LOG)
return; /* Header, including the timestamp */
time(&timep);
pos = snprintf(buf, LINE_MAX, "<%s>[%s:%d] ", ctime(&timep), func, line); /* Variable arguments */
va_start(va, fmt);
pos = vsnprintf(buf + pos, LINE_MAX - pos, fmt, va);
va_end(va); /* Raise a warning if the buffer is overran */
if (pos >= (LINE_MAX - 1))
fprintf(stderr, "%s\n", "buffer may have been truncated"); fprintf(stdout, "%s\n", buf);
} void error_log(char *err, const char *func, int line, int ret, char *fmt, ... )
{
char buf[LINE_MAX] = {0};
time_t timep;
va_list va;
int pos; /* Bail if we don't have logging functionality enabled */
if (!IDLE_MEM_CFG_PRINT_LOG)
return; /* Header, including the timestamp */
time (&timep);
if(err) {
pos = snprintf(buf, LINE_MAX, "<%s>[%s:%d]: Retval %d \" %s \" ",
ctime(&timep), func, line, ret, err);
} else {
pos = snprintf(buf, LINE_MAX, "<%s>[%s:%d]: Retval %d ",
ctime(&timep), func, line, ret);
} /* Variable arguments */
va_start(va, fmt);
pos = vsnprintf(buf + pos, LINE_MAX - pos, fmt, va);
va_end(va); /* Raise a warning if the buffer is overran */
if (pos >= (LINE_MAX - 1))
fprintf(stderr, "%s\n", "buffer may have been truncated"); fprintf(stderr, "%s\n", buf);
}
#cat log.h
/* Log */
void error_log(char *err, const char *func, int line, int ret, char *fmt, ... );
void ilog(const char *func, int line, char *fmt, ... );
#define ELOG(libc, ret, fmt, args... ) \
do { \
if (libc) \
error_log(strerror(errno), __func__, __LINE__, ret, fmt, ##args); \
else \
error_log(NULL, __func__, __LINE__, ret, fmt, ##args); \
} while(0) #define ILOG(fmt, args...) \
do { \
ilog(__func__, __LINE__, fmt, ##args); \
} while(0)
#cat main.c
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "log.h" void main(void)
{
char *name1 = "jack";
char *name2 = "tom";
int ret; ret = 0;
ILOG("hello %s, my name is %s\n", name1, name2);
ELOG(0, ret, "waiting you");
ELOG(1, ret, "waiting you");
}
gcc -o main log.c log.h main.c
#./main
<Fri Jan 11 11:19:04 2019
>[main:13] hello jack, my name is tom <Fri Jan 11 11:19:04 2019
>[main:14]: Retval 0 waiting you
<Fri Jan 11 11:19:04 2019
>[main:15]: Retval 0 " Success " waiting you

A useful logger function in C project.的更多相关文章

  1. [WASM] Call a JavaScript Function from WebAssembly

    Using WASM Fiddle, we show how to write a simple number logger function that calls a consoleLog func ...

  2. 10 Rules of Highly Successful Project Management

    I commited the information below to report PDU of PMI. ^_^. In this paper, the author introduces his ...

  3. 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger

    本篇是在上一篇的基础上添加日志功能,并记录NLog在Asp.Net Core里的使用方法. 第一部分:默认Logger支持 一.project.json添加日志包引用,并在cmd窗口使用 dotnet ...

  4. [转]使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger

    本文转自:https://www.cnblogs.com/niklai/p/5662094.html 本篇是在上一篇的基础上添加日志功能,并记录NLog在Asp.Net Core里的使用方法. 第一部 ...

  5. 转: requirejs中文api (详细)

    RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代码的模块化.它鼓励在使用脚本时以modul ...

  6. Slim 文档-First Application 翻译

    最近刚好在用Slim框架,看文档的时候,中文文档中缺了这一篇没有翻译,于是我就把它翻译过来了.本人英语水平有限,如有错误请告知. 如果你在寻找创建一款非常简单的 Slim 应用程序的流程,来这里算是找 ...

  7. TapTap推广统计逻辑

    当我们在Taptap上访问某款游戏时,比如https://www.taptap.com/app/34762,taptap会记录下这次访问,它是怎么做的呢. 首先,用记事本打开这个网址,在head部分看 ...

  8. 【个人经历】记自己的第一次GitHub开源代码共享经历

    题记: 自己做程序员快三年有余了,感觉自己和刚入职相比确实有了不少进步,当然三年要是不进步那不就傻了吗,有时候我也在想,我在这三年里留下了什么,当然也不是说有多么高尚的想法,就是以后对别人介绍自己的时 ...

  9. gulp & webpack整合

    为什么需要前端工程化? 前端工程化的意义在于让前端这个行业由野蛮时代进化为正规军时代,近年来很多相关的工具和概念诞生.好奇心日报在进行前端工程化的过程中,主要的挑战在于解决如下问题:✦ 如何管理多个项 ...

随机推荐

  1. 【hdu3966】Aragorn's Story

    题意:给一棵树,并给定各个点权的值,然后有3种操作:I C1 C2 K: 把C1与C2的路径上的所有点权值加上KD C1 C2 K:把C1与C2的路径上的所有点权值减去KQ C:查询节点编号为C的权值 ...

  2. SQLyog软件里无法插入中文(即由默认的latin1改成UTF8编码格式)

    问题详情: 无法插入中文? 解决办法: 需要修改编码格式,由默认的latin1改为utf8. 改成, 成功!

  3. 洛谷P1850 [noip2016]换教室——期望DP

    题目:https://www.luogu.org/problemnew/show/P1850 注释掉了一堆愚蠢,自己还是太嫩了... 首先要注意选或不选是取 min 而不是 /2 ,因为这里的选或不选 ...

  4. 配置文件git config介绍

    Git有一个工具被称为git config,它允许你获得和设置配置变量:这些变量可以控制Git的外观和操作的各个方面. 一. 配置文件的存储位置 这些变量可以被存储在三个不同的位置: 1./etc/g ...

  5. kernel信息及其相关命令

    内核 linux内核是单内核体系设计.但充分借鉴了微内核设计体系的优点,为内核引入模块化机制 内核组成部分: kernel: 内核核心,一般为bzImage,通常在/boot 目录下,名称为vmlin ...

  6. samba - linux客户端访问samba服务器的指令(转载)

    转自:http://linux.sheup.com/linux/linux5303.htm linux客户端访问samba服务器的指令2004-04-23 15:18 pm来自:Linux文档现载:W ...

  7. 如何在 ubuntu 12.04 上安装 skype(转载)

    转自:http://blog.51osos.com/linux/how-to-install-skype-in-ubuntu-12-04/ 添加Canonical Partner Repository ...

  8. P3990 [SHOI2013]超级跳马

    传送门 首先不难设\(f[i][j]\)表示跳到\((i,j)\)的方案数,那么不难得到如下转移 \[f[i][j]=\sum\limits_{k=1}^{\frac n2}f[i-2k+1][j-1 ...

  9. Spring中bean的作用域与生命周期

    在 Spring 中,那些组成应用程序的主体及由 Spring IOC 容器所管理的对象,被称之为 bean.简单地讲,bean 就是由 IOC 容器初始化.装配及管理的对象,除此之外,bean 就与 ...

  10. Android项目模块化遇到的问题

    1.问题背景 gradle 4 MacOs 10.14.3 Android Studio 3 在android模块化的时候,例如,有两个模块,一个是usercenter,另一个是common. 其中u ...