C++ WIN32 日志程序

头文件

***

 1 #pragma once
 2 #pragma once
 3 #include "stdio.h"
 4 #include "windows.h"
 5 #include "tchar.h"
 6 #include "shlwapi.h"
 7 #pragma comment(lib,"shlwapi.lib")
 8 class Log
 9 {
10 public:
11     static bool Write(TCHAR* logStr, TCHAR* logType = _T("log"), TCHAR* logDir = _T("log"));
12     //参数如何区分目录和文件:
13     //如果最后有\\肯定是目录
14     //如果最后又后缀".",则肯定是文件
15     //如果最后没有\\,也没有后缀‘.",则认为是路径
16     static bool CreateFolder(TCHAR* pszPath);
17 private:
18     Log();
19     ~Log();
20     static Log* InitInstance();
21     //当前路径
22     static TCHAR s_curPath[MAX_PATH];
23     //日志目录
24     static TCHAR s_logDir[MAX_PATH];
25     //日志的全目录路径
26     static TCHAR s_logPath[MAX_PATH];
27     static Log* s_instance;
28 };

源文件

**

#include "Log.h"
Log* Log::s_instance = NULL;
TCHAR Log::s_curPath[MAX_PATH];
TCHAR Log::s_logPath[MAX_PATH];
TCHAR Log::s_logDir[MAX_PATH];
Log* Log::InitInstance()
{
    if (s_instance == NULL)
    {
        s_instance = new Log();
    }
    return s_instance;
}
//日志写入硬盘的程序,合计三个参数
//参数1:
//参数2:
//参数3:
bool Log::Write(TCHAR* logStr, TCHAR* logType, TCHAR* logDir)
{
    _tcscpy_s(s_logDir, logDir);
    InitInstance();
    SYSTEMTIME time;
    GetLocalTime(&time);
    TCHAR date[128] = { 0 }, filename[128] = { 0 }, hh[50] = _T("\n");
    swprintf_s(date, _T("%d-%02d-%02d %02d:%02d:%02d"), time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
    swprintf_s(filename, _T("%s\\%s_%d-%02d-%02d.log"), s_logPath, logType, time.wYear, time.wMonth, time.wDay);
    FILE* fp;
    _tfopen_s(&fp, filename, _T("a,ccs=UTF-8"));
    TCHAR newstr[1024] = { 0 };
    swprintf_s(newstr, _T("%s : %s\n"), date, logStr);
    if (fp) {
        fwrite(newstr, sizeof(TCHAR), _tcslen(newstr), fp);
        fclose(fp);
        return true;
    }
    else {
        return false;
    }
}
bool Log::CreateFolder(TCHAR* pszPath)
{
    //如果后面有\\则是路径,如果没有,但有后缀,则是文件,无后缀则也是路径
    TCHAR szPath[_MAX_PATH] = { 0 };
    swprintf_s(szPath, pszPath, _MAX_PATH);
    szPath[_MAX_PATH - 1] = 0;
    TCHAR* pdot = _tcsrchr(szPath, _T(.));
    TCHAR* psp = _tcsrchr(szPath, _T(\\));
    if (psp && pdot && pdot > psp)
    {
        //在路径最后找到上面两个字符证明是文件,去掉文件名
        psp[0] = 0;
    }
    else
    {
        //缺省是个目录,这个地方可能吧没有后缀的文件当作目录,这里不做处理,默认当成目录
    }
    PathAddBackslash(szPath);
    if (PathIsDirectory(szPath))
        return true;
    psp = _tcschr(szPath, _T(\\));
    while (psp)
    {
        *psp = 0;
        if (!PathIsDirectory(szPath))
        {
            if (!CreateDirectory(szPath, 0))
                return false;
        }
        *psp = _T(\\);
        psp = _tcschr(psp + 1, _T(\\));
    }
    return true;
};
Log::Log()//构造函数
{
    //取当前执行模块的全路径,如果此模块是被其它程序调用的,返回的路径还是这个程序的路径
    ::GetModuleFileName(NULL, s_curPath, MAX_PATH);
    //从路径中移除文件名
    PathRemoveFileSpec(s_curPath);
    swprintf_s(s_logPath, _T("%s\\%s"), s_curPath, s_logDir);
    if (!CreateFolder(s_logPath))
    {
        MessageBox(NULL, _T("日志目录创建失败"), NULL, 0);
    }
}
Log::~Log()//析构函数
{
}

 

C++ WIN32 日志程序

上一篇:浅谈安卓去除网络弹窗(找到方法,后续陆续添加)


下一篇:java获取本地json格式的内容