Here is a C++ logging library implemented with Win32 APIs that ensures thread safety. It consists of just two files, making it simple to integrate and use.
Header File
The header file includes several functions for logging operations. While there are many interfaces defined, only a few are common used:
WriteProgramLogNoMask: Outputs log entries.InitProgramLogExByBuf: Initializes the logging system.CloseProgramLog: Finalizes logging and flushes buffers to disk.
#if !defined(__LOG_H__)
#define __LOG_H__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
/* Usage example
InitProgramLogExByBuf();
WriteProgramLogNoMask("%s %s", "aa", "bb"); // outputs "aa bb"
CloseProgramLog();
*/
namespace RLBase {
#ifdef __cplusplus
extern "C"
{
#endif
// Parameter definitions similar to BaseInitProgramLog
XHBASE_API void DeleteProgramLog(LPCTSTR pProjectName = NULL, LPCTSTR pLogFilePath = NULL);
// Definitions for BaseInitProgramLog parameters
// dwMask definition
#define BASE_LOG_MASK_ALL 0xFFFFFFFF
// dwFlag definitions
#define BASE_LOG_DELETE_MASK 0x0000000F // Controls log file size
#define BASE_LOG_DELETE_NO 0x00000000 // No automatic deletion of log files, dwMaxLogFileSize/dwMaxReserveSize are ignored
#define BASE_LOG_DELETE_INIT 0x00000001 // Delete log files based on size during initialization
#define BASE_LOG_DELETE_CLOSE 0x00000002 // Delete log files based on size upon closing
#define BASE_LOG_DELETE_WRITE 0x00000004 // Delete log files based on size during writing
#define BASE_LOG_RELEASE 0x80000000 // Output logs even in Release builds
#define BASE_LOG_SAFE 0x40000000 // Encrypt log files
#define BASE_LOG_PROJECTNAME 0x20000000 // Include project name in logs
#define BASE_LOG_THREADID 0x10000000 // Display thread ID
#define BASE_LOG_TIME 0x08000000 // Include timestamp in logs
#ifdef __BASE_LOG_RELEASE__
#define BASE_FLAG_INIT (BASE_LOG_DELETE_WRITE | BASE_LOG_RELEASE | BASE_LOG_PROJECTNAME | BASE_LOG_THREADID | BASE_LOG_TIME | BASE_LOG_SAFE)
#else
#define BASE_FLAG_INIT (BASE_LOG_DELETE_WRITE | BASE_LOG_RELEASE | BASE_LOG_PROJECTNAME | BASE_LOG_THREADID | BASE_LOG_TIME)
#endif
#define __BASE_LOG_ENABLE__
#ifdef __BASE_LOG_ENABLE__
// dwMask controls which logs are actually written
// dwFlag sets various options
// dwLogBufSize specifies buffer size (0 = no buffer, -1 = default 100K, other = custom size)
// dwMaxLogFileSize maximum file size in KB (0 = no auto deletion)
// dwMaxReserveSize maximum reserved space in KB (0 = delete all)
// pProjectName project identifier (defaults to executable name if null)
// pLogFilePath path for log files (absolute or relative)
XHBASE_API void InitLog(const wchar_t* company, const wchar_t* product, const wchar_t* programName = NULL);
XHBASE_API void InitProgramLog(DWORD dwMask = BASE_LOG_MASK_ALL, DWORD dwFlag = BASE_FLAG_INIT, DWORD dwLogBufSize = -1, DWORD dwMaxLogFileSize = 0, DWORD dwMaxReserveSize = 0, LPCTSTR pProjectName = NULL, LPCTSTR pLogFilePath = NULL);
XHBASE_API void InitProgramLogEx(DWORD dwMask = BASE_LOG_MASK_ALL, LPCTSTR pProjectName = NULL, LPCTSTR pLogFilePath = NULL);
XHBASE_API void InitProgramLogExByBuf(DWORD dwMask = BASE_LOG_MASK_ALL, LPCTSTR pProjectName = NULL, LPCTSTR pLogFilePath = NULL);
XHBASE_API void CloseProgramLog();
XHBASE_API void FlushProgramLog(); // Flushes buffered logs to file
XHBASE_API void WriteProgramLogString(DWORD dwMask, LPCTSTR lpszLogText);
XHBASE_API void WriteProgramLogBin(DWORD dwMask, LPCTSTR lpszFront, LPCTSTR lpszBack, LPCTSTR lpszBuf, DWORD uBufLength);
XHBASE_API void WriteProgramLog(DWORD dwMask, LPCTSTR lpszFormat, ...);
XHBASE_API void WriteProgramLogNoMask(LPCTSTR lpszFormat, ...);
#define WriteProgramLogStringNoMask(lpszLogText) WriteProgramLogString(BASE_LOG_MASK_ALL,lpszLogText)
#define WriteProgramLogBinNoMask(lpszFront,lpszBack,lpszBuf,nBufLength) WriteProgramLogBin(BASE_LOG_MASK_ALL,lpszFront,lpszBack,lpszBuf,nBufLength)
#else // __BASE_LOG_ENABLE__
#define InitProgramLog __noop
#define InitProgramLogEx __noop
#define InitProgramLogExNoBuf __noop
#define CloseProgramLog() __noop
#define FlushProgramLog() __noop
#define WriteProgramLogString(dwMask,lpszLogText) __noop
#define WriteProgramLogBin(dwMask,lpszFront,lpszBack,lpszBuf,nBufLength) __noop
#define WriteProgramLog __noop
#define WriteProgramLogNoMask __noop
#define WriteProgramLogStringNoMask(lpszLogText) __noop
#define WriteProgramLogBinNoMask(lpszFront,lpszBack,lpszBuf,nBufLength) __noop
#endif // __BASE_LOG_ENABLE__
#ifdef __cplusplus
}
#endif
//}}
}
#endif // !defined(__BASE_PROGRAMLOG_H__)
Usage Example
InitProgramLogExByBuf();
WriteProgramLogNoMask("%s %s", "aa", "bb"); // outputs "aa bb"
CloseProgramLog();