`
dengbaoleng
  • 浏览: 1134013 次
文章分类
社区版块
存档分类
最新评论

一个进程安全的日志类, Linux实现

 
阅读更多

项目中需要分类日志功能,且日志由多个进程产生,所以写了这个类。

  1. #ifndefCAPPLOG_H_
  2. #defineCAPPLOG_H_
  3. #include"Generic.h"
  4. enumLOG_TYPE
  5. {
  6. LOG_DEAGNOSTIC_MSG=0x00000000,
  7. LOG_EVENT=0x00000001,
  8. LOG_ACTIVITY=0x00000002,
  9. LOG_ERROR=0x00000004,
  10. };
  11. classCAppLog{
  12. public:
  13. CAppLog();
  14. virtual~CAppLog();
  15. staticvoidappendDeagnosticMsg(constchar*lpszFmt,...);
  16. staticvoidappendEvent(constchar*lpszFmt,...);
  17. staticvoidappendActivity(constchar*lpszFmt,...);
  18. staticvoidappendError(constchar*lpszFmt,...);
  19. private:
  20. staticsem_t*s_pLogFileSem;
  21. staticvoidappend(LOG_TYPEemType,constchar*lpszText);
  22. };
  23. #endif/*CAPPLOG_H_*/

以下是它的实现

  1. /*
  2. *CAppLog.cpp
  3. *
  4. *Createdon:Jan1,2009
  5. *Author:root
  6. */
  7. #include"CAppLog.h"
  8. #include"CSystemHelper.h"
  9. #defineLOG_SEM_NAME"LogFileSem"
  10. sem_t*CAppLog::s_pLogFileSem=NULL;
  11. CAppLogg_oAppLog;
  12. CAppLog::CAppLog(){
  13. if(s_pLogFileSem==NULL)
  14. {
  15. s_pLogFileSem=sem_open(LOG_SEM_NAME,O_CREAT,0666,1);
  16. }
  17. }
  18. CAppLog::~CAppLog(){
  19. if(s_pLogFileSem!=NULL)
  20. {
  21. sem_close(s_pLogFileSem);
  22. s_pLogFileSem=NULL;
  23. }
  24. }
  25. voidCAppLog::append(LOG_TYPEemType,constchar*lpszText)
  26. {
  27. charszBuffer[10240]={0};
  28. intnRet=readlink("/proc/self/exe",szBuffer,sizeof(szBuffer));
  29. if(nRet<=0||nRet>=PATH_MAX)
  30. return;
  31. stringstrTemp(szBuffer);
  32. stringstrDir=strTemp.substr(0,strTemp.rfind('/')+1);
  33. stringstrExe=strTemp.substr(strTemp.rfind('/')+1);
  34. time_tulNow={0};
  35. time(&ulNow);
  36. tm*pNow=localtime(&ulNow);
  37. memset(szBuffer,0,sizeof(szBuffer));
  38. sprintf(szBuffer,"%sLog/%04d/%04d-%02d/"
  39. ,strDir.c_str()
  40. ,1900+pNow->tm_year
  41. ,1900+pNow->tm_year
  42. ,1+pNow->tm_mon
  43. );
  44. CSystemHelper::createDirectory(szBuffer);
  45. sprintf(szBuffer,"%sLog/%04d/%04d-%02d/%04d-%02d-%02d.log"
  46. ,strDir.c_str()
  47. ,1900+pNow->tm_year
  48. ,1900+pNow->tm_year
  49. ,1+pNow->tm_mon
  50. ,1900+pNow->tm_year
  51. ,1+pNow->tm_mon
  52. ,pNow->tm_mday
  53. );
  54. sem_wait(s_pLogFileSem);
  55. {
  56. ofstreamstream(szBuffer,ios::out|ios::app);
  57. if(stream)
  58. {
  59. charszFormated[20480]={0};
  60. sprintf(szFormated,"/r/n%02d[%02d:%02d:%02dPID=%05dTID=%05ldEXE=%-15s]%s"
  61. ,emType
  62. ,pNow->tm_hour
  63. ,pNow->tm_min
  64. ,pNow->tm_sec
  65. ,getpid()
  66. ,(long)syscall(SYS_gettid)
  67. ,strExe.c_str()
  68. ,lpszText
  69. );
  70. stream<<szFormated;
  71. stream.flush();
  72. stream.close();
  73. }
  74. }
  75. sem_post(s_pLogFileSem);
  76. }
  77. voidCAppLog::appendDeagnosticMsg(constchar*lpszFmt,...)
  78. {
  79. va_listarg;
  80. va_start(arg,lpszFmt);
  81. charszBuffer[10240]={0};
  82. vsprintf(szBuffer,lpszFmt,arg);
  83. va_end(arg);
  84. CAppLog::append(LOG_DEAGNOSTIC_MSG,szBuffer);
  85. }
  86. voidCAppLog::appendEvent(constchar*lpszFmt,...)
  87. {
  88. va_listarg;
  89. va_start(arg,lpszFmt);
  90. charszBuffer[10240]={0};
  91. vsprintf(szBuffer,lpszFmt,arg);
  92. va_end(arg);
  93. CAppLog::append(LOG_EVENT,szBuffer);
  94. }
  95. voidCAppLog::appendActivity(constchar*lpszFmt,...)
  96. {
  97. va_listarg;
  98. va_start(arg,lpszFmt);
  99. charszBuffer[10240]={0};
  100. vsprintf(szBuffer,lpszFmt,arg);
  101. va_end(arg);
  102. CAppLog::append(LOG_ACTIVITY,szBuffer);
  103. }
  104. voidCAppLog::appendError(constchar*lpszFmt,...)
  105. {
  106. va_listarg;
  107. va_start(arg,lpszFmt);
  108. charszBuffer[10240]={0};
  109. vsprintf(szBuffer,lpszFmt,arg);
  110. va_end(arg);
  111. CAppLog::append(LOG_ERROR,szBuffer);
  112. }


使用起来很简单,将类加入到工程,然后在需要的地方包含此类的头文件,然后直接调用静态方法。这里用到了一个辅助类的函数 CSystemHelper::createDirectory,它的实现如下:

  1. voidCSystemHelper::createDirectory(constchar*lpszDir)
  2. {
  3. stringstrDir(lpszDir);
  4. intnIndex=0;
  5. while(nIndex!=string::npos)
  6. {
  7. intnFind=strDir.find("/",nIndex+1);
  8. stringstrTemp=strDir.substr(0,nFind+1);
  9. DIR*dir=opendir(strTemp.c_str());
  10. if(dir!=NULL)
  11. closedir(dir);
  12. else
  13. mkdir(strTemp.c_str(),S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
  14. nIndex=nFind;
  15. }
  16. }

分享到:
评论

相关推荐

    Linux C 实现日志打印功能

    Linux C 实现日志打印功能

    linux进程监控与自动重启的简单实现方法

    linux 下服务器程序会因为各种原因dump掉,就会影响用户使用,这里提供一个简单的进程监控和重启功能。 实现原理: 由定时任务crontab调用脚本,脚本用ps检查进程是否存在,如果不存在则重启并写入日志。 crontab...

    非常宝贵的LINUX学习笔记

    【linux学习笔记-12】守护进程的日志实现 【linux学习笔记-13】基本进程通信--文件锁 【linux学习笔记-14】基本进程通信--信号 【linux学习笔记-15】基本进程通信--管道 【linux学习笔记-16】shell管道重定向程序的...

    学习C++,学习windows/linux编程,学习网络编程,学习reactor模型,自己实现一遍练手;

    学习C++,学习windows/linux编程,学习网络编程,学习reactor模型,自己实现一遍练手; 包含(以下实现是Windows/Linux平台通用的,美名其曰跨平台) 基本工具库: 日志工具(日志优先级、同步日志、异步日志、带...

    Easylogger轻量级日志的 linuxdemo

    EasyLogger 是一款超轻量级(ROM, RAM)、高性能的 C/C++ 日志库,非常适合对资源敏感的软件项目,例如: IoT 产品、可穿戴设备、智能家居等等。相比 log4c、zlog 这些知名的 C/C++ 日志库, EasyLogger 的功能更加...

    论文研究-Linux环境下网络日志审计系统的实现 .pdf

    Linux环境下网络日志审计系统的实现,张志辉,,随着因特网的迅猛发展和国家信息化进程的加速,网络安全的形势更加严峻,迫切需要行之有效的网络安全保障技术。网络日志审计系统是�

    Linux学习笔记Linux学习资料Linux教程

    【linux学习笔记-12】守护进程的日志实现.doc 【linux学习笔记-13】基本进程通信--文件锁.doc 【linux学习笔记-14】基本进程通信--信号.doc 【linux学习笔记-15】基本进程通信--管道.doc 【linux学习笔记-16】shell...

    node-windows, Windows 对 node.js 脚本( 守护进程事件日志UAC等)的支持.zip

    node-windows, Windows 对 node.js 脚本( 守护进程事件日志UAC等)的支持 更新 (2/11/17)Gitter自 2015年以来一直在解决...这个项目和它的姐妹项目( 节点 mac & 节点 linux )的数量是一个比我自己提供的更多的注意事项。

    基于C语言实现的进程运行轨迹的跟踪与统计【100010139】

    进程从创建(Linux 下调用 fork())到...在 Linux 0.11 上实现进程运行轨迹的跟踪 基本任务是在内核中维护一个日志文件*/var/process.log*,把操作系统启动到系统关机过程中所有进程的运行轨迹都记录在这一 log 文件中。

    linux大作业报告

    4、编写程序实现一个简单的员工档案管理系统,具备简单的员工资料增加、删除和查询等功能,并采用二进制文件保存员工的资料信息。 5、为了便于文件的管理和传输,某些时候需要将特别大的文件切割为多个指定长度小的...

    C++编写LINUX守护进程的实现代码

    守护进程是运行在后台的一种特殊进程,它独立于控制终端并且周期性地执行某种任务或循环等待处理某些事件的发生; 守护进程一般在系统启动时开始运行,除非强行终止,否则直到系统关机才随之一起停止运行; 守护进程...

    边干边学——LINUX内核指导

    2. 2 实现一个简单的shell程序 2. 3 shell编程 第3章 内核时钟 3. 1 关于时钟和定时器 3. 2 Linux系统时钟 3. 3 Linux系统定时器 3. 4 时钟命令介绍 3. 5 定时器的应用 第4章 内核模块 4. 1 概述 4. 2 模块实现机制 ...

    清华大学Linux操作系统原理与应用

    6.5 实例——利用系统调用实现一个调用日志收集系统 143 6.5.1 代码体系结构 143 6.5.2 把代码集成到内核中 146 6.5.3 实现步骤 148 习题6 148 第7章 内核中的同步 149 7.1 临界区和竞争状态 149 7.1.1 临界区举例 ...

    基于KRSI(eBPF+LSM)的Linux安全审计、控制和行为分析工具

    针对操作系统、内核安全,safeguard是一个基于eBPF的Linux审计观测工具,可以实现安全操作的拦截及审计记录。项目采用libbpfgo库,使用go语言实现顶层控制。日志记录配置范围内的行为。针对文件,进程,网络的安全...

    kangle web服务器 v3.2.1 正式版 for linux.zip

    用户之间安全隔离,一个用户出问题不影响其他用户。安全支持php、asp、asp.net、java、ruby等多种动态开发语言。   kangle web服务器主要特点: 1、免费开源 kangle技术团队希望国人拥有一款真正好用、易用、...

    Linux命令执行程序shell

    在linux下,C语言实现的模拟shell程序。能够显示每条命令使用的cpu时间、进程中断次数、换页次数。

    Linux启动程序systemd

    init是内核启动的第一个用户空间进程,主要负责启动、终止系统中的基础服务进程。 Linux下,init主要有三个实现版本: System V,传统的init Upstart,Ubuntu后期针对sys-v的一个改进实现版本 systemd,是一套...

    linuxshell

    实验任务一: 编写一个Shell过程完成如下功能(必须在脚本中使用函数): 1、合并两个$1、$2文件为$3,并显示。 2、如果缺少$3,那么先报告缺少$3,然后将合并后的内容输出到mydoc.txt。如果有$3,就合并到...

    边干边学Linux__第二版_doc格式

    12.6 实验一:使用proc文件系统的一个简单例子 12.7 实验二:利用/proc文件系统显示缺页状态 12.8 实验三:seq file使用例子 第13章 内核模块 13.1 什么是内核模块 13.2 模块实现机制 13.3 使用内核模块 13.4 实例 ...

Global site tag (gtag.js) - Google Analytics