logmsg.h

void LOGMSG(string logmsg,...)//...
{
	stringstream name;
	name<<"test "<<TM.years()<<"-"<<TM.month()<<"-"<<TM.day()<<".log";
	ofstream log;
	log.open(name.str(), ios::out | ios::app);
	log <<TM.hour()<<":"<<TM.mint()<<":"<<TM.sec()<<" Message: "<< logmsg<<endl;
}

main.cpp

int main()

{
	int a = 100;
	LOGMSG("type test!!",a,"times");
}

but it's only wtite into the file "type test!!" while i want it to type in "type test 100 times"

how can i do that

Recommended Answers

All 9 Replies

You should either convert the a into a string or adjust your parameters. For example :

template<typename ReturnType, typename InputType>
ReturnType convertTo(const InputType& input){
 stringstream stream;
 stream << input;
 ReturnType val = ReturnType();
 stream >> val;
 return val;
}
//...your LOGMSG function
int main(){
 int a = 100;
 string msg = "type test!!" + convertTo<string>(a) + " times";
 LOGMSG( msg );
}

You should either convert the a into a string or adjust your parameters. For example :

template<typename ReturnType, typename InputType>
ReturnType convertTo(const InputType& input){
 stringstream stream;
 stream << input;
 ReturnType val = ReturnType();
 stream >> val;
 return val;
}
//...your LOGMSG function
int main(){
 int a = 100;
 string msg = "type test!!" + convertTo<string>(a) + " times";
 LOGMSG( msg );
}

this wont work for me why!!?

becuase i will use the LOGMSG to write in the Error so for eg
i got a mysql error!
so it should be like this

LOGMSG("MysqlError",mysql_error())

but how can i make the function handel somthing like mysql_error() or even if am coding aonther thing and i need to put a warrnin/error or MSG

the way you give me i will keep making new var. adding to it the msg then pass the msg to LOGMSG(); there should be a way to do wat am asking for right?!

So in the above example, you want the message generated by myeql_error() to be logged into the file? I am assuming mysql_error() returns a string or something similar?

So in the above example, you want the message generated by myeql_error() to be logged into the file? I am assuming mysql_error() returns a string or something similar?

yes

look i got part of the code that i want make but it's not complete and i dont know how to work on it

here it is

inline void	log_InlineLog##y(const char * sFormat, ...)\
{\
	va_list argptr;\
	va_start( argptr, sFormat);     /* Initialize variable arguments. */\
\
	time_t ltime;\
	time( &ltime );\
	tm *	pTm = localtime(&ltime);\
	char	buf[4096] = "";					
	sprintf(buf, "%02d:%02d:%02d " #y ": ", pTm->tm_hour, pTm->tm_min, pTm->tm_sec);\
	int		ret = vsprintf(buf + strlen(buf), sFormat, argptr);\
	strcpy(buf + strlen(buf), "\n");\
	if(!(ret < 4096-2)) log_SaveFile(x, "ASSERT(ret < 4096-2)");	
\
	va_end( argptr );              /* Reset variable arguments.      */\
\
	log_SaveFile(x,buf);\
}
extern char		log_szFilename[];
log_InlineLog(log_szFilename, Message)
#define LOGMSG			log_InlineLogMessage

do u have anyclue on how it's working

Edit: forgot to put the last line

So you want something like this then.

#include <iostream>
#include <string>

using namespace std;

typedef string (&ErrorFunc)();

string myError(){ return "Failure"; }

void logFile(const std::string& id,const ErrorFunc func){
  cout << id << func() << endl;
  //log into file
}

int main(){
 logFile("Msg : ", myError );
}

nope i want the func to handel it automatic like for e.g.

int a=5;
int b=10;
int c=11;

LOGMSG(a,"just test",b,c)

so when ever i put a vriable or const it's automatic wrting it into the file got me!?

then overload it. One for functions. And one with a variable argument list.

then overload it. One for functions. And one with a variable argument list.

like i know how to :d

give me an example thnx for your kinddly help

nvm i will work on it like the way u said

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.