利用可变参数打印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 ...
随机推荐
- 2018AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions
论文标题:AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions 来源/作者机构情况: 谷歌,http:// ...
- SkylineGlobe 如何实现工程进度管理或者说是对象生命周期管理
SkylineGlobe 的 TerraExplorer Pro里面,给我们提供了一个Timespan Tags工具,通过这个工具,我们可以设置ProjectTree任务组对象的生命周期: 然后通过调 ...
- springbootAdmin+eureka集群+swagger
请移步githubb下载源码.知识共享.(https://github.com/yivvonllh) 或者直接git下载(https://github.com/yivvonllh/spring-clo ...
- c语言程序设计 第一例子
#include <studio.h> int main(){ printf("this is dog.\n"); return 0; } studio.h 表示st ...
- WPF解决界面全屏化但不遮挡任务栏的问题
原文:WPF解决界面全屏化但不遮挡任务栏的问题 学习C#有一段时间了,现在跟着做项目,碰到有个客户端界面总是全屏,对于客户来说没有任务栏很不习惯,所以做了些略微的修改 </pre>&l ...
- POJ1845
这还是一道综合了许多数论的知识点的,做完也涨了不少姿势 但还是因为约数和公式这个鬼东西去找了度娘 题意很简单,就是求\(A^B\)的约数之和\(mod\ 9901\). 但是这种题意越是简单的题目越是 ...
- [Python]Practice makes perfect
Practice makes perfect 发音被儿子鄙视了.需要加强练习 以此自勉.
- cgroup.conf系统初始配置
# Slurm cgroup support configuration file # # See man slurm.conf and man cgroup.conf for further # i ...
- SpringBoot笔记
官网: http://springboot.fun/ 收集到一个比较全的: https://blog.csdn.net/xiaoyu411502/article/details/52474037 Id ...
- MySQL高可用架构-MMM环境部署记录
MMM介绍MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序.MMM使用Perl语言开发,主要用来监控和管理 ...