文章目录

  • 一、功能实现
  • 二、日志源码
  • 三、结果展示

在我们自己做项目过程中会用到日志系统来记录程序的信息,这就需要我们自己来完成日志系统。

一、功能实现

完成的主要功能:

  • 日志文件位置可通过参数解析改变,默认为当前路径下的my_log.log文件下;
  • 日志系统分级设置,包含四个级别: LOG_ERROR(错误)、LOG_WARN(警告)、LOG_INFO(信息)、LOG_DEBUG(调试);
  • 日志记录包含月份、时间、文件名、函数名、行号;

二、日志源码

/**********************************************************************************      Copyright:  (C) 2023 WangDengtao<1799055460@qq.com>*                  All rights reserved.**       Filename:  my_log.c*    Description:  This file *                 *        Version:  1.0.0(2023年04月19日)*         Author:  WangDengtao <1799055460@qq.com>*      ChangeLog:  1, Release initial version on "2023年04月19日 11时08分01秒"*                 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <getopt.h>#define LEVEL_MAX 3
#define log_write(level, ms, arg...) LOG_WRITE(__FILE__, __LINE__, __func__, level, ms, ##arg)typedef struct log_s
{FILE *fp;char filepath[64];
}log_t;log_t LOG;/* === INFO ===* 在这里输出的信息,对最终用户具有实际意义,也就是最终用户要能够看得明白。** === DEBUG ===* 这个级别最低,一般在系统开发环境中使用,产品环境一般来不输出。** === WARN、ERROR ===* 警告,这个在系统运行时检测到了一个不正常的状态。*/
enum {LOG_ERROR,     /* Error */LOG_WARN,      /* Warning */LOG_INFO,      /* Information */LOG_DEBUG      /* Debug */
};void LOG_WRITE(const char *file, const int line, const char *func, int level, char *ms, ...);
void get_time(char *buf, int buf_size);//获取时间的函数
int log_init(char *file_path, int size_path);//初始化结构体中的filepath,默认输出到终端,有路径文件输入到路径文件
void pro_usage(char *program_name);//使用说明int main(int argc, char *argv[])
{int    a = 1;int    rv;int    ch = -1;char   file_path[64];/*参数解析路径*/static struct option long_options[] = {{"help", no_argument, 0, 'h'},{"filepath", required_argument, 0, 'p'},{0, 0, 0, 0}};void pro_usage(char *program_name){printf("%s usage: \n", program_name);printf("-h(--help): Get Help and Support.\n");printf("-p(--filepath): The location of the save path.\n");printf("No path is set. The default path is terminal output.\n");exit(0) ;}while((ch = getopt_long(argc, argv, "hp:", long_options, NULL)) != -1){switch(ch){case 'h':{pro_usage(argv[0]);break;}case 'p':{snprintf(file_path, sizeof(file_path), "%s", optarg);break;}}}//printf("main:file_path: %s\n", file_path);memset(&LOG, 0, sizeof(LOG));rv = log_init(file_path, strlen(file_path));if(rv < 0){printf("log_init failure\n");return -1;}//printf("file_path:%s\n", LOG.filepath);printf("The log system is started successfully!\n");log_write(LOG_ERROR, "hello %d\n", a);log_write(LOG_WARN, "hello %d\n", a);log_write(LOG_INFO, "hello %d\n", a);log_write(LOG_DEBUG, "hello %d\n", a);return 0;
}/*初始化结构体中的filepath,默认输出到当前文件my_log.log,有路径文件输入到路径文件*/
int log_init(char *file_path, int size_path)
{//printf("log_init:file_path:%s\n",file_path);if(size_path > 0){strncpy( LOG.filepath, file_path, sizeof(LOG.filepath) );//snprintf(LOG.filepath, sizeof(LOG.filepath),"%s", file_path);//printf("1LOG.filepath: %s\n", LOG.filepath);  return 0;}else{strncpy( LOG.filepath, "my_log.log", sizeof(LOG.filepath) );//snprintf(LOG.filepath, sizeof(LOG.filepath),"%s", "stdout");//printf("2LOG.filepath: %s\n", LOG.filepath);return 0;}
}/*获取时间的函数*/
void get_time(char *buf, int buf_size)
{struct tm *tm_ptr;time_t the_time;(void)time(&the_time);tm_ptr = gmtime(&the_time);sprintf(buf, "%02d-%02d-%02d  %02d:%02d:%02d",tm_ptr->tm_year+1900, tm_ptr->tm_mon+1, tm_ptr->tm_mday,(tm_ptr->tm_hour)+8, tm_ptr->tm_min, tm_ptr->tm_sec);
}/* 日志系统写操作*/
void LOG_WRITE(const char *file, const int line, const char *func, int level, char *ms, ...)
{const char global_log_level_string[4][10] = {"ERROR", "WARNING", "INFO", "DEBUG"};char wzLog[1024];                       char time_buf[1024];                    va_list args;                char buf[2500];va_start(args, ms);                     memset(wzLog, 0, sizeof(wzLog));        vsprintf(wzLog, ms, args);              va_end(args);                           get_time(time_buf, sizeof(time_buf));   snprintf(buf, sizeof(buf), "[%s %s %s %d] %s: %s", time_buf, file, func, line, global_log_level_string[level], wzLog);//printf("buf:%s", buf);/*strcat(time_buf, "] ");strcat(time_buf, global_log_level_string[level]);strcat(time_buf, ": ");strcat(time_buf, wzLog);*///printf("log_write:filepath:%s\n", LOG.filepath);LOG.fp = fopen(LOG.filepath,"a+");    fwrite(buf, 1, strlen(buf), LOG.fp);  fclose(LOG.fp);                             return ;
}

Makefile(默认是生成的my_log.log文件):

CC = gcc
file = my_logall:${CC} ${file}.c -o ${file}clean:rm -rf ${file}rm -rf ${file}.log

三、结果展示

wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ls
Makefile   my_log.c

执行(默认为当前文件夹下生成my_log.log日志文件):

wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ make
gcc my_log.c -o my_log
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ls
Makefile   my_log   my_log.c
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ./my_log -h
./my_log usage:
-h(--help): Get Help and Support.
-p(--filepath): The location of the save path.
No path is set. The default path is terminal output.
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ./my_log
The log system is started successfully!
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ls
Makefile   my_log   my_log.c   my_log.log
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ cat my_log.log
[2023-04-19  16:55:53 my_log.c main 107] ERROR: hello 1
[2023-04-19  16:55:53 my_log.c main 108] WARNING: hello 1
[2023-04-19  16:55:53 my_log.c main 109] INFO: hello 1
[2023-04-19  16:55:53 my_log.c main 110] DEBUG: hello 1

生成自定义的日志文件:

wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ./my_log -p haha.log
The log system is started successfully!
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ls
haha.log   Makefile   my_log   my_log.c   my_log.log
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ cat haha.log
[2023-04-19  17:08:59 my_log.c main 107] ERROR: hello 1
[2023-04-19  17:08:59 my_log.c main 108] WARNING: hello 1
[2023-04-19  17:08:59 my_log.c main 109] INFO: hello 1
[2023-04-19  17:08:59 my_log.c main 110] DEBUG: hello 1
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ ./my_log -p ../haha.log
The log system is started successfully!
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao/my_syslog$ cd ..
wangdengtao@wangdengtao-virtual-machine:~/wangdengtao$ cat haha.log
[2023-04-19  17:10:02 my_log.c main 107] ERROR: hello 1
[2023-04-19  17:10:02 my_log.c main 108] WARNING: hello 1
[2023-04-19  17:10:02 my_log.c main 109] INFO: hello 1
[2023-04-19  17:10:02 my_log.c main 110] DEBUG: hello 1

回滚的话暂时没用到,后面用到了在搞搞。

C语言编写自己的日志系统相关推荐

  1. 用c语言编写程序学工系统,[工学]chap02_用C语言编写程序.ppt

    [工学]chap02_用C语言编写程序 Chap 2 用C语言编写程序 2.1 在屏幕上显示 Hello World! 2.2 求华氏温度 100°F 对应的摄氏温度 2.3 计算分段函数 2.4 输 ...

  2. 自动点名系统c语言,用C语言编写一个随机点名系统

    /*编写一个随机点名系统,运行该系统后,按空格键可以显示出一名同学,以前被选中的同学,将不会再次被选中*/ #include /*standard input & output*/ #incl ...

  3. 【无标题】C语言编写一个简单答题系统

    这是蒟蒻写的第一个博客,将就看看吧! 首先,我写的是一个填空题答题系统,填空题数目为20. 其次,需要随机出题,题目出现顺序要不一致. (1) 产生随机数 1~20 (2)  解决运气不好的问题1-1 ...

  4. 用c语言编写rfid读卡系统,USB免驱RFID读写器编程解析之一:智能卡篇

    随着RFID应用的普及,越来越多的软件工程师需要使用RFID读写器编程来实现自己的需求.为了使软件工程师更快的了解RFID读写器的使 用,本文选择一款市面上常用的USB免驱RFID读写器SDT-HA来 ...

  5. C语言编写简单的答题系统(填空题)

    这是本人发布的第一个博客,如有不足请见谅并指出谢谢. 填空题答题系统要求: (1):随机出题(使用随机数,假定二十题,不重复) (2):显示题号 (3):反馈正误,如错误则给出正确答案 (4):英文大 ...

  6. 如何用C语言编写学生选修课程系统?

    假定有 n 门课程,每门课程有:课程编号,课程名称,课程性质(公共课.必 修课.选修课),总学时,授课学时,实验或上机学时,学分,开课学期等信 息,学生可按要求(如总学分不得少于 60)自由选课. 1 ...

  7. c语言机票座位预定系统_c语言编写~~~机票座位预订系统

    展开全部 #include #include #include #include #include #include #include //overflow #define ok 1 typedef ...

  8. python做一个考试系统_请用 Python 语言编写一个简易的系统登录程序。

    import tkinter.messagebox import tkinter my_window = tkinter.Tk() # 生成 my_window 主窗口 my_window.title ...

  9. c语言编写小学数学测试系统,用c语言编写 小学数学考试题

    满意答案 ssjzrl 2013.09.09 采纳率:49%    等级:12 已帮助:13053人 #include "stdio.h" #include "time. ...

最新文章

  1. flask 学习实战项目实例
  2. 事件冒泡和阻止事件冒泡
  3. 判断是否是完全二叉树_【数据结构】二叉树高频考试题目【代码模板】!
  4. PC如何控制device进入suspend模式
  5. 【报告分享】2021年视频号发展年中报告.pdf(附下载链接)
  6. $NOIp2018$劝退记
  7. 圆形led屏幕_展示厅LED大屏幕安装价格/芮城
  8. Java Script小技巧【对象,属性】(转载)
  9. c语言+游戏破解,c语言获得键盘的按键
  10. 经典Python面试题之数据库和缓存
  11. Python-Spyder中文包正式发布!
  12. pcdj dex 3破解版激活教程
  13. 第一章 WEB应用程序开发流程
  14. 步进电机c语言驱动原理,连接PC的步进电机简单驱动电路
  15. Vasp 石墨烯能带计算
  16. hr面试十大经典提问
  17. 三星6818核心板接口众多兼容三星4418开发板
  18. 高等数学:对向量及其线性运算和数量积、向量积的见解
  19. 跳槽穷半年,转行穷三年,死守会穷一辈子
  20. 如何一键删除PPT的动画效果?

热门文章

  1. 《深入理解LINUX内存管理》学习笔记(一)
  2. 光伏储能联合并网系统matlab/simlink仿真
  3. 两年美团算法大佬的个人总结与学习建议
  4. c语言 case语句用法,switch case语句的用法
  5. 数据挖掘工程师知识集锦
  6. 做校招笔试前,必须知道的产品运营专业知识
  7. 实录分享 | Google Borg 系统 与 Coding Docker 实践
  8. [java]实习一个月~总结+期望~
  9. css定位-子绝父相
  10. 关于户口和三方协议的问题