A useful logger function in C project.
#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.的更多相关文章
- [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 ...
- 10 Rules of Highly Successful Project Management
I commited the information below to report PDU of PMI. ^_^. In this paper, the author introduces his ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger
本篇是在上一篇的基础上添加日志功能,并记录NLog在Asp.Net Core里的使用方法. 第一部分:默认Logger支持 一.project.json添加日志包引用,并在cmd窗口使用 dotnet ...
- [转]使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger
本文转自:https://www.cnblogs.com/niklai/p/5662094.html 本篇是在上一篇的基础上添加日志功能,并记录NLog在Asp.Net Core里的使用方法. 第一部 ...
- 转: requirejs中文api (详细)
RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代码的模块化.它鼓励在使用脚本时以modul ...
- Slim 文档-First Application 翻译
最近刚好在用Slim框架,看文档的时候,中文文档中缺了这一篇没有翻译,于是我就把它翻译过来了.本人英语水平有限,如有错误请告知. 如果你在寻找创建一款非常简单的 Slim 应用程序的流程,来这里算是找 ...
- TapTap推广统计逻辑
当我们在Taptap上访问某款游戏时,比如https://www.taptap.com/app/34762,taptap会记录下这次访问,它是怎么做的呢. 首先,用记事本打开这个网址,在head部分看 ...
- 【个人经历】记自己的第一次GitHub开源代码共享经历
题记: 自己做程序员快三年有余了,感觉自己和刚入职相比确实有了不少进步,当然三年要是不进步那不就傻了吗,有时候我也在想,我在这三年里留下了什么,当然也不是说有多么高尚的想法,就是以后对别人介绍自己的时 ...
- gulp & webpack整合
为什么需要前端工程化? 前端工程化的意义在于让前端这个行业由野蛮时代进化为正规军时代,近年来很多相关的工具和概念诞生.好奇心日报在进行前端工程化的过程中,主要的挑战在于解决如下问题:✦ 如何管理多个项 ...
随机推荐
- JBOSS和EJB学习一
1.使用软件 IDE:Eclipse4.3(开普勒) EE版本 服务器:jboss EAP 6.2 eclipse-jboss plugin:jbosstools-Update-4.1.2.Final ...
- Keil和IAR——使用笔记
1. 几个宏的定义 Keil和IAR均用到以下三种宏定义,分别是:USE_STDPERIPH_DRIVER——表示使用ST提供的标准外设固件库:STM32F40_41xxx——作为芯片的判断:VECT ...
- PL/SQL -->隐式游标(SQL%FOUND)
PL/SQL -->隐式游标(SQL%FOUND) 分类: SQL/PLSQL 基础2010-12-22 16:23 4084人阅读 评论(0) 收藏 举报 sqlexceptionoracle ...
- POJ1673 ZOJ1776 三角形四心模板
POJ1673 题中所述点即为三角形的垂心,用向量法可以轻松证明. 垂心 重心 外心 均位于三角形的欧拉线上,且三者有线性关系,于是,求出重心和外心即可求得垂心. 重心就是三点的平均值,外心可以通过解 ...
- ZOJ 3956 Course Selection System 背包DP
ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的d ...
- Linux 下編輯 PDF 檔的工具(PDF editor under Linux)(转载)
转自:http://www.gtwang.org/2011/05/linux-pdf.html PDF 檔雖然是一個跨平台的檔案格式,但 Adobe 只有提供免費的 Adobe Reader,要看 P ...
- [转]深入C语言内存区域分配(进程的各个段)详解
一般情况下,一个可执行二进制程序(更确切的说,在Linux操作系统下为一个进程单元,在UC/OSII中被称为任务)在存储(没有调入到内存运行)时拥有3个部分,分别是代码段(text).数据段(data ...
- D. Vasya And The Matrix(Educational Codeforces Round 48)
D. Vasya And The Matrix time limit per test2 seconds memory limit per test256 megabytes inputstandar ...
- Manacher HDOJ 5371 Hotaru's problem
题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...
- vue中引入swiper插件
这里我们使用npm的方式安装swiper插件. 1.npm install vue-awesome-swiper --save 2.在main.js文件中引入文件 import Vue from 'v ...