//log打印日志,可以保存到文件中,当日志文件大于5M,自动备份到DebugLog2.log中,DebugLog1.log继续存放最新的日志,这样就保证5M以上的最新日志
#include <netinet/in.h> // for sockaddr_in
#include <sys/types.h> // for socket
#include <sys/socket.h> // for socket
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include <string.h> // for bzero
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
//==================================================================
// debug with time and show type with stdout or file
// if DebugLog1.log > 5M ,copy to DebugLog2.log save
//===================================================================
#define MAXLOGSIZE 5000000
#define MAXLINSIZE 16000
char logfilename1[]="DebugLog1.log";
char logfilename2[]="DebugLog2.log";
#define DEBUG_FILE 1
time_t timep;
struct tm *tmp;
typedef enum G_LOG_LEVEL
{
LOG_LEVEL_DEBUG = 0,
LOG_LEVEL_INFO,
LOG_LEVEL_WARN,
LOG_LEVEL_ERROR,
LOG_LEVEL_FATAL,
LOG_LEVEL_NONE,
}d_log_level_t;
d_log_level_t g_LogLevel;
void d_common_log_set_level(d_log_level_t LogLevel);
#define D_LOG_DEBUG(...) \
{\
if(g_LogLevel <= LOG_LEVEL_DEBUG)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[DEBUG] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[DEBUG] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_INFO(...) \
{\
if(g_LogLevel <= LOG_LEVEL_INFO)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[INFO] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[INFO] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_WARN(...) \
{\
if(g_LogLevel <= LOG_LEVEL_WARN)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[WARN] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[WARN] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_ERROR(...) \
{\
if(g_LogLevel <= LOG_LEVEL_ERROR)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[ERROR] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[ERROR] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_FATAL(...) \
{\
if(g_LogLevel <= LOG_LEVEL_FATAL)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[NOTICE] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[NOTICE] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
void d_common_log_set_level(d_log_level_t LogLevel)
{
g_LogLevel = LogLevel;
}
int main(int argc,char **argv)
{
d_common_log_set_level(LOG_LEVEL_DEBUG);
int i;
for (i=0;i<10000;i++) {
D_LOG_DEBUG("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
sleep(1);
}
return 0;
}
#include <netinet/in.h> // for sockaddr_in
#include <sys/types.h> // for socket
#include <sys/socket.h> // for socket
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include <string.h> // for bzero
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
//==================================================================
// debug with time and show type with stdout or file
// if DebugLog1.log > 5M ,copy to DebugLog2.log save
//===================================================================
#define MAXLOGSIZE 5000000
#define MAXLINSIZE 16000
char logfilename1[]="DebugLog1.log";
char logfilename2[]="DebugLog2.log";
#define DEBUG_FILE 1
time_t timep;
struct tm *tmp;
typedef enum G_LOG_LEVEL
{
LOG_LEVEL_DEBUG = 0,
LOG_LEVEL_INFO,
LOG_LEVEL_WARN,
LOG_LEVEL_ERROR,
LOG_LEVEL_FATAL,
LOG_LEVEL_NONE,
}d_log_level_t;
d_log_level_t g_LogLevel;
void d_common_log_set_level(d_log_level_t LogLevel);
#define D_LOG_DEBUG(...) \
{\
if(g_LogLevel <= LOG_LEVEL_DEBUG)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[DEBUG] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[DEBUG] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_INFO(...) \
{\
if(g_LogLevel <= LOG_LEVEL_INFO)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[INFO] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[INFO] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_WARN(...) \
{\
if(g_LogLevel <= LOG_LEVEL_WARN)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[WARN] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[WARN] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_ERROR(...) \
{\
if(g_LogLevel <= LOG_LEVEL_ERROR)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[ERROR] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[ERROR] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
#define D_LOG_FATAL(...) \
{\
if(g_LogLevel <= LOG_LEVEL_FATAL)\
{\
{ \
time(&timep); \
tmp=localtime(&timep); \
fprintf(stdout,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(stdout,"[NOTICE] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(stdout,__VA_ARGS__); \
fflush(stdout); \
}\
if (DEBUG_FILE == 1 ) \
{ \
FILE *fp = fopen(logfilename1,"a"); \
time(&timep); \
tmp=localtime(&timep); \
fprintf(fp,"[%4d-%02d-%02d %02d:%02d:%02d] ", \
(1900+tmp->tm_year), \
(1+tmp->tm_mon), \
tmp->tm_mday, \
tmp->tm_hour, \
tmp->tm_min, \
tmp->tm_sec); \
fprintf(fp,"[NOTICE] %s:%s:%d:\t", __FILE__, __FUNCTION__, __LINE__); \
fprintf(fp,__VA_ARGS__); \
if (ftell(fp)>MAXLOGSIZE) {\
fclose(fp);\
rename(logfilename1,logfilename2);\
}\
else \
fclose(fp); \
}\
}\
}
void d_common_log_set_level(d_log_level_t LogLevel)
{
g_LogLevel = LogLevel;
}
int main(int argc,char **argv)
{
d_common_log_set_level(LOG_LEVEL_DEBUG);
int i;
for (i=0;i<10000;i++) {
D_LOG_DEBUG("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
sleep(1);
}
return 0;
}