Author Topic: How to use TRACE_TO_FILE and TRACE_THIS_TO_FILE Macros  (Read 20919 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
How to use TRACE_TO_FILE and TRACE_THIS_TO_FILE Macros
« on: July 18, 2013, 10:50:35 am »
Hi, in cclogger.cpp, there are macros:
Code
#define TRACE_TO_FILE(msg)                                           \
    if (g_EnableDebugTraceFile && !g_DebugTraceFile.IsEmpty())       \
    {                                                                \
        wxTextFile f(g_DebugTraceFile);                              \
        if ((f.Exists() && f.Open()) || (!f.Exists() && f.Create())) \
        {                                                            \
            f.AddLine(msg);                                          \
            cbAssert(f.Write() && f.Close());                        \
        }                                                            \
    }                                                                \

#define TRACE_THIS_TO_FILE(msg)                                      \
    if (!g_DebugTraceFile.IsEmpty())                                 \
    {                                                                \
        wxTextFile f(g_DebugTraceFile);                              \
        if ((f.Exists() && f.Open()) || (!f.Exists() && f.Create())) \
        {                                                            \
            f.AddLine(msg);                                          \
            cbAssert(f.Write() && f.Close());                        \
        }                                                            \
    } 

So, how to use them?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9702
Re: How to use TRACE_TO_FILE and TRACE_THIS_TO_FILE Macros
« Reply #1 on: July 18, 2013, 11:27:20 am »
So, how to use them?
The first one enables you to add messages to a trace file which are gloablly triggered by the state of the variable "g_EnableDebugTraceFile". The second one will ignore the state and always write to the debug file. The files name must b provided though "g_DebugTraceFile", otherwise none of the both is written.

Its really for very low-level debugging only. I used it from time to time to trace freezes. But then, such debug statements across the code won't make it into SVN. So these macros are for a personal developer's convenience.

Use them as:
TRACE_TO_FILE(wxT("Entering Loop..."))
...or:
TRACE_TO_FILE(wxString::Format(wxT("Entering Loop %d..."), 5))
« Last Edit: July 18, 2013, 11:28:51 am by MortenMacFly »
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: How to use TRACE_TO_FILE and TRACE_THIS_TO_FILE Macros
« Reply #2 on: July 18, 2013, 11:55:16 am »
So, how to use them?
The first one enables you to add messages to a trace file which are gloablly triggered by the state of the variable "g_EnableDebugTraceFile". The second one will ignore the state and always write to the debug file. The files name must b provided though "g_DebugTraceFile", otherwise none of the both is written.

Its really for very low-level debugging only. I used it from time to time to trace freezes. But then, such debug statements across the code won't make it into SVN. So these macros are for a personal developer's convenience.

Use them as:
TRACE_TO_FILE(wxT("Entering Loop..."))
...or:
TRACE_TO_FILE(wxString::Format(wxT("Entering Loop %d..."), 5))

So, how do you put those macros? Did you put them in the function body of
Code
void CCLogger::Log(const wxString& msg)
void CCLogger::DebugLog(const wxString& msg)
?

Also, about this variable: g_EnableDebugTraceFile, how do you set if or reset it? Do you want to log the message when the parsing is parsing a specific file? I see there is a similar code (maybe added by Loaden, the comment below was added by me in my local copy)

Code
        // in some cases, you are only interested in the log message of some specific file
        // you can use this macro, it will set the global value g_EnableDebugTrace in the
        // Tokenizer, so DebugLog message is only print when parsing this file.
        #define TRACE2_SET_FLAG(traceFile) \
            g_EnableDebugTrace = !g_DebugTraceFile.IsEmpty() && traceFile.EndsWith(g_DebugTraceFile)
With this feature, if the parsing parsing many source files, but you are only interest the log message for one file: e.g.  aaa.cpp, you can set this variable when parsing aaa.cpp, and later reset the variable in parsing other files.

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: How to use TRACE_TO_FILE and TRACE_THIS_TO_FILE Macros
« Reply #3 on: July 18, 2013, 11:58:27 am »
BTW: why not just use the command line start up option:
Code
--log-to-file --debug-log-to-file

In this case, all the cclogger log messages are go to log files, so you can see all the locker messages (mutex or critical section locker messages) there.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2808
Re: How to use TRACE_TO_FILE and TRACE_THIS_TO_FILE Macros
« Reply #4 on: July 30, 2013, 06:22:16 pm »
Code
--log-to-file --debug-log-to-file

Where are the resulting logs. Do these parameters work on ms windows?

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: How to use TRACE_TO_FILE and TRACE_THIS_TO_FILE Macros
« Reply #5 on: July 31, 2013, 01:07:31 am »
Code
--log-to-file --debug-log-to-file

Where are the resulting logs. Do these parameters work on ms windows?

These files will named "codeblocks.log" and "codeblocks-debug.log" in the save folder of the C::B executable(codeblocks.exe)
Yes, it works under Windows.
I noticed that there are some flush issue about the log, but that doesn't hurt much, because when C::B exit, all messages were flush out.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.