Skip to content

Commit

Permalink
随便的加了个日志的加密接口
Browse files Browse the repository at this point in the history
  • Loading branch information
daixian committed Apr 12, 2021
1 parent f3755fd commit dc5fcb8
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 56 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
2. 内部从glog换成的spdlog。
3. 完全去掉了boost,换成了Poco库。
4. 使用conan的CI已经完成 -> [Release下载(x64,x86)](https://github.com/daixian/dlog/releases)
5. 随便的加了个日志的加密接口。

## 编译库的方法
1. 使用pip安装conan构建工具:
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class DlogConan(ConanFile):
name = "dlog"
version = "2.6.7"
version = "2.6.8"
license = "WTFPL???"
author = "daixian<[email protected]>"
url = "https://github.com/daixian/dlog"
Expand Down
64 changes: 36 additions & 28 deletions src/dlog/Common/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,18 @@ void Debug::init(const char* logDir, const char* program, INIT_RELATIVE rel)
isInitFail = false;
}
catch (const std::exception& e) {
//std::cerr << "Debug.init():初始化失败!" << e.what() << '\n';
isInitFail = true; //标记初始化失败过了
mt.unlock();
clear();
throw e;
}

mt.unlock();
//检查看看日志文件是否存在了,如果filelogger指针为null,或者连文件也不存在
if (filelogger == nullptr || !File(logFilePath).exists()) {
string msg = "Debug.init():无法创建日志文件 -> " + logFilePath; //底下马上要clear(),所以这里先写了
clear();
isInitFail = true; //标记初始化失败过了
//std::cerr << msg << '\n';
throw std::invalid_argument(msg);
if (isFileEnable && (filelogger == nullptr || !File(logFilePath).exists())) {
//string msg = "Debug.init():无法创建日志文件 -> " + logFilePath; //底下马上要clear(),所以这里先写了
if (!isInitFail) {
isInitFail = true; //标记初始化失败过了
clear();
}
}
else {
//清空老文件
Expand Down Expand Up @@ -143,6 +140,10 @@ void Debug::clear()
logFileThr = spdlog::level::level_enum::debug;
logMemoryThr = spdlog::level::level_enum::debug;
logConsoleThr = spdlog::level::level_enum::debug;

isEncryptConsole = false;
isEncryptFile = false;

MemoryLog::GetInst()->clear();

logPattern = _logPattern;
Expand All @@ -158,6 +159,8 @@ void Debug::clear()
spdlog::drop_all(); //这里要注意删除,否则无法再创建同名的

exLoggerCallback = nullptr;
exLoggerEncryptCallback = nullptr;
exLoggerEncrypDeletetCallback = nullptr;

programName.clear();
logDirPath.clear();
Expand All @@ -168,6 +171,8 @@ void Debug::clear()
consolelogger = nullptr;
spdlog::drop_all();
exLoggerCallback = nullptr;
exLoggerEncryptCallback = nullptr;
exLoggerEncrypDeletetCallback = nullptr;
}
mt.unlock();
}
Expand Down Expand Up @@ -230,36 +235,39 @@ void Debug::setIsConsoleEnable(bool enable)
{
isConsoleEnable = enable;

//如果是设置为不使能,那么就直接退出
//如果是设置为不使能那么就直接退出
if (isConsoleEnable == false) {
return;
}

//设置控制台为UTF8输出
try {
//如果是设置为使能,如果指针还没有初始化过
if (consolelogger == nullptr) {
//ischcp65001设置控制台为UTF8输出
#if defined(_WIN32) || defined(_WIN64)
if (ischcp65001) {
system("chcp 65001"); //测试了在windows下有效
}
if (ischcp65001) {
system("chcp 65001"); //测试了在windows下有效
}
#endif

//如果是设置为使能,如果指针还没有初始化过
if (consolelogger == nullptr) {
consolelogger = spdlog::stdout_color_mt("console");
consolelogger = spdlog::stdout_color_mt("console");

consolelogger->set_level(spdlog::level::trace);
auto console_sink = dynamic_cast<spdlog::sinks::stdout_color_sink_mt*>(consolelogger->sinks().back().get());
consolelogger->set_level(spdlog::level::trace);
auto console_sink = dynamic_cast<spdlog::sinks::stdout_color_sink_mt*>(consolelogger->sinks().back().get());

#if defined(_WIN32) || defined(_WIN64)
//win下好像可以选择的颜色不多
//console_sink->set_color(spdlog::level::trace, console_sink->BOLD);
//console_sink->set_color(spdlog::level::debug, console_sink->CYAN);
//新版本的API貌似改了
console_sink->set_color(spdlog::level::info, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
//win下好像可以选择的颜色不多
//console_sink->set_color(spdlog::level::trace, console_sink->BOLD);
//console_sink->set_color(spdlog::level::debug, console_sink->CYAN);
//新版本的API貌似改了
console_sink->set_color(spdlog::level::info, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#else
//console_sink->set_color(spdlog::level::info, console_sink->white);
//console_sink->set_color(spdlog::level::info, console_sink->white);
#endif
//console_sink->set_color(spdlog::level::warn, console_sink->YELLOW);
//console_sink->set_color(spdlog::level::err, console_sink->RED);
//console_sink->set_color(spdlog::level::warn, console_sink->YELLOW);
//console_sink->set_color(spdlog::level::err, console_sink->RED);
}
}
catch (const std::exception&) {
}
}

Expand Down
44 changes: 40 additions & 4 deletions src/dlog/Common/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ enum class INIT_RELATIVE
MODULE = 1,
};

// 日志回调
// 日志外部回调
typedef void (*LoggerCallback)(int level, const char* message);

//加密日志的函数回调
typedef const char* (*LoggerEncryptCallback)(const char* message, void*& ptr);

//释放加密文本对象的方法
typedef void (*LoggerEncrypDeletetCallback)(void* deletePtr);

/**
* Debug日志系统.
*
Expand Down Expand Up @@ -118,6 +124,12 @@ class Debug
// 是否使能文件日志.
bool isFileEnable = true;

// 是否加密文件日志
bool isEncryptFile = false;

// 是否加密控制台
bool isEncryptConsole = false;

// 大于等于这个优先级的常规日志(文件日志)都会工作.
spdlog::level::level_enum logFileThr = spdlog::level::level_enum::debug;

Expand All @@ -139,9 +151,15 @@ class Debug
// 是否初始化失败了.
bool isInitFail = false;

// 外部传入的logger指针
// 外部传入的logger指针.
LoggerCallback exLoggerCallback = nullptr;

// 外部的加密函数指针.
LoggerEncryptCallback exLoggerEncryptCallback = nullptr;

// 文本对象的释放方法
LoggerEncrypDeletetCallback exLoggerEncrypDeletetCallback = nullptr;

// 是否执行chcp 65001
bool ischcp65001 = false;

Expand Down Expand Up @@ -249,10 +267,28 @@ class Debug
void LogMsg(spdlog::level::level_enum logThr, const char* msg)
{
if (isFileEnable && logFileThr <= logThr && filelogger != nullptr) { //满足优先级才输出 - 文件
filelogger->log(logThr, msg);
if (isEncryptFile && exLoggerEncryptCallback != nullptr) {
//如果设置了加密日志
void* deletePtr = nullptr;
filelogger->log(logThr, exLoggerEncryptCallback(msg, deletePtr));
if (exLoggerEncrypDeletetCallback != nullptr) //如果有释放方法则释放
exLoggerEncrypDeletetCallback(deletePtr);
}
else {
filelogger->log(logThr, msg);
}
}
if (isConsoleEnable && logConsoleThr <= logThr && consolelogger != nullptr) { //满足优先级才输出 - 控制台
consolelogger->log(logThr, msg);
if (isEncryptConsole && exLoggerEncryptCallback != nullptr) {
//如果设置了加密日志
void* deletePtr = nullptr;
consolelogger->log(logThr, exLoggerEncryptCallback(msg, deletePtr));
if (exLoggerEncrypDeletetCallback != nullptr) //如果有释放方法则释放
exLoggerEncrypDeletetCallback(deletePtr);
}
else {
consolelogger->log(logThr, msg);
}
}
if (isMemLogEnable && logMemoryThr <= logThr) { //满足优先级才输出 - 内存队列
MemoryLog::GetInst()->addLog(msg);
Expand Down
51 changes: 40 additions & 11 deletions src/dlog/dlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ extern "C" DLOG_EXPORT void* __cdecl dlog_global_ptr()
extern "C" DLOG_EXPORT int __cdecl dlog_init(const char* logDir, const char* program, dlog_init_relative dir_relatvie,
bool isForceInit)
{
//这个函数要主动清空初始化错误标志
Debug::GetInst()->isInitFail = false;

if (isForceInit == false) {
if (!Debug::GetInst()->isInit) {
Debug::GetInst()->init(logDir, program, (INIT_RELATIVE)dir_relatvie);
if (Debug::GetInst()->isInitFail)
return -1;
return 0; //第一次初始化
}
return 1; //成功复用
Expand All @@ -35,6 +40,8 @@ extern "C" DLOG_EXPORT int __cdecl dlog_init(const char* logDir, const char* pro
strcmp(program, "dlog") != 0) { //同时第二次设置的这个程序名不能等于默认名字
Debug::GetInst()->clear();
Debug::GetInst()->init(logDir, program, (INIT_RELATIVE)dir_relatvie);
if (Debug::GetInst()->isInitFail)
return -1;
return 2; //强制重设了一次glog
}
return 3; //强制重设了一次glog
Expand Down Expand Up @@ -75,7 +82,10 @@ extern "C" DLOG_EXPORT void __cdecl dlog_enable(bool enable)

extern "C" DLOG_EXPORT void __cdecl dlog_console_log_enable(bool enable)
{
Debug::GetInst()->setIsConsoleEnable(enable);
if (!Debug::GetInst()->isInit)
Debug::GetInst()->isConsoleEnable = enable;
else
Debug::GetInst()->setIsConsoleEnable(enable);
}

extern "C" DLOG_EXPORT void __cdecl dlog_file_log_enable(bool enable)
Expand Down Expand Up @@ -132,7 +142,7 @@ extern "C" DLOG_EXPORT void __cdecl LogI(const char* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::info)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

std::vector<char> buf(DEBUG_LOG_BUFF_SIZE, '\0');
Expand Down Expand Up @@ -168,7 +178,7 @@ extern "C" DLOG_EXPORT void __cdecl LogW(const char* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::warn)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

std::vector<char> buf(DEBUG_LOG_BUFF_SIZE, '\0');
Expand Down Expand Up @@ -204,7 +214,7 @@ extern "C" DLOG_EXPORT void __cdecl LogE(const char* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::err)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

std::vector<char> buf(DEBUG_LOG_BUFF_SIZE, '\0');
Expand Down Expand Up @@ -240,7 +250,7 @@ extern "C" DLOG_EXPORT void __cdecl LogD(const char* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::debug)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

std::vector<char> buf(DEBUG_LOG_BUFF_SIZE, '\0');
Expand Down Expand Up @@ -276,7 +286,7 @@ extern "C" DLOG_EXPORT void __cdecl wLogI(const wchar_t* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::info)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

//这个函数原来使用的是vswprintf,但是跨平台有问题,如果包含中文则失败。
Expand Down Expand Up @@ -306,7 +316,7 @@ extern "C" DLOG_EXPORT void __cdecl wLogW(const wchar_t* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::warn)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

//这个函数原来使用的是vswprintf,但是跨平台有问题,如果包含中文则失败。
Expand Down Expand Up @@ -348,7 +358,7 @@ extern "C" DLOG_EXPORT void __cdecl wLogE(const wchar_t* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::err)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

//这个函数原来使用的是vswprintf,但是跨平台有问题,如果包含中文则失败。
Expand Down Expand Up @@ -390,7 +400,7 @@ extern "C" DLOG_EXPORT void __cdecl wLogD(const wchar_t* strFormat, ...)
{
if (Debug::GetInst()->isNeedLog(spdlog::level::level_enum::debug)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

//这个函数原来使用的是vswprintf,但是跨平台有问题,如果包含中文则失败。
Expand Down Expand Up @@ -431,7 +441,7 @@ extern "C" DLOG_EXPORT void __cdecl LogMsg(dlog_level level, const char* message
{
if (Debug::GetInst()->isNeedLog((spdlog::level::level_enum)level)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}
Debug::GetInst()->LogMsg((spdlog::level::level_enum)level, message);
}
Expand All @@ -442,7 +452,7 @@ extern "C" DLOG_EXPORT void __cdecl wLogMsg(dlog_level level, const wchar_t* mes

if (Debug::GetInst()->isNeedLog((spdlog::level::level_enum)level)) {
if (!Debug::GetInst()->isInit && !Debug::GetInst()->isInitFail) { //如果还没有初始化过,那么就调用默认构造
dlog_init();
Debug::GetInst()->init("log", "dlog", INIT_RELATIVE::APPDATA);
}

rapidjson::StringBuffer buffUtf8;
Expand Down Expand Up @@ -483,6 +493,25 @@ extern "C" DLOG_EXPORT void __stdcall dlog_set_logger_function(DlogLoggerCallbac
Debug::GetInst()->exLoggerCallback = fp;
}

extern "C" DLOG_EXPORT void __stdcall dlog_set_encrypt_function(DlogLoggerEncryptCallback fpEncrypt,
DlogLoggerEncrypDeletetCallback fpDelete)
{
Debug::GetInst()->exLoggerEncryptCallback = fpEncrypt;
Debug::GetInst()->exLoggerEncrypDeletetCallback = fpDelete;
Debug::GetInst()->isEncryptFile = true;
Debug::GetInst()->isEncryptConsole = false;
}

extern "C" DLOG_EXPORT void __stdcall dlog_set_is_encrypt_file(bool isEncryptFile)
{
Debug::GetInst()->isEncryptFile = isEncryptFile;
}

extern "C" DLOG_EXPORT void __stdcall dlog_set_is_encrypt_console(bool isEncryptConsole)
{
Debug::GetInst()->isEncryptConsole = isEncryptConsole;
}

extern "C" DLOG_EXPORT void __stdcall dlog_set_is_chcp65001(bool ischcp65001)
{
Debug::GetInst()->ischcp65001 = ischcp65001;
Expand Down
Loading

0 comments on commit dc5fcb8

Please sign in to comment.