C/C++写日志代码

C代码

#include "stdafx.h"
#include <stdio.h>
#include <time.h>

#define DEBUG_LOG( str ) log_append_to_file("c:\\test.txt", str,__FILE__,__LINE__ ); 

void log_append_to_file(char* filename,char* str,char* sourceFile,int fileLine)
{
 time_t t;
 time(&t);
 struct tm* tp= localtime(&t);
 printf("%x\n",tp);
 char now_str[100];
 strftime(now_str, 100, "%Y-%m-%d %H:%M:%S", tp);

 FILE *fo;
 fo = fopen(filename, "a");
    if (fo == 0) { 
        return;
    }

 fprintf(fo, "%s %s(:%d):%s\r\n",now_str,sourceFile,fileLine, str);
 fclose(fo);
}

int main(int argc, char **argv)
{
/*********************define******************/

 DEBUG_LOG("test");
 printf("Hello World!\n");
 return 0;
}

 

C++代码

#include <time.h>
#include <stdio.h>
#include <string.h>

#ifndef HYP_COMMON_INLINE_H
 #define HYP_COMMON_INLINE_H

#define DEBUG_LOG( str ) printf( "%s(:%d):%s",__FILE__,__LINE__, str );

struct SpeedMardData
{
 time_t startT;
};

static void fatal(char* message);
static void* xmalloc(size_t size);
static void SpeedMard_Start(struct SpeedMardData* data);
static void SpeedMard_stopAndPrint(struct SpeedMardData data,int testCount);

inline void fatal(char* message){
 printf("fatal:%s \n",message);
 exit(0);
}

inline void* xmalloc(size_t size) {
 register void *value = malloc(size);
 if (value == 0)
  fatal("virtual memory exhausted");
 return value;
}

inline void SpeedMard_Start(struct SpeedMardData* data)
{
 data->startT =  time(NULL);
}

inline void SpeedMard_stopAndPrint(struct SpeedMardData data,int testCount)
{
 time_t now = time(NULL);
 printf("now:%d \n",(int)now);
 printf("data.startT:%d \n",(int)data.startT);
 double dTime = difftime (now, data.startT);
 printf("dTime:%f speed %f times/sec \n", dTime,(double)testCount / dTime);
}

inline void debug_append(const char* filename,const char* str)
{
     FILE *fp;
     fp=fopen(filename,"a");
     fwrite(str,sizeof(char),strlen(str),fp);
     fwrite("\n",sizeof(char),1,fp);
     fclose(fp);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值