文章目录
- 1、个人测试
- 2、log4cplus
- 3、log4j2(Apache)
- 4、log4cxx(Apache)
- 5、log4cpp
- 6、log4c
- 7、Log4Qt
- 8、boost.log
- 9、glog (Google Logging Library )
- 后续
日志的作用是记录系统的运行过程及异常信息,为快速定位系统运行中出现的问题及开发过程中的程序调试问题提供详细信息。
1、个人测试
这里废话不多说,直接上代码。
- FxDebugLog.h:
/*
* This source file is part of 爱看书的小沐
* @author: tomcat
* @date: 2022-01-20
*/
#pragma once
enum LOG_FLAGS {
LOG_INFO = 1 << 0,
LOG_WARN = 1 << 1,
LOG_ERROR = 1 << 2,
LOG_DATE = 1 << 3,
LOG_PRINTF = 1 << 4,
LOG_MESSAGEBOX = 1 << 5,
LOG_OUTPUTDEBUG = 1 << 6,
LOG_FILE = 1 << 7,
LOG_SENDMESSAGE = 1 << 8,
LOG_PIPE = 1 << 9,
LOG_EDIT = 1 << 10,
LOG_NOLF = 1 << 11,
LOG_DATE2 = 1 << 12,
};
/*------------------------------------------------------*/
namespace FxLib { namespace Debug {
void LogShort(const TCHAR* logFileName, const TCHAR* log2write, bool bWrap = true);
void LogLong(unsigned nFlags, void* pData, const char* fmt, ...);
void ConsoleStart();
void ConsoleEnd();
void OutputDebugStringF(const _TCHAR *szFmt, ...);
void MessageBoxF(const _TCHAR *szFmt, const _TCHAR *szCaption, unsigned int uType, ...);
}} // namespace FxLib::Debug
/*------------------------------------------------------*/
#define FXLOG_FILE(file, _format, ...) FxLib::Debug::LogLong(LOG_FILE, (void*)file, _format,##__VA_ARGS__)
#define FXLOG_FILEDATE(file, _format, ...) FxLib::Debug::LogLong(LOG_FILE|LOG_DATE, (void*)file, _format,##__VA_ARGS__)
#define FXLOG_OUTPUT(_format, ...) FxLib::Debug::LogLong(LOG_OUTPUTDEBUG, NULL, _format,##__VA_ARGS__)
#define FXLOG_EDIT(hWndEdit, _format, ...) FxLib::Debug::LogLong(LOG_EDIT, (void*)hWndEdit, _format,##__VA_ARGS__)
#define FXLOG_EDITDATE(hWndEdit, _format, ...) FxLib::Debug::LogLong(LOG_EDIT|LOG_DATE, (void*)hWndEdit, _format,##__VA_ARGS__)
#define FXLOG_EDITDATE2(hWndEdit, _format, ...) FxLib::Debug::LogLong(LOG_EDIT|LOG_DATE2, (void*)hWndEdit, _format,##__VA_ARGS__)
- FxDebugLog.cpp:
/*
* This source file is part of 爱看书的小沐
* @author: tomcat
* @date: 2022-01-20
*/
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include "FxDebugLog.h"
namespace FxLib { namespace Debug {
void LogShort(const TCHAR* logFileName, const TCHAR* log2write, bool bWrap)
{
#ifdef _UNICODE
FILE *fp = _tfopen(logFileName, _T("a+,ccs=UTF-8"));
#else
FILE *fp = _tfopen(logFileName, _T("a+"));
#endif
if(fp == NULL) return;
fwrite(log2write, sizeof(log2write[0]), _tcslen(log2write), fp);
if(bWrap) _fputtc('\n', fp);
fflush(fp);
fclose(fp);
}
void LogLong(unsigned nFlags, void* pData, const char* fmt, ...)
{
//get time string
char szTime[64];
memset(szTime, 0, sizeof(szTime));
if(nFlags & LOG_DATE) {
SYSTEMTIME systime;
::GetLocalTime(&systime);
sprintf_s(szTime, 64, "[%04d-%02d-%02d %02d:%02d:%02d]",
systime.wYear, systime.wMonth, systime.wDay,
systime.wHour, systime.wMinute, systime.wSecond);
}
else if(nFlags & LOG_DATE2) {
SYSTEMTIME systime;
::GetLocalTime(&systime);
sprintf_s(szTime, 64, "[%02d:%02d:%02d]",
systime.wHour, systime.wMinute, systime.wSecond);
}
//create input string
const int buffer_size = 4096;
va_list args;
static char buffer[ buffer_size ];
memset(buffer, 0, sizeof(buffer));
va_start( args, fmt );
int len = vsprintf_s( buffer, buffer_size - 2, fmt, args );
va_end( args );
if ( len < 0 || len > buffer_size - 2 ) {
len = buffer_size - 2;
}
if(nFlags & LOG_NOLF) {
buffer[len] = '\0';
}
else {
buffer[len] = '\n';
buffer[len+1] = '\0';
}
//print log
static char szEdit[1024*4];
memset(szEdit, 0, sizeof(szEdit));
if(nFlags & LOG_DATE || nFlags & LOG_DATE2) {
strcat_s(szEdit, szTime);
strcat_s(szEdit, ": ");
}
strcat_s(szEdit, buffer);
if (nFlags & LOG_FILE) {
LPCSTR logFileName = (LPCSTR)pData;
LogShort(logFileName, szEdit, false);
}
else if (nFlags & LOG_EDIT) {
HWND hWndEdit = (HWND)pData;
int nLines = (int)::SendMessage(hWndEdit, EM_GETLINECOUNT, 0, 0);
#define BUFFER_SIZE 1024*1024*4
static char buffer[BUFFER_SIZE] = "\0";
int bufLen = ::GetWindowText(hWndEdit, buffer, BUFFER_SIZE);
if(bufLen < BUFFER_SIZE && bufLen >= 2 && !(buffer[bufLen - 1] == '\n' && buffer[bufLen - 2] == '\r'))
strcat_s(buffer, "\r\n");
strcat_s(buffer, szEdit);
::SendMessage(hWndEdit, EM_SETSEL, bufLen, bufLen);
::SendMessage(hWndEdit, EM_SCROLLCARET, 0, 0L);
::SetFocus(hWndEdit);
::SendMessage(hWndEdit, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)buffer);
::SendMessage(hWndEdit, EM_LINESCROLL, 0, nLines);
if(nLines > 1000) {
::SetWindowTextA(hWndEdit, "");
::SendMessage(hWndEdit, EM_LINESCROLL, 0, 0);
}
}
else if (nFlags & LOG_PRINTF) {
printf(szEdit);
}
else if (nFlags & LOG_MESSAGEBOX) {
::MessageBoxA(NULL, szEdit, "Log", MB_OK);
}
else if (nFlags & LOG_OUTPUTDEBUG) {
::OutputDebugStringA(szEdit);
}
}
void ConsoleStart()
{
AllocConsole();
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
}
void ConsoleEnd()
{
fclose(stdin);
fclose(stdout);
fclose(stderr);
FreeConsole();
}
void OutputDebugStringF(const _TCHAR *szFmt, ...)
{
va_list vaArgs;
va_start(vaArgs, szFmt);
// wvsprintf guarantees not to write more than 1024 chars
static _TCHAR szBuf[1024];
wvsprintf(szBuf, szFmt, vaArgs);
va_end(vaArgs);
OutputDebugString(szBuf);
}
void MessageBoxF(const _TCHAR *szFmt, const _TCHAR *szCaption, unsigned int uType, ...)
{
va_list vaArgs;
va_start(vaArgs, uType);
// wvsprintf guarantees not to write more than 1024 chars
static _TCHAR szBuf[1024];
wvsprintf(szBuf, szFmt, vaArgs);
va_end(vaArgs);
MessageBox(NULL, szBuf, szCaption, uType);
}
}} // namespace FxLib::Debug
2、log4cplus
https://sourceforge.net/projects/log4cplus/https://github.com/log4cplus/log4cplus
- log4cplus is a simple to use C++ logging API providing thread-safe, flexible, and arbitrarily granular control over log management and configuration. It is modelled after the Java log4j API.
- log4cplus是一个易于使用的C ++ 日志记录API,log4cplus具有灵活、强大、使用简单、多线程安全的特点。通过将信息划分优先级使其可以面向程序调试、运行、测试、和维护等全生命周期;你可以选择将信息输出到屏幕、文件、甚至是远程服务器;通过指定策略对日志进行定期备份等等。
- Last Update: 2021-08-09
- Last Version: 2.0.7 ( 2021-08-09 )
3、log4j2(Apache)
https://logging.apache.org/log4j/2.x/index.htmlhttps://logging.apache.org/log4j/2.x/download.html
- Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture.
- Last Version: 2.17.1
4、log4cxx(Apache)
https://logging.apache.org/log4cxx/latest_stable/https://github.com/apache/logging-log4cxx
- Apache Log4cxx is a C++ port of Apache Log4j
- Last Update: 10 days ago
- Last Version: 0.12.1 ( 10 days ago )
5、log4cpp
http://log4cpp.sourceforge.net/
- Log4cpp is library of C++ classes for flexible logging to files, syslog, IDSA and other destinations. It is modeled after the Log4j Java library, staying as close to their API as is reasonable.
- Last Published: 2017-04-18
- Last Version: 1.1.3 ( 2017-07-13 )
6、log4c
http://log4c.sourceforge.net/
- Log4c is a library of C for flexible logging to files, syslog and other destinations. It is modeled after the Log for Java library (http://jakarta.apache.org/log4j/), staying as close to their API as is reasonable. Here is a short introduction to Log4j which describes the API, and design rationale.
- Last Update: 2016-01-30
- Last Version: 1.2.4 ( 2013-10-03 )
7、Log4Qt
http://log4qt.sourceforge.net/
- Log4Qt is a C++ port of the Apache Software Foundation Log4j package using the Trolltech Qt Framework. It is intended to be used by open source and commercial Qt projects.
- Last Update: 2009-03-01
- Last Version: 0.3 ( 2009-03-01 )
8、boost.log
https://www.boost.org/https://www.boost.org/users/download/https://boostorg.jfrog.io/artifactory/main/release/https://sourceforge.net/projects/boost/files/boost/https://github.com/boostorg/boost
- Last Update: 8 hours ago
- Last Version: 1.78.0 ( 2 Dec 2021 )
9、glog (Google Logging Library )
https://github.com/google/glog
- Google Logging (glog) is a C++98 library that implements application-level logging. The library provides logging APIs based on C+±style streams and various helper macros.
- Last Update: 3 days ago
- last Verison: 0.5.0 (8 May 2021)
后续
如果你觉得这些文字有一点点用处,可以给作者点个赞;╮( ̄▽ ̄)╭
如果你感觉作者写的不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进。o_O???
谢谢各位小伙伴们啦( ´ ▽ ‘)ノ ( ´ ▽ ` )っ!!!