linux c/c++ 后台开发基础之:c++日志模块

日志是服务端开发必不可少的模块,工作过几个公司,基本每个公司或者项目组都有自己的日志模块,有使用开源的,也有自己写的,这是自己写的一个日志模块,经过线上大并发的测试,性能良好,使用也较方便。该日志采用的是单例模式,支持自动按天分文件,按日志行数自动分文件,是多线程安全的单例模式,没有外部依赖,linux操作系统均可以使用。

  1. /******************************************************** 
  2. function:log class 
  3. version:1.0 
  4. date:2013.4.19 
  5. modify:2014.12.21 
  6. *********************************************************/  
  7.   
  8. #ifndef LOG_H   
  9. #define LOG_H   
  10.   
  11. #include <stdio.h>   
  12. #include <iostream>   
  13. #include <stdarg.h>   
  14. #include <pthread.h>   
  15. using namespace std;  
  16.   
  17. class Log  
  18. {  
  19.     public:  
  20.         static Log* get_instance()  
  21.         {  
  22.             static Log instance;  
  23.             return &instance;  
  24.         }  
  25.   
  26.         bool init(const char* file_name, int log_buf_size = 8192, int split_lines = 5000000);  
  27.   
  28.         void write_log(int level, const char* format, ...);  
  29.   
  30.         void flush(void);  
  31.   
  32.     private:  
  33.         Log();  
  34.         virtual ~Log();  
  35.   
  36.     private:  
  37.         pthread_mutex_t *m_mutex;  
  38.         char dir_name[128];  
  39.         char log_name[128];  
  40.         int m_split_lines;  
  41.         int m_log_buf_size;  
  42.         long long  m_count;  
  43.         int m_today;  
  44.         FILE *m_fp;  
  45.         char *m_buf;  
  46. };  
  47.   
  48. #define LOG_DEBUG(format, ...) Log::get_instance()->write_log(0, format, __VA_ARGS__)   
  49. #define LOG_INFO(format, ...) Log::get_instance()->write_log(1, format, __VA_ARGS__)   
  50. #define LOG_WARN(format, ...) Log::get_instance()->write_log(2, format, __VA_ARGS__)   
  51. #define LOG_ERROR(format, ...) Log::get_instance()->write_log(3, format, __VA_ARGS__)   
  52.   
  53. #endif   
/********************************************************
function:log class
version:1.0
date:2013.4.19
modify:2014.12.21
*********************************************************/

#ifndef LOG_H
#define LOG_H

#include <stdio.h>
#include <iostream>
#include <stdarg.h>
#include <pthread.h>
using namespace std;

class Log
{
	public:
		static Log* get_instance()
		{
			static Log instance;
			return &instance;
		}

		bool init(const char* file_name, int log_buf_size = 8192, int split_lines = 5000000);

		void write_log(int level, const char* format, ...);

		void flush(void);

	private:
		Log();
		virtual ~Log();

	private:
		pthread_mutex_t *m_mutex;
		char dir_name[128];
		char log_name[128];
		int m_split_lines;
		int m_log_buf_size;
		long long  m_count;
		int m_today;
		FILE *m_fp;
		char *m_buf;
};

#define LOG_DEBUG(format, ...) Log::get_instance()->write_log(0, format, __VA_ARGS__)
#define LOG_INFO(format, ...) Log::get_instance()->write_log(1, format, __VA_ARGS__)
#define LOG_WARN(format, ...) Log::get_instance()->write_log(2, format, __VA_ARGS__)
#define LOG_ERROR(format, ...) Log::get_instance()->write_log(3, format, __VA_ARGS__)

#endif 

  1. /******************************************************** 
  2. function:log class 
  3. version:1.0 
  4. date:2013.4.19 
  5. modify:2014.12.21 
  6. *********************************************************/  
  7. #include <string.h>   
  8. #include <time.h>   
  9. #include <sys/time.h>   
  10. #include <stdarg.h>   
  11. #include "log.h"   
  12. #include <pthread.h>   
  13. using namespace std;  
  14.   
  15. Log::Log()  
  16. {  
  17.     m_count = 0;  
  18.     m_mutex = new pthread_mutex_t;  
  19.     pthread_mutex_init(m_mutex, NULL);  
  20. }  
  21.   
  22. Log::~Log()  
  23. {  
  24.     if(m_fp != NULL)  
  25.     {  
  26.         fclose(m_fp);  
  27.     }  
  28.     pthread_mutex_destroy(m_mutex);  
  29.   
  30.     if(m_mutex != NULL)  
  31.     {  
  32.         delete m_mutex;  
  33.     }  
  34. }  
  35.   
  36. bool Log::init(const char* file_name, int log_buf_size, int split_lines)  
  37. {  
  38.     m_log_buf_size = log_buf_size;  
  39.     m_buf = new char[m_log_buf_size];  
  40.     memset(m_buf, '\0'sizeof(m_buf));  
  41.     m_split_lines = split_lines;  
  42.   
  43.     time_t t = time(NULL);  
  44.     struct tm* sys_tm = localtime(&t);  
  45.     struct tm my_tm = *sys_tm;  
  46.     char *p = strrchr(file_name, '/');  
  47.     char log_full_name[256] = {0};  
  48.     if(p == NULL)  
  49.     {  
  50.         snprintf(log_full_name, 255, "%d_%02d_%02d_%s",my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday, file_name);     
  51.     }  
  52.     else  
  53.     {  
  54.         strcpy(log_name, p+1);  
  55.         strncpy(dir_name, file_name, p - file_name + 1);  
  56.         snprintf(log_full_name, 255, "%s%d_%02d_%02d_%s",dir_name, my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday, log_name );   
  57.     }  
  58.   
  59.     m_today = my_tm.tm_mday;  
  60.   
  61.     m_fp = fopen(log_full_name, "a");  
  62.     if(m_fp == NULL)  
  63.     {  
  64.         return false;  
  65.     }  
  66.   
  67.     return true;  
  68. }  
  69.   
  70. void Log::write_log(int level, const char* format, ...)  
  71. {  
  72.     struct timeval now = {0,0};  
  73.     gettimeofday(&now, NULL);  
  74.     time_t t = now.tv_sec;  
  75.     struct tm* sys_tm = localtime(&t);  
  76.     struct tm my_tm = *sys_tm;  
  77.     char s[16] = {0};  
  78.     switch(level)  
  79.     {  
  80.         case 0 : strcpy(s, "[debug]:"); break;  
  81.         case 1 : strcpy(s, "[info]:"); break;  
  82.         case 2 : strcpy(s, "[warn]:"); break;  
  83.         case 3 : strcpy(s, "[erro]:"); break;  
  84.         default:  
  85.                strcpy(s, "[info]:"); break;  
  86.     }  
  87.     int n = snprintf(m_buf, 48, "%d-%02d-%02d %02d:%02d:%02d.%06d %s ",  
  88.             my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday,  
  89.             my_tm.tm_hour, my_tm.tm_min, my_tm.tm_sec, now.tv_usec, s);  
  90.   
  91.     pthread_mutex_lock(m_mutex);  
  92.     m_count++;  
  93.     if(m_today != my_tm.tm_mday || m_count % m_split_lines == 0) //everyday log   
  94.     {  
  95.         char new_log[256] = {0};  
  96.         fflush(m_fp);  
  97.         fclose(m_fp);  
  98.         char tail[16] = {0};  
  99.         snprintf(tail, 16,  "%d_%02d_%02d_", my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday);  
  100.         if(m_today != my_tm.tm_mday)  
  101.         {  
  102.             snprintf(new_log, 255, "%s%s%s", dir_name, tail, log_name);  
  103.             m_today = my_tm.tm_mday;  
  104.             m_count = 0;  
  105.         }  
  106.         else  
  107.         {  
  108.             snprintf(new_log, 255, "%s%s%s.%d", dir_name, tail, log_name, m_count/m_split_lines);  
  109.         }  
  110.         m_fp = fopen(new_log, "a");  
  111.     }  
  112.     pthread_mutex_unlock(m_mutex);  
  113.       
  114.     va_list valst;  
  115.     va_start(valst, format);  
  116.   
  117.     pthread_mutex_lock(m_mutex);  
  118.     int m = vsnprintf(m_buf + n, m_log_buf_size-1, format, valst);  
  119.     m_buf[n + m - 1] = '\n';  
  120.     fputs(m_buf, m_fp);  
  121.     pthread_mutex_unlock(m_mutex);  
  122.   
  123.     va_end(valst);  
  124. }  
  125.   
  126. void Log::flush(void)  
  127. {  
  128.     pthread_mutex_lock(m_mutex);  
  129.     fflush(m_fp);  
  130.     pthread_mutex_unlock(m_mutex);  
  131. }  
/********************************************************
function:log class
version:1.0
date:2013.4.19
modify:2014.12.21
*********************************************************/
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#include "log.h"
#include <pthread.h>
using namespace std;

Log::Log()
{
	m_count = 0;
	m_mutex = new pthread_mutex_t;
	pthread_mutex_init(m_mutex, NULL);
}

Log::~Log()
{
	if(m_fp != NULL)
	{
		fclose(m_fp);
	}
	pthread_mutex_destroy(m_mutex);

	if(m_mutex != NULL)
	{
		delete m_mutex;
	}
}

bool Log::init(const char* file_name, int log_buf_size, int split_lines)
{
	m_log_buf_size = log_buf_size;
	m_buf = new char[m_log_buf_size];
	memset(m_buf, '\0', sizeof(m_buf));
	m_split_lines = split_lines;

	time_t t = time(NULL);
	struct tm* sys_tm = localtime(&t);
	struct tm my_tm = *sys_tm;
	char *p = strrchr(file_name, '/');
	char log_full_name[256] = {0};
	if(p == NULL)
	{
		snprintf(log_full_name, 255, "%d_%02d_%02d_%s",my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday, file_name);	
	}
	else
	{
		strcpy(log_name, p+1);
		strncpy(dir_name, file_name, p - file_name + 1);
		snprintf(log_full_name, 255, "%s%d_%02d_%02d_%s",dir_name, my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday, log_name ); 
	}

	m_today = my_tm.tm_mday;

	m_fp = fopen(log_full_name, "a");
	if(m_fp == NULL)
	{
		return false;
	}

	return true;
}

void Log::write_log(int level, const char* format, ...)
{
	struct timeval now = {0,0};
	gettimeofday(&now, NULL);
	time_t t = now.tv_sec;
	struct tm* sys_tm = localtime(&t);
	struct tm my_tm = *sys_tm;
	char s[16] = {0};
	switch(level)
	{
		case 0 : strcpy(s, "[debug]:"); break;
		case 1 : strcpy(s, "[info]:"); break;
		case 2 : strcpy(s, "[warn]:"); break;
		case 3 : strcpy(s, "[erro]:"); break;
		default:
			   strcpy(s, "[info]:"); break;
	}
	int n = snprintf(m_buf, 48, "%d-%02d-%02d %02d:%02d:%02d.%06d %s ",
			my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday,
			my_tm.tm_hour, my_tm.tm_min, my_tm.tm_sec, now.tv_usec, s);

	pthread_mutex_lock(m_mutex);
	m_count++;
	if(m_today != my_tm.tm_mday || m_count % m_split_lines == 0) //everyday log
	{
		char new_log[256] = {0};
		fflush(m_fp);
		fclose(m_fp);
		char tail[16] = {0};
		snprintf(tail, 16,  "%d_%02d_%02d_", my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday);
		if(m_today != my_tm.tm_mday)
		{
			snprintf(new_log, 255, "%s%s%s", dir_name, tail, log_name);
			m_today = my_tm.tm_mday;
			m_count = 0;
		}
		else
		{
			snprintf(new_log, 255, "%s%s%s.%d", dir_name, tail, log_name, m_count/m_split_lines);
		}
		m_fp = fopen(new_log, "a");
	}
	pthread_mutex_unlock(m_mutex);
	
	va_list valst;
	va_start(valst, format);

	pthread_mutex_lock(m_mutex);
	int m = vsnprintf(m_buf + n, m_log_buf_size-1, format, valst);
	m_buf[n + m - 1] = '\n';
	fputs(m_buf, m_fp);
	pthread_mutex_unlock(m_mutex);

	va_end(valst);
}

void Log::flush(void)
{
	pthread_mutex_lock(m_mutex);
	fflush(m_fp);
	pthread_mutex_unlock(m_mutex);
}


  1. #include "log.h"   
  2. void *f(void* args)  
  3. {  
  4.     for(int i = 0;i < 100000; i++)  
  5.     {  
  6.             Log::get_instance()->write_log(1, "d=%d,c=%c,s=%s,f=%f", i,'a',"log", 1.000);  
  7.             Log::get_instance()->write_log(2, "d=%d,c=%c,s=%s,f=%f", i,'a',"log", 1.000);  
  8.             Log::get_instance()->write_log(3, "d=%d,c=%c,s=%s,f=%f", i,'a',"log", 1.000);  
  9.   
  10.             LOG_INFO("%d", 123456789);  
  11.             LOG_ERROR("%d", 123456789);  
  12.             LOG_DEBUG("%d", 123456789);  
  13.             LOG_WARN("%d", 123456789);  
  14.   
  15.     }  
  16. }  
  17.   
  18. int main()  
  19. {  
  20.     Log::get_instance()->init("./mylog.log", 100, 2000000);  
  21.     int i = 0;  
  22.     Log::get_instance()->write_log(1, "d=%d,c=%c,s=%s,f=%f", i,'a',"log", 1.000);  
  23.     Log::get_instance()->write_log(2, "d=%d,c=%c,s=%s,f=%f", i,'a',"log", 1.000);  
  24.     Log::get_instance()->write_log(3, "d=%d,c=%c,s=%s,f=%f", i,'a',"log", 1.000);  
  25.   
  26.     LOG_INFO("%d", 123456789);  
  27.     LOG_ERROR("%d", 123456789);  
  28.     LOG_DEBUG("%d", 123456789);  
  29.     LOG_WARN("%d", 123456789);  
  30.   
  31.     pthread_t id;  
  32.     for(int i = 0; i < 10; i++)  
  33.     {  
  34.         pthread_create(&id, NULL, f, NULL);  
  35.         pthread_join(id,NULL);  
  36.     }  
  37.   
  38.     for(;;)  
  39.     {  
  40.         sleep(5);  
  41.         Log::get_instance()->flush();  
  42.     }  
  43.   
  44.     return 0;  
  45. }  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值