利用可变参数打印log
// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
//
#pragma once
#include <string>
#include <Windows.h>
#include <stdio.h>
#include "stdafx.h"
#include <stdarg.h>
#include <stdlib.h>
#include <cstring> using namespace std; const char* g_path = "C:\\test.log";
/*
string GetTime()
{
SYSTEMTIME st;
::GetLocalTime(&st);
char szTime[26] = { 0 };
sprintf_s(szTime, "%04d-%02d-%02d %02d:%02d:%02d %d ", \
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
return szTime;
}
*/
int myfprintf(const char* pFileName, const char* pFunName, const long lLine, const char* fmt, ...)
{
int ret = ;
//va_list是一个字符串指针,用于获取不确定个数的参数
va_list args;
//读取可变参数的过程其实就是在堆栈中,使用指针,遍历堆栈段中
//的参数列表,从低地址到高地址一个一个的把参数内容读出来的过程
va_start(args, fmt);
//该函数会根据参数fmt字符串来转换格式并格式化数据,然后将结果输出到参数Stream指定的文件中
//直到出现字符串结束的\0为止。
FILE* fp = NULL;
fopen_s(&fp, g_path,"a+");
//string strTime = GetTime();
//fprintf(fp,"%s ", strTime.c_str());
int nFileNameLen = strlen(pFileName);
char szLine[10] = { 0 };
sprintf(szLine, "%ld", lLine);
int nLineLen = strlen(szLine);
int nSpaceLen = 30 - nFileNameLen - nLineLen;
for (int i = 0; i<nSpaceLen; ++i)
fwrite(" ", 1, 1, fp);
fprintf(fp,"%s:%ld ", pFileName, lLine);
ret = vfprintf(fp, fmt, args);
printf("%d\n",ret);
//获取完所有参数之后,为了避免发生程序瘫痪,需要将 ap指针关闭,其实这个函数相当于将args设置为NULL
va_end(args);
fflush(fp);
fclose(fp);
return ret;
}
#define LOG(fmt, ...) myfprintf(__FILE__, __FUNCTION__, __LINE__, fmt,##__VA_ARGS__) int main(void)
{
char test1[] = "aaaaaaaaaa";
char* p = test1;
long test2 = ;
long long test3 = ;
LOG("test1 is:%s test2 is:%d test3 is:%lld\n ",p,test2,test3);
LOG("test1 is:%s test2 is:%d test3 is:%lld\n ", p, test2, test3);
getchar();
return ;
}
#include <stdio.h>
#include <stdarg.h> void WriteFrmtd(FILE *stream, char *format, ...)
{
va_list args; va_start(args, format);
vfprintf(stream, format, args);
va_end(args);
}
void WriteFrmtd1(const char* pFileName, const char* pFunName, const long lLine, char *format, ...)
{
FILE* stream = fopen("file1.txt","w"); fprintf(stream,"%s %s %ld ",pFileName,pFunName,lLine); va_list args; va_start(args, format);
vfprintf(stream, format, args);
va_end(args);
} #define LOG(fmt, ...) WriteFrmtd1(__FILE__, __FUNCTION__, __LINE__, fmt,##__VA_ARGS__) int main ()
{
// FILE *fp; // fp = fopen("file.txt","w"); char buff[]; //WriteFrmtd(fp, "This is just one argument %d %s %p\n", 10,"hello",buff);
LOG("This is just one argument %d %s %p\n", ,"hello",buff); // fclose(fp); return();
}
利用可变参数打印log的更多相关文章
- 利用可变参数打印log2
#pragma once #include <string> #include "StdAfx.h" #include <Windows.h> using ...
- C利用可变参数列表统计一组数的平均值,利用函数形式参数栈原理实现指针运算
//描述:利用可变参数列表统计一组数的平均值 #include <stdarg.h> #include <stdio.h> float average(int num, ... ...
- _vsnprintf在可变参数打印中的用法
_vsnprintf,C语言库函数之一,属于可变参数.用于向字符串中打印数据.数据格式用户自定义. 函数简介 编辑 头文件: #include <stdarg.h> 函数声明: int _ ...
- 利用可变参数模拟Printf()函数实现一个my_print()函数和调用可变参数注意的陷阱!
可变参数函数的实现与函数调用的栈结构密切相关,正常情况下C的函数参数入栈规则为__stdcall, 它是从右到左的,即函数中的最右边的参数最先入栈. 例如,对于函数: void test(char a ...
- Linux可变参数打印日志(二)
#include<stdio.h> #include<stdlib.h> #include<stdarg.h> #include<string.h> # ...
- Linux 打印可变参数日志
实现了传输进去的字符串所在的文档,函数和行数显示功能. 实现了将传入的可变参数打印到日志功能. #include<stdio.h> #include<stdarg.h> #in ...
- Redis源码笔记--服务器日志和函数可变参数处理server.c
前言 Redis源码中定义了几个和日志相关的函数,用于将不同级别的信息打印到不同的位置(日志文件或标准输出,取决于配置文件的设置),这些函数的定义位于 server.h 和server.c 文件中,包 ...
- 【C语言】模拟实现printf函数(可变参数)
一.printf函数介绍 printf功能 printf函数是格式化输出函数,一般用于向标准输出设备按规定格式输出信息. printf原型 int printf( const char* format ...
- Java基础——可变参数
大家都知道main方法的参数就是一个数组类型的,那么它其实也是可以改成不定参数类型.我试了试,并调用了一些弹出来的方法. public class ClassC2 { public static vo ...
随机推荐
- python 全栈开发,Day37(操作系统的发展史)
昨日内容回顾: # C/S和B/S架构 # osi五层模型 # 应用层 # 自定义协议(struct) _ 解决黏包 # 验证客户端合法性 _ hmac os.urandom # 解决TCP协议的se ...
- Android学习之六种事件响应方法汇总
java源码如下: 1.MainActivity.java源码 package com.example.responsetest; import android.app.Activity; impor ...
- 可以设置超时版的的fetch
// 超时版的fetch _fetch(fetch, timeout) { return Promise.race([ fetch, new Promise(function (resolve, re ...
- Linux java 命令行编译 jar包
Java 命令行编译成class,然后在打包成jar文件. 编译成class javac -classpath $CLASS_PATH -d class ./src/Hello.java 可以通过ja ...
- Bitcoin 使用及配置记录
常用配置 bitcoin-qt.exe -testnet -printtoconsole -conf=D:\Bitcoin\bitcoin.conf -datadir=D:\Bitcoin\Data ...
- visual studio Web发布至 IIS WebDeploy出错(未能创建SSL/TLS安全通道)Could not create SSL/TLS secure channel
问题发生的原因是VS 15.9尝试使用系统默认值进行TLS握手,但是要在VS内的某处设置为TLS1.2. 此问题的解决方法是在部署项目的IIS服务器上启用TLS 1.2.例如,请按照此文章中的说明操作
- 移动端自动化测试-WTF Appium?
手机App分为两大类,原生App(Native App)和混合APP(Hybrid App) 原生App(Native App) 原生App实际就是我们所常见的传统App开发模式,云端数据存储+App ...
- Express中间件,看这篇文章就够了(#^.^#)
底层:http模块 express目前是最流行的基于Node.js的web开发框架,express框架建立在内置的http模块上, var http = require('http') var app ...
- 从源码的角度再看 React JS 中的 setState
在这一篇文章中,我们从源码的角度再次理解下 setState 的更新机制,供深入研究学习之用. 在上一篇手记「深入理解 React JS 中的 setState」中,我们简单地理解了 React 中 ...
- shell脚本之特殊符号总结性梳理
# 井号 (comments) 这几乎是个满场都有的符号#!/bin/bash 井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注解文字,不会被执行. # This line ...