zoukankan      html  css  js  c++  java
  • C和C++ log

    一、

    1. C++项目中用到c代码,各个代码打印日志用的不一样,Qt C++用qDebug,纯C++用cout,C语言用printf,但打印日志结果不同步,影响判断,误导找问题。

       所以最好采用记录日志的形式,记录的日志同步。

    2.在mfc使用会出现以下错误:

    错误 C4996 '_itoa': This function or variable may be unsafe. Consider using _itoa_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

    解决方法:

    项目右键 -- 属性 -- C/C++ -- 预处理器 -- 预处理器定义,在里面添加  _CRT_SECURE_NO_DEPRECATE 即可;

    二、C

    log2.h

    #ifndef LOG_H
    #define LOG_H
    
    #include <stdio.h>
    #include <stdint.h>
    #include <stdbool.h>
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    
        //class log2
        //{
        //public:
        //log2();
    
        void logd(const char *type_name, const uint8_t *data, int len);
        void logdx(const char *type_name, const uint8_t *data, int len);
        
        void logRemoveFile();
        //private:
        
        //};
    
    #ifdef __cplusplus
    }
    #endif
    #endif // LOG2_H

    log2.c

    #include "stdafx.h"//mfc 需要添加此头文件
    #include "log.h"
    #include "time.h"
    #include "windows.h"
    const char* FILE_PATH = "logc.txt";
    void logd(const char *type_name, const uint8_t *data, int len)
    {
        FILE* pFile = fopen("logc.txt", "a");
    
        SYSTEMTIME st = { 0 };
        GetLocalTime(&st);
        fprintf(pFile, "[%04d-%02d-%02d %02d:%02d:%02d %03d] ", st.wYear, st.wMonth, st.wDay,
            st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
    
        fprintf(pFile, "%s
    ", type_name);
    
        for (int i = 0; i < len; i++)
        {
    
            fprintf(pFile, " %d", data[i]);
        }
    
        fprintf(pFile, "
    ");
    
        fflush(pFile);
        fclose(pFile);
    
    }
    void logdx(const char *type_name, const uint8_t *data,int len)
    {
    
        FILE* pFile = fopen(FILE_PATH, "a");
        
        SYSTEMTIME st = { 0 };
        GetLocalTime(&st);
        fprintf(pFile, "[%04d-%02d-%02d %02d:%02d:%02d %03d] ", st.wYear, st.wMonth, st.wDay,
            st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
    
        fprintf(pFile, "%s
    ", type_name);
    
        for (int i = 0; i < len; i++)
        {
    
            fprintf(pFile, " %02X", data[i]);
        }
        
        fprintf(pFile, "
    ");
    
        fflush(pFile);
        fclose(pFile);
    }
    void logRemoveFile()
    {
        remove("logc.txt");
    }
  • 相关阅读:
    Delphi cxGrid –--> RecordIndex out of Range
    局域网共享需要密码
    提高AdoQuery的速度
    string literals may have at most 255 elements
    delphi控件属性大全-详解-简介
    Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值
    FastReport 使用说明
    delphi的取整函数round、trunc、ceil和floor
    cxGrid 速度
    SQL Server 无法在服务器上访问指定的路径或文件解决方法
  • 原文地址:https://www.cnblogs.com/ike_li/p/12378323.html
Copyright © 2011-2022 走看看