一份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;
} 






#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);
}

你可能感兴趣的:(C++,c,F#,C#,FP)