项目中需要分类日志功能,且日志由多个进程产生,所以写了这个类。
- #ifndefCAPPLOG_H_
- #defineCAPPLOG_H_
-
- #include"Generic.h"
-
enumLOG_TYPE
- {
- LOG_DEAGNOSTIC_MSG=0x00000000,
- LOG_EVENT=0x00000001,
- LOG_ACTIVITY=0x00000002,
- LOG_ERROR=0x00000004,
- };
-
-
classCAppLog{
-
public:
- CAppLog();
-
virtual~CAppLog();
-
-
staticvoidappendDeagnosticMsg(constchar*lpszFmt,...);
-
staticvoidappendEvent(constchar*lpszFmt,...);
-
staticvoidappendActivity(constchar*lpszFmt,...);
-
staticvoidappendError(constchar*lpszFmt,...);
-
-
-
private:
-
staticsem_t*s_pLogFileSem;
-
-
staticvoidappend(LOG_TYPEemType,constchar*lpszText);
- };
-
-
- #endif/*CAPPLOG_H_*/
-
以下是它的实现
-
- #include"CAppLog.h"
- #include"CSystemHelper.h"
-
- #defineLOG_SEM_NAME"LogFileSem"
-
-
- sem_t*CAppLog::s_pLogFileSem=NULL;
- CAppLogg_oAppLog;
-
-
- CAppLog::CAppLog(){
-
if(s_pLogFileSem==NULL)
- {
- s_pLogFileSem=sem_open(LOG_SEM_NAME,O_CREAT,0666,1);
- }
- }
-
- CAppLog::~CAppLog(){
-
if(s_pLogFileSem!=NULL)
- {
- sem_close(s_pLogFileSem);
- s_pLogFileSem=NULL;
- }
- }
-
-
voidCAppLog::append(LOG_TYPEemType,constchar*lpszText)
- {
-
charszBuffer[10240]={0};
-
intnRet=readlink("/proc/self/exe",szBuffer,sizeof(szBuffer));
-
if(nRet<=0||nRet>=PATH_MAX)
-
return;
-
- stringstrTemp(szBuffer);
-
stringstrDir=strTemp.substr(0,strTemp.rfind('/')+1);
-
stringstrExe=strTemp.substr(strTemp.rfind('/')+1);
-
-
time_tulNow={0};
- time(&ulNow);
-
tm*pNow=localtime(&ulNow);
-
memset(szBuffer,0,sizeof(szBuffer));
-
sprintf(szBuffer,"%sLog/%04d/%04d-%02d/"
- ,strDir.c_str()
- ,1900+pNow->tm_year
- ,1900+pNow->tm_year
- ,1+pNow->tm_mon
- );
- CSystemHelper::createDirectory(szBuffer);
-
sprintf(szBuffer,"%sLog/%04d/%04d-%02d/%04d-%02d-%02d.log"
- ,strDir.c_str()
- ,1900+pNow->tm_year
- ,1900+pNow->tm_year
- ,1+pNow->tm_mon
- ,1900+pNow->tm_year
- ,1+pNow->tm_mon
- ,pNow->tm_mday
- );
-
- sem_wait(s_pLogFileSem);
-
- {
- ofstreamstream(szBuffer,ios::out|ios::app);
-
if(stream)
- {
-
charszFormated[20480]={0};
-
sprintf(szFormated,"/r/n%02d[%02d:%02d:%02dPID=%05dTID=%05ldEXE=%-15s]%s"
- ,emType
- ,pNow->tm_hour
- ,pNow->tm_min
- ,pNow->tm_sec
- ,getpid()
-
,(long)syscall(SYS_gettid)
- ,strExe.c_str()
- ,lpszText
- );
- stream<<szFormated;
- stream.flush();
- stream.close();
- }
- }
-
- sem_post(s_pLogFileSem);
- }
-
-
voidCAppLog::appendDeagnosticMsg(constchar*lpszFmt,...)
- {
-
va_listarg;
- va_start(arg,lpszFmt);
-
charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
-
- CAppLog::append(LOG_DEAGNOSTIC_MSG,szBuffer);
- }
-
-
voidCAppLog::appendEvent(constchar*lpszFmt,...)
- {
-
va_listarg;
- va_start(arg,lpszFmt);
-
charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
-
- CAppLog::append(LOG_EVENT,szBuffer);
- }
-
-
voidCAppLog::appendActivity(constchar*lpszFmt,...)
- {
-
va_listarg;
- va_start(arg,lpszFmt);
-
charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
-
- CAppLog::append(LOG_ACTIVITY,szBuffer);
- }
-
-
voidCAppLog::appendError(constchar*lpszFmt,...)
- {
-
va_listarg;
- va_start(arg,lpszFmt);
-
charszBuffer[10240]={0};
- vsprintf(szBuffer,lpszFmt,arg);
- va_end(arg);
-
- CAppLog::append(LOG_ERROR,szBuffer);
- }
-
使用起来很简单,将类加入到工程,然后在需要的地方包含此类的头文件,然后直接调用静态方法。这里用到了一个辅助类的函数 CSystemHelper::createDirectory,它的实现如下:
-
voidCSystemHelper::createDirectory(constchar*lpszDir)
- {
- stringstrDir(lpszDir);
-
intnIndex=0;
-
while(nIndex!=string::npos)
- {
-
intnFind=strDir.find("/",nIndex+1);
- stringstrTemp=strDir.substr(0,nFind+1);
- DIR*dir=opendir(strTemp.c_str());
-
if(dir!=NULL)
- closedir(dir);
-
else
- mkdir(strTemp.c_str(),S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
-
- nIndex=nFind;
- }
- }
分享到:
相关推荐
Linux C 实现日志打印功能
linux 下服务器程序会因为各种原因dump掉,就会影响用户使用,这里提供一个简单的进程监控和重启功能。 实现原理: 由定时任务crontab调用脚本,脚本用ps检查进程是否存在,如果不存在则重启并写入日志。 crontab...
【linux学习笔记-12】守护进程的日志实现 【linux学习笔记-13】基本进程通信--文件锁 【linux学习笔记-14】基本进程通信--信号 【linux学习笔记-15】基本进程通信--管道 【linux学习笔记-16】shell管道重定向程序的...
学习C++,学习windows/linux编程,学习网络编程,学习reactor模型,自己实现一遍练手; 包含(以下实现是Windows/Linux平台通用的,美名其曰跨平台) 基本工具库: 日志工具(日志优先级、同步日志、异步日志、带...
EasyLogger 是一款超轻量级(ROM, RAM)、高性能的 C/C++ 日志库,非常适合对资源敏感的软件项目,例如: IoT 产品、可穿戴设备、智能家居等等。相比 log4c、zlog 这些知名的 C/C++ 日志库, EasyLogger 的功能更加...
Linux环境下网络日志审计系统的实现,张志辉,,随着因特网的迅猛发展和国家信息化进程的加速,网络安全的形势更加严峻,迫切需要行之有效的网络安全保障技术。网络日志审计系统是�
【linux学习笔记-12】守护进程的日志实现.doc 【linux学习笔记-13】基本进程通信--文件锁.doc 【linux学习笔记-14】基本进程通信--信号.doc 【linux学习笔记-15】基本进程通信--管道.doc 【linux学习笔记-16】shell...
node-windows, Windows 对 node.js 脚本( 守护进程事件日志UAC等)的支持 更新 (2/11/17)Gitter自 2015年以来一直在解决...这个项目和它的姐妹项目( 节点 mac & 节点 linux )的数量是一个比我自己提供的更多的注意事项。
进程从创建(Linux 下调用 fork())到...在 Linux 0.11 上实现进程运行轨迹的跟踪 基本任务是在内核中维护一个日志文件*/var/process.log*,把操作系统启动到系统关机过程中所有进程的运行轨迹都记录在这一 log 文件中。
4、编写程序实现一个简单的员工档案管理系统,具备简单的员工资料增加、删除和查询等功能,并采用二进制文件保存员工的资料信息。 5、为了便于文件的管理和传输,某些时候需要将特别大的文件切割为多个指定长度小的...
守护进程是运行在后台的一种特殊进程,它独立于控制终端并且周期性地执行某种任务或循环等待处理某些事件的发生; 守护进程一般在系统启动时开始运行,除非强行终止,否则直到系统关机才随之一起停止运行; 守护进程...
2. 2 实现一个简单的shell程序 2. 3 shell编程 第3章 内核时钟 3. 1 关于时钟和定时器 3. 2 Linux系统时钟 3. 3 Linux系统定时器 3. 4 时钟命令介绍 3. 5 定时器的应用 第4章 内核模块 4. 1 概述 4. 2 模块实现机制 ...
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 临界区举例 ...
针对操作系统、内核安全,safeguard是一个基于eBPF的Linux审计观测工具,可以实现安全操作的拦截及审计记录。项目采用libbpfgo库,使用go语言实现顶层控制。日志记录配置范围内的行为。针对文件,进程,网络的安全...
用户之间安全隔离,一个用户出问题不影响其他用户。安全支持php、asp、asp.net、java、ruby等多种动态开发语言。 kangle web服务器主要特点: 1、免费开源 kangle技术团队希望国人拥有一款真正好用、易用、...
在linux下,C语言实现的模拟shell程序。能够显示每条命令使用的cpu时间、进程中断次数、换页次数。
init是内核启动的第一个用户空间进程,主要负责启动、终止系统中的基础服务进程。 Linux下,init主要有三个实现版本: System V,传统的init Upstart,Ubuntu后期针对sys-v的一个改进实现版本 systemd,是一套...
实验任务一: 编写一个Shell过程完成如下功能(必须在脚本中使用函数): 1、合并两个$1、$2文件为$3,并显示。 2、如果缺少$3,那么先报告缺少$3,然后将合并后的内容输出到mydoc.txt。如果有$3,就合并到...
12.6 实验一:使用proc文件系统的一个简单例子 12.7 实验二:利用/proc文件系统显示缺页状态 12.8 实验三:seq file使用例子 第13章 内核模块 13.1 什么是内核模块 13.2 模块实现机制 13.3 使用内核模块 13.4 实例 ...