Skip to content

Instantly share code, notes, and snippets.

@Mainvooid
Created April 17, 2019 09:03
Show Gist options
  • Select an option

  • Save Mainvooid/73e3d8bd135a312f6e543942f1e0ba43 to your computer and use it in GitHub Desktop.

Select an option

Save Mainvooid/73e3d8bd135a312f6e543942f1e0ba43 to your computer and use it in GitHub Desktop.
调式日志封装 #C++ #windows
#include "log.h"
#include <windows.h>
#include <tchar.h>
#include <iostream>
void OutputDebugStringEx(const wchar_t * format, ...)
{
wchar_t buf[512];
va_list args;
va_start(args, format);
int len = _vstprintf_s(buf, format, args);
va_end(args);
OutputDebugString(buf);
}
void PrintLog(const wchar_t * wmsgbuf, int nLevel, const char* _func, const char * _file, int _line)
{
DWORD func_BufSize = MultiByteToWideChar(CP_UTF8, 0, _func, -1, NULL, 0);
DWORD file_BufSize = MultiByteToWideChar(CP_UTF8, 0, _file, -1, NULL, 0);
wchar_t* wfunc = new wchar_t[func_BufSize];
wchar_t* wfile = new wchar_t[file_BufSize];
wmemset(wfunc, 0, func_BufSize);
wmemset(wfile, 0, file_BufSize);
MultiByteToWideChar(CP_UTF8, 0, _func, -1, LPWSTR(wfunc), func_BufSize);
MultiByteToWideChar(CP_UTF8, 0, _file, -1, LPWSTR(wfile), file_BufSize);
switch (nLevel) {
case LOG_LEVEL::Info:
OutputDebugStringEx(L"Info : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
case LOG_LEVEL::Warm:
OutputDebugStringEx(L"Warm : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
case LOG_LEVEL::Error:
OutputDebugStringEx(L"Error : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
case LOG_LEVEL::Fatal:
OutputDebugStringEx(L"Fatal : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
}
delete wfunc;
delete wfile;
}
void PrintLog(std::string msgbuf, int nLevel, const char* _func, const char * _file, int _line)
{
DWORD msgbuf_BufSize = MultiByteToWideChar(CP_UTF8, 0, msgbuf.c_str(), -1, NULL, 0);
DWORD func_BufSize = MultiByteToWideChar(CP_UTF8, 0, _func, -1, NULL, 0);
DWORD file_BufSize = MultiByteToWideChar(CP_UTF8, 0, _file, -1, NULL, 0);
wchar_t* wmsgbuf = new wchar_t[msgbuf_BufSize];
wchar_t* wfunc = new wchar_t[func_BufSize];
wchar_t* wfile = new wchar_t[file_BufSize];
wmemset(wmsgbuf, 0, msgbuf_BufSize);
wmemset(wfunc, 0, func_BufSize);
wmemset(wfile, 0, file_BufSize);
MultiByteToWideChar(CP_UTF8, 0, msgbuf.c_str(), -1, LPWSTR(wmsgbuf), msgbuf_BufSize);
MultiByteToWideChar(CP_UTF8, 0, _func, -1, LPWSTR(wfunc), func_BufSize);
MultiByteToWideChar(CP_UTF8, 0, _file, -1, LPWSTR(wfile), file_BufSize);
switch (nLevel) {
case LOG_LEVEL::Info:
OutputDebugStringEx(L"Info : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
case LOG_LEVEL::Warm:
OutputDebugStringEx(L"Warm : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
case LOG_LEVEL::Error:
OutputDebugStringEx(L"Error : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
case LOG_LEVEL::Fatal:
OutputDebugStringEx(L"Fatal : %s...[%s(%d) %s]\n", wmsgbuf, wfile, _line, wfunc);
break;
}
delete wmsgbuf;
delete wfunc;
delete wfile;
}
#pragma once
#include <iostream>
enum LOG_LEVEL {
Info,
Warm,
Error,
Fatal
};
//OutputDebugStringW扩展版
void OutputDebugStringEx(const wchar_t * format, ...);
/*
*@brief 打印Debug信息(中文可能有问题)
*@param msgbuf 提示信息
*@param nLevel log等级 LOG_LEVEL : Info,Warm,Error,Fatal
*@param _func 宏 __DUNCTION__ 函数名
*@param _file 宏 __FILE__ 文件名
*@param _line 宏 __LINE__ 行数
*/
void PrintLog(const wchar_t * wmsgbuf, int nLevel, const char* _func, const char * _file, int _line);
void PrintLog(std::string msgbuf, int nLevel, const char* _func, const char * _file, int _line);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment