Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I tried to define like this #def

Status
Not open for further replies.

AMosmann

Programmer
May 27, 2003
82
0
0
DE
I tried to define like this

#define AA "A" //AA a defined Value
TRACE("#%s#\n",AA); //result #A#
#define BB AA //BB as a temp storage of AA
TRACE("#%s=%s#\n", BB , AA );//#A=A#
#define AA "a" //new Value to AA
TRACE("#%s#(%s)#\n",AA , BB );//#a#(a)#

why does the precompiler change both, AA and BB, and how to avoid this?

[3eyes]





Greetings Andreas
 
Ok...

#define AA "A" will replace all AA's in your code with "A"
#define BB AA will replace all BB's with AA's which will replace all AA's in your code with "A"

#define AA "a" will replace all AA's in your code with "a" but the BB will replace all BB's with AA's which will replace all AA's with "a" in your code.

#defines are preprocessor commands that get compiled in, they do not work like variables.

Matt
 
thx. Is there another way?


I have written som macros for debug, that write some texts to a file (XTRACE), and for special paragraphs I would like to choose a special file.

example:


#define TRACEFILE "" //will not write in File

XTRACE (...); //nothing happens

#define TRACEFILE "WriteThisDown.txt"

XTRACE (...); //works fine, traces to file

#define TRACEFILE "" //set back to NoWrite


I would like do something like

#define OLDFILE TRACEFILE
#define TRACEFILE "AnySpecialFile.txt"

XTRACE (...)

#define TRACEFILE OLDFILE

Any Idea?

:-|






Greetings Andreas
 
#define XTRACE(filename, strData) { CStdioFile f; f.Open(filename,CFile::modeWrite|CFile::modeNoTruncate); f.SeekToEnd(); f.WriteString(strData); f.WriteString("\n"); f.Close(); }

This macro is untested

Matt
 
How about

const char* OLDFILE = TRACEFILE;
#define TRACEFILE "AnySpecialFile.txt"

XTRACE (...)

#define TRACEFILE OLDFILE

~~~~~

However i would not recommend using defines at all.

There are many different ways you could do traces, for example your XTRACE could have a parameter for filename

Regards,
Rich.


 
In the Headerfile "MyPrivateFunctions.h"

extern CString XTraceStringOnlyInMacro;

#ifdef XTRACE_ENABLE

#define XTRACE10(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE9(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8,S9) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8,S9); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE8(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE7(FORMAT,S1,S2,S3,S4,S5,S6,S7) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3,S4,S5,S6,S7); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE6(FORMAT,S1,S2,S3,S4,S5,S6) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3,S4,S5,S6); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE5(FORMAT,S1,S2,S3,S4,S5) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3,S4,S5); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE4(FORMAT,S1,S2,S3,S4) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3,S4); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE3(FORMAT,S1,S2,S3) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2,S3); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE2(FORMAT,S1,S2) XTraceStringOnlyInMacro.Format(FORMAT,S1,S2); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE1(FORMAT,S1) XTraceStringOnlyInMacro.Format(FORMAT,S1); XTRACE(XTraceStringOnlyInMacro);

#define XTRACE(FORMAT) LogToFile(0, FORMAT, 0, __LINE__ , TRACEFILE)

#else
#define XTRACE10(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10)
#define XTRACE9(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8,S9)
#define XTRACE8(FORMAT,S1,S2,S3,S4,S5,S6,S7,S8)
#define XTRACE7(FORMAT,S1,S2,S3,S4,S5,S6,S7)
#define XTRACE6(FORMAT,S1,S2,S3,S4,S5,S6)
#define XTRACE5(FORMAT,S1,S2,S3,S4,S5)
#define XTRACE4(FORMAT,S1,S2,S3,S4)
#define XTRACE3(FORMAT,S1,S2,S3)
#define XTRACE2(FORMAT,S1,S2)
#define XTRACE1(FORMAT,S1)
#define XTRACE(FORMAT)
#endif

In the CPP File "MyPrivateFunctions.cpp"

void LogToFile(int Level, CString sMessage, int Tracelevel, /*CString Time,*/ int Line, CString File){
if (File.GetLength()>0){
//Es wird nur geschrieben, wenn tatsächlich eine Datei angegeben ist
bool IsNewLine=false;
if (Level<=0){
Level=-Level;
IsNewLine=true;
};
if (Level<=Tracelevel){
CString OutputLine=&quot;&quot;;
if (IsNewLine){
OutputLine.Format(&quot;\n%s&quot;,&quot;&quot;);
CString Time;
Time.Format(&quot;%10.1f&quot;,(float)(GetTickCount()-StartTickCount)/1000.);
if (Time==LastTimeStamp){
for (int i=0;i<LastTimeStamp.GetLength();i++) OutputLine+=&quot; &quot;;
}else{
OutputLine+=Time;
LastTimeStamp=Time;
};
CString sLineNumber;sLineNumber.Format(&quot; %5i:&quot;,Line);
OutputLine+=sLineNumber;
};
OutputLine+=sMessage;
ofstream f;
f.open(File,ios::eek:ut | ios::app);
f << OutputLine;
f.close();
};
};
}

CString XTraceStringOnlyInMacro;

In any other files of your project

#define TRACEFILE &quot;&quot;
XTRACE (&quot;-----&quot;);nothing happens

#define TRACEFILE &quot;MyTraceFile.txt&quot;
XTRACE (&quot;-----&quot;);writes to MyTraceFile.txt

Believe me, it is much easier to use than to write filename into the Parameter list.
Besides: I put the line number to the file (could do it with filename too), the most simple way to get the correct line number to the file is to use a macro, 'cause this is substituted by compiler...

Try It out, if you want, it helped me a lot.
The only problem I still have is like I said: how to switch between #define's. Thx


[cheers]



Greetings Andreas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top