利用可变参数打印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 ...
随机推荐
- 【Codeforces 1120C】Compress String
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...
- Android学习之基础知识四-Activity活动5讲(Activity的生命周期)
一.返回栈 1.Android是通过任务(Task)来管理活动,一个任务就是一个返回栈内所有活动的集合. 2.返回栈是一个后进先出的数据结构,每启动一个新的活动,该活动就会覆盖原来的活动,位于栈顶位置 ...
- mysql导出CSV格式的文件
select * from Account into outfile "/tmp/haha.csv" fields terminated by ',' lines term ...
- Apache与Nginx
Apache与Nginx的优缺点比较 --- 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的 ...
- Spring Boot 之订制 logo
Spring Boot 之订制 logo 简介 变量 配置 编程 源码 引申和引用 Spring Boot 启动时默认会显示以下 logo: . ____ _ __ _ _ /\\ / ___'_ _ ...
- 获取2个集合List<T>的共同元素
获取2个集合List<T>的共同元素,循环2个集合,然后比对. class Bj { public void GetIntersect() { , , , , , , }; , , , , ...
- flask seesion组件
一.简介 flask中session组件可分为内置的session组件还有第三方flask-session组件,内置的session组件功能单一,而第三方的flask-sessoin可支持re ...
- Android 真机调试
/************************摘抄*****************************/ 刚好遇到这个问题,在网上百度了一下,看到有人分享了引起该问题的几个原因: 1.手机设 ...
- Spring Boot(十八):使用 Spring Boot 集成 FastDFS
上篇文章介绍了如何使用 Spring Boot 上传文件,这篇文章我们介绍如何使用 Spring Boot 将文件上传到分布式文件系统 FastDFS 中. 这个项目会在上一个项目的基础上进行构建. ...
- 编写自己的dapper lambda扩展-使用篇
前言 这是针对dapper的一个扩展,支持lambda表达式的写法,链式风格让开发者使用起来更加优雅.直观.现在暂时只有MsSql的扩展,也没有实现事务的写法,将会在后续的版本补充. 这是个人业余的开 ...