How to trace MFC Serialize calls with Macros
I got a MFC app that is writing a huge hierarichy of objects to disk. To make sense of what is being written I thought of logging all the calls to archive << via stream insertion and .write method by replacing those with macros
#pragma once
#ifndef LOGMAGIC
#define LOGMAGIC
class LogTab
{
public:
static int LogIndentCount;
LogTab()
{
LogIndentCount++;
}
~LogTab()
{
LogIndentCount--;
}
};
#defi开发者_开发百科ne ARINSERT(AR,OBJ) TRACE( "%*s %s\n", LogTab::LogIndentCount, #OBJ); AR << OBJ;
#define ARWRITE(AR,OBJ,SIZE) TRACE("%*s %s\n", LogTab::LogIndentCount, #OBJ); AR.write(OBJ, SIZE);
#endif
So I created above snippet of code and put it in stdafx.h but I'm getting the following error:
Error 1 error LNK2001: unresolved external symbol "public: static int LogTab::LogIndentCount" (?LogIndentCount@LogTab@@2HA)
What am I doing wrong? Is there a better way to achieve what I am doing?
You have to define LogTab::LogIndentCount
in any one of the .cpp files as,
#include"LogTab.h"
//...
int LogTab::LogIndentCount = 0;
[As a side note, if it's a multi threaded system which is using this class
then you may think of making LogIndentCount
synchronized (thread safe)]
A static variable must be explicitly initialized.
精彩评论