码迷,mamicode.com
首页 > 编程语言 > 详细

c++相关函数实现

时间:2019-01-02 12:30:52      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:mdi   c++   实现   format   max   函数   mda   alt   sub   

日志打印

extern std::string GetProgramDir();
extern std::string GetProgramName();
extern int write_log(const char *format, ...);
int write_log(const char *format, ...)
{
    std::string log_name = GetProgramDir() + ‘\\‘ + GetProgramName() + ".log";
    FILE* pFile = fopen(log_name.c_str(), "a");
    va_list arg;
    int done;

    va_start(arg, format);

    time_t time_log = time(NULL);//#include <time.h>
    struct tm* tm_log = localtime(&time_log);
    fprintf(pFile, "%04d-%02d-%02d %02d:%02d:%02d ", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);

    done = vfprintf(pFile, format, arg);
    va_end(arg);
    fflush(pFile);
    fclose(pFile);
    return done;
}
std::string GetProgramDir()
{
    std::string program_file_name;
    char the_filename[MAX_PATH];  //#include <Windows.h>
    DWORD length = ::GetModuleFileName(nullptr, the_filename, MAX_PATH);
    if (length > 0 && length < MAX_PATH)
    {
        program_file_name = the_filename;
    }
    int pos = program_file_name.find_last_of(‘\\‘, program_file_name.length());
    return program_file_name.substr(0, pos);  // Return the directory without the file name
}
std::string GetProgramName()
{
    std::string program_file_name;
    char the_filename[MAX_PATH];
    DWORD length = ::GetModuleFileName(nullptr, the_filename, MAX_PATH);
    if (length > 0 && length < MAX_PATH)
    {
        program_file_name = the_filename;
    }
    int pos1 = program_file_name.find_last_of(‘\\‘, program_file_name.length());
    int pos2 = program_file_name.find_last_of(‘.‘, program_file_name.length());
    int len = pos2 - pos1 - 1;
    return program_file_name.substr(pos1 + 1, len);  // Return the directory without the file name
}

c++相关函数实现

标签:mdi   c++   实现   format   max   函数   mda   alt   sub   

原文地址:http://blog.51cto.com/haidragon/2337674

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!