利用可变参数打印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 ...
随机推荐
- C语言程序设计II—第八周教学
第八周教学总结(15/4-21/4) 教学内容 本周的教学内容为: 8.4 电码加密 知识点:指针与字符串,重难点:字符指针与字符串的关联和区别: 8.5 任意个整数求和 知识点:动态内存分配的概念和 ...
- Java中try catch finally语句中含有return语句的执行情况(总结版)
在这里看到了try >但有一点是可以肯定的,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有return语句的话,那么直接从finally中返回了,这也 ...
- Python常见十六个错误集合,你知道那些?
使用python会出现各种各样的错误,以下是Python常见的错误以及解决方法. 1.ValueError: 'Conv2d_1a_3×3' is not a valid scope name 这个是 ...
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- [Oracle]Oracle 各产品的 生命周期
http://www.oracle.com/us/support/library/lifetime-support-technology-069183.pdf
- 线程中join()的用法
Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行. public static void main(String[] args) throws Interrupted ...
- B. Interesting drink
链接 [http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/B] 题意 给你n个数,q次查询,每次输入一个m,问n个数中有多少个数小于 ...
- 个人博客week2
1. 是否需要有代码规范 对于是否需要有代码规范,请考虑下列论点并反驳/支持: 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 我是个艺术家,手艺人,我有自己的规 ...
- UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现(转)
UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现 类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统 ...
- 选择J2EE的SSH框架的理由
选择J2EE的SSH框架的理由 Struts2框架: Struts2框架的基本思想是采用MVC设计模式,即将应用设计成模型(Model).视图(View)和控制器(Control)三个部分:控制部分由 ...