Commit 6a5a53b7 by Zeng Rixuan

no message

parent 9b2e452a
111
\ No newline at end of file
This diff is collapsed. Click to expand it.
QT += core
QT -= gui
CONFIG += c++11
TARGET = easylog
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
feature/easyloggingpp/my_easylogging.cpp \
feature/easyloggingpp/src/easylogging++.cc
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += \
feature/easyloggingpp/my_easylogging.h \
feature/easyloggingpp/inc/easylogging++.h \
feature/easyloggingpp/inc/normal_typedef.h
INCLUDEPATH += $$PWD/feature/easyloggingpp/inc
INCLUDEPATH += $$PWD/feature/easyloggingpp
This source diff could not be displayed because it is too large. You can view the blob instead.
#ifndef _NORMAL_TYPEDEF_H_
#define _NORMAL_TYPEDEF_H_
#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
/// SOCKET
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/time.h>
#include <pthread.h>
#if defined(_WIN32)
#include<WinSock2.h> //WindowsSocket
#include <ws2tcpip.h>
#include <windows.h>
#include <qdebug.h>
//typedef struct tagTHREADNAME_INFO
//{
// DWORD dwType; // must be 0x1000
// LPCSTR szName; // pointer to name (in user addr space)
// DWORD dwThreadID; // thread ID (-1=caller thread)
// DWORD dwFlags; // reserved for future use, must be zero
//} THREADNAME_INFO;
//void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
//{
// THREADNAME_INFO info;
// info.dwType = 0x1000;
// info.szName = szThreadName;
// info.dwThreadID = dwThreadID;
// info.dwFlags = 0;
// __try
// {
// RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info );
// }
// __catch(EXCEPTION_CONTINUE_EXECUTION){}
//}
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/prctl.h>
#endif
/// define c++11 std
#if 11
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#endif
/**
* @name define normal type;
*
** @} */
#if defined(_WIN32)
#else
typedef bool BOOLEAN;
typedef void VOID;
typedef int SOCKET;
#endif
typedef unsigned char UINT8;
typedef signed char INT8;
typedef unsigned char OCTET;
typedef char CHAR;
typedef unsigned short UINT16;
typedef signed short INT16;
typedef unsigned int UINT32;
typedef signed int INT32;
typedef unsigned long ULONG;
typedef signed long LONG;
typedef unsigned long long UINT64;
typedef long long INT64;
typedef float FLOAT;
typedef double DOUBLE;
typedef int SOCKET_FP;
/// for socket
#if defined(_WIN32)
#else
#define INVALID_SOCKET -1
typedef int SOCKET;
#endif
typedef int af_family;
typedef int in_port;
#define BACKLOG_LEN 10
#define SOCKET_SERVER_PORT 6666
#define SOCKET_CLIENT_PORT 9999
#define LOCAL_HOST "127.0.0.1" ///< 默认ip本地回环地址
///< 网络角色枚举
typedef enum
{
SOCKET_ROLE_SERVER = 0,
SOCKET_ROLE_CLIENT = 1
} SOCKET_ROLE_E;
///< 网络通讯协议的枚举
typedef enum
{
SOCKET_PROTO_TCP = 0,
SOCKET_PROTO_UDP = 1,
SOCKET_PROTO_UDP_MC = 2 ///< udp 组播通讯
} SOCKET_PROTO_E;
#if defined(_WIN32)
#else
#define TRUE true
#define FALSE false
#endif
///<记录返回状态
typedef enum
{
Lret_success = 0, ///< 成功
Lret_fail = 1, ///< 失败
Lret_timeout = 2, ///< 超时错误
Lret_accept_fail = 3,///< accept失败
Lret_connet_fail = 4, ///< 连接失败
Lret_recv_fail = 5,///< 接收失败
Lret_format_fail = 6,///< 格式打包失败
Lret_param_check_error = 7,///< 报文参数检查失败
Lret_max,
} Lret;
const char ret_str[][30] =
{
" success ",
" fail ",
" timeout ",
" accept_fail ",
" connet_fail ",
" recv_fail ",
" format_fail ",
" param_check_error "
} ;
void printf_init_log(char *log_info);
void printf_debug_log(char *log_info);
void En_G_LOG_DEBUG_INFO(int is_en);
void En_G_DEBUG_TERMINAL_LOG(int is_en);
#endif
#include "my_easylogging.h"
using namespace std;
// easylogging 相关内容
INITIALIZE_EASYLOGGINGPP
#if ELPP_FEATURE_CRASH_LOG
/**
* @brief myCrashHandler
* 程序奔溃前的回调函数,发出最后的呐喊,写下最后的log
* @param sig
*/
void myCrashHandler(int sig)
{
LOG(ERROR) << "Woops! Crashed!";
// FOLLOWING LINE IS ABSOLUTELY NEEDED AT THE END IN ORDER TO ABORT APPLICATION
el::Helpers::crashAbort(sig);
}
#endif
/**
* @brief rolloutHandler
* 日志备份,当文件超过一定大小后,就生成一个bak文件
* @param filename
* @param size
*/
void rolloutHandler(const char *filename, std::size_t size)
{
/// 备份日志
static unsigned int idx;
time_t nowtime;
struct tm *p;
time(&nowtime);
p = localtime(&nowtime);
#if defined(_WIN32)
cout << "###########LOG SIZE FULL#############" << endl;
std::stringstream ss;
string cc;
ss << "ren " << filename << " ./log/log_backup_" << ++idx << "_" << p->tm_year << p->tm_mon << p->tm_mday << ".log";
cc = ss.str();
replace(cc.begin(), cc.end(), '/', '\\');
system(cc.c_str());
#else
/*下面这段 需要优化*/
// system("mkdir bin");
std::stringstream ss;
std::time_t t = std::time(0); // t is an integer type
ss << "mv "
<< filename << " ./log/log_backup_" << ++idx << "_" << p->tm_year << p->tm_mon << p->tm_mday << ".log";
system(ss.str().c_str());
#endif
}
/**
* @brief easylogginginit
* easylogging初始化函数,加载配置文件,注册回调函数都在这里。
*/
void easylogginginit(int enable_log ,int enable_terminal)
{
/*
必须设置标记 LoggingFlag::StrictLogFileSizeCheck
否则,配置文件中MAX_LOG_FILE_SIZE = 1048576不生效
*/
el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
const string log_conf_file = R"(./log/log.conf)";
// const string log_conf_file = R"(/root/gitlab/xdevice-platform/doc/log.conf)";
// 加载配置文件,构造一个配置器对象
el::Configurations conf(log_conf_file.data());
// 用配置文件配置所有的日志记录器
el::Loggers::reconfigureAllLoggers(conf);
#if ELPP_FEATURE_CRASH_LOG
el::Helpers::setCrashHandler(myCrashHandler); //配置程序闪退的时候,再log中记录一下
#endif
/// 注册回调函数
el::Helpers::installPreRollOutCallback(rolloutHandler);
}
/**
* @brief
* @param size 日志长度
* @param str 日志内容
* @param logLevel 当前日志等级
*/
void logStrOut(int size, std::string str, LEVEL_LOG logLevel)
{
switch (logLevel)
{
case MY_ERR:
// LOG(ERROR) << "[Arr Count:" << size << "]" << str;
LOG(ERROR) << str;
break;
case MY_Debug:
// LOG(DEBUG) << "[Arr Count:" << size << "]" << str;
LOG(DEBUG) << str;
break;
case MY_Info:
// LOG(INFO) << "[Arr Count:" << size << "]" << str;
LOG(INFO) << str;
break;
case MY_Trace:
// LOG(TRACE) << "[Arr Count:" << size << "]" << str;
LOG(TRACE) << str;
break;
case MY_Warn:
// LOG(WARNING) << "[Arr Count:" << size << "]" << str;
LOG(WARNING) << str;
break;
default:
break;
}
}
/**
* @brief
* 对char *型特例化
* @tparam
* @param info My Param doc
* @param buff My Param doc
* @param size My Param doc
* @param logLevel My Param doc
*/
template <>
void logDump(char *info, char *buff, int size, LEVEL_LOG logLevel)
{
std::string str(info);
str += "(ASCII): ";
if (buff == NULL || size <= 0)
str += "buff is null!";
else
{
std::stringstream ss;
for (int _dumpArrSize = 0; _dumpArrSize < size; ++_dumpArrSize)
ss << static_cast<int>(buff[_dumpArrSize]) << " ";
str += ss.str();
}
logStrOut(size, str, logLevel);
}
/**
* @brief
* 对UINT8 *型特例化 安装16进制打印
* @tparam
* @param info My Param doc
* @param buff My Param doc
* @param size My Param doc
* @param logLevel My Param doc
*/
template <>
void logDump(char *info, UINT8 *buff, int size, LEVEL_LOG logLevel)
{
std::string str(info);
str += "(HEX): ";
if (buff == NULL || size <= 0)
str += "buff is null!";
else
{
std::stringstream ss, s1;
for (int _dumpArrSize = 0; _dumpArrSize < size; ++_dumpArrSize)
// s1 =(buff[_dumpArrSize];
ss << std::hex << static_cast<uint>(buff[_dumpArrSize]) << " ";
str += ss.str();
}
logStrOut(size, str, logLevel);
}
// template <typename T>
// void fun(T a)
// {
// cout << "The main template fun(): " << a << endl;
// }
// template <> // 对int型特例化
// void fun(int a)
// {
// cout << "Specialized template for int type: " << a << endl;
// }
/**
* @brief
* 测试函数
*/
void log_test()
{
// char aa = 4;
// char *cc = "12345";
UINT8 cc[] = {0X31, 0X31, 0X31};
char buf[3] = {5, 6, 7};
//INFO测试
LOG(INFO) << "》》》》》》》》》》打印测试《《《《《《《《";
LOG(INFO) << " UINT8 cc[] = {0X31, 0X31, 0X31};";
LOG(INFO) << " char buf[3] = {5, 6, 7};";
LOG(INFO) << "#################封装之前###########";
LOG(INFO) << "打印cc :" << cc;
LOG(INFO) << "打印buf[3]:"
<< "buf = " << buf << " buf[0] = " << buf[0] << " buf[1] = " << buf[1] << " (INT8)buf[2] = " << (UINT8)(buf[2]);
LOG(INFO) << "####################封装之后##########";
LogInfoDump("打印cc", cc, 3);
LogInfoDump("打印buf[3]", buf, 3);
//ERROR测试
LOG(ERROR) << "》》》》》》》》》》打印测试《《《《《《《《";
LOG(ERROR) << " UINT8 cc[] = {0X31, 0X31, 0X31};";
LOG(ERROR) << " char buf[3] = {5, 6, 7};";
LogErrorDump("打印cc", cc, 3);
LogErrorDump("打印buf[3]", buf, 3);
}
#ifndef _MY_EASYLOGGING_H_
#define _MY_EASYLOGGING_H_
#include "easylogging++.h"
#include "normal_typedef.h"
#include <iostream>
enum LEVEL_LOG
{
MY_ERR = 0,
MY_Warn,
MY_Debug,
MY_Trace,
MY_Info
};
void myCrashHandler(int sig);
void rolloutHandler(const char *filename, std::size_t size);
void easylogginginit(int enable_log ,int enable_terminal);
void logStrOut(int size, std::string str, LEVEL_LOG logLevel);
#define LogInfoDump(info, buff, size) logDump(info, buff, size, LEVEL_LOG::MY_Info)
#define LogTraceDump(info, buff, size) logDump(info, buff, size, LEVEL_LOG::MY_Trace)
#define LogWarnDump(info, buff, size) logDump(info, buff, size, LEVEL_LOG::MY_Warn)
#define LogErrorDump(info, buff, size) logDump(info, buff, size, LEVEL_LOG::MY_ERR)
#define LogDebugDump(info, buff, size) logDump(info, buff, size, LEVEL_LOG::MY_Debug)
/**
* @brief
* 能方便输出:char buf[len] , int buf[lend]等数组,
* 而无需区分数组元素是字符类型还是整数类型。
* 能方便输出数据包。
* @tparam _TYPE
* @param info 日志头
* @param buff 日志内容
* @param size buff的size
* @param logLevel 日志等级
*/
template <typename T1>
void logDump(char *info, T1 *buff, int size, LEVEL_LOG logLevel)
{
std::string str(info);
str += ": ";
if (buff == NULL || size <= 0)
str += "buff is null!";
else
{
std::stringstream ss;
for (int _dumpArrSize = 0; _dumpArrSize < size; ++_dumpArrSize)
ss << (buff[_dumpArrSize]) << ",";
str += ss.str();
}
logStrOut(size, str, logLevel);
}
template <>
void logDump(char *info, char *buff, int size, LEVEL_LOG logLevel);
template <>
void logDump(char *info, UINT8 *buff, int size, LEVEL_LOG logLevel);
void log_test();
// template <typename T>
// void fun(T a)
// {
// std::cout << "The main template fun(): " << a << std::endl;
// }
// template <> // 对int型特例化
// void fun(int a);
#endif
// template <>
// void logDump(char *info, unsigned char *buff, int size, LEVEL_LOG logLevel);
// template <>
// void logDump(char *info, const char *buff, int size, LEVEL_LOG logLevel);
// template <>
// void logDump(char *info, const unsigned char *buff, int size, LEVEL_LOG logLevel);
This source diff could not be displayed because it is too large. You can view the blob instead.
* GLOBAL:
FORMAT = "%datetime:[%level]%msg"
ENABLED = true
TO_FILE = true
TO_STANDARD_OUTPUT = false
PERFORMANCE_TRACKING = false
MAX_LOG_FILE_SIZE = 209715200
FILENAME = "./log/info_%datetime{%Y%M%d}.log"
* INFO:
FORMAT = "%datetime %msg"
ENABLED = true
* DEBUG:
FORMAT = "%datetime:[%level] %func[%line] %msg"
ENABLED = true
* WARNING:
ENABLED = true
* TRACE:
ENABLED = false
* VERBOSE:
FORMAT = "%datetime{%d/%M/%y} | %level-%vlevel | %msg"
ENABLED = true
* ERROR:
FORMAT = "%datetime:[%level] %func[%line] %msg"
ENABLED = true
TO_STANDARD_OUTPUT = true
* FATAL:
ENABLED = true
TO_STANDARD_OUTPUT = true
#include <stdio.h>
#include <iostream>
#include "./feature/easyloggingpp/my_easylogging.h"
// #define HDDD
int main()
{
easylogginginit(1,1); //日志初始化
log_test(); //日志自测函数 里面可以看细节的用法
printf("Hello world\n\r");
#if 0
while (1)
{
LOG(INFO) << "》》》》》》》》》》打印测试11111《《《《《《《《";
// sleep(1);//秒
usleep(1 * 1000); //微秒
// printf("HDDD not def\n\r");
// std::cout << "An exception occurred. Exception Nr. " << std::endl;
LOG(INFO) << "》》》》》》》》》》打印测试《《《《《《《《";
LOG(INFO) << "》》》》》》》》》》打印测试2222《《《《《《《《";
}
#endif
try
{
throw 20;
}
catch (int e)
{
std::cout << "An exception occurred. Exception Nr. " << e << '\n';
}
#ifdef HDDD
printf("HDDD def\n\r");
#else
printf("HDDD not def\n\r");
// std::cout << HDDD << std::endl;
#endif // #
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment