#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. HDU 1505 City Game(DP)

    City Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. luogu2540 斗地主增强版

    题目大意 给你一副手牌,没有飞机带翅膀,按斗地主的规则,求将所有牌打出的最少次数. 题解 先不考虑顺子 我们已经知道花色对牌没有影响,那么如果不考虑顺子,每个牌具体是什么数字我们也用不着知道,我们关心 ...

  3. Codeforces Round #369 (Div. 2) 套题

    A:模拟水题不说 #include <iostream> #include <string.h> #include <algorithm> #include < ...

  4. 打印二叉树中距离根节点为k的所有节点

    package tree; public class Printnodesatkdistancefromroot { /** * Given a root of a tree, and an inte ...

  5. kernel信息及其相关命令

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

  6. 07-11 Linux命令操作

    1. 查看当做操作目录位置    > pwd 2. 查看(当前)目录里边的文件内容    > ls                //list    > ls  -l  或ll    ...

  7. [App Store Connect帮助]四、添加 App 图标、App 预览和屏幕快照(2)添加一个 App Store 图标

    您必须提供一个 App Store 图标,用于在 App Store 中的不同部分展示您的 App.请遵照 Human Interface Guidelines(<人机界面准则>)创建您的 ...

  8. 模拟 HDOJ 4552 Running Rabbits

    题目传送门 /* 模拟:看懂题意,主要是碰壁后的转向,笔误2次 */ #include <cstdio> #include <algorithm> #include <c ...

  9. Android 性能优化(4)Optimizing Layout Hierarchies:用Hierarchy Viewer和Layoutopt优化布局

    Optimizing Layout Hierarchies This lesson teaches you to Inspect Your Layout Revise Your Layout Use ...

  10. 附加数据库错误代码 - 950【MSSQL】

    分析 (539)代表的是Sql Server 2000数据库的内部版本号,也就是说要附加的数据库文件是由Sql Server 2000创建的,但是我们知道Sql Server 2016 数据库是不兼容 ...