I am a little confused with what might be a standard C++ issue, but I don't get it. There is much to read about namespaces and global variables, but I can't see the error in my case.
My app uses a function in global namespace, to write into a log file whenever I like to.
For that purpose, I implemented into the header of
myApp.h
Code: Select all
namespace Log
{
extern bool writelogfile;
extern wxString sLastLog;
void SetWriteLogFile(bool bWriteLog);
void DoLog(wxString logstring, bool bForceLog = false, bool bNoDoubleLog = false);
};
myApp.cpp
Code: Select all
namespace Log
{
bool writelogfile = false;
wxString sLastLog = "";
void SetWriteLogFile(bool bWriteLog)
{
writelogfile = bWriteLog;
}
void Log::DoLog(wxString logstring, bool bForceLog, bool bNoDoubleLog)
{
if (bNoDoubleLog)
{
if (sLastLog == logstring)
{
return;
}
}
bool bLogging = false;
if (bForceLog)
{
bLogging = true;
}
else if (writelogfile)
{
bLogging = true;
}
if (bLogging)
{
sLastLog = logstring;
wxLogNull logNo;
wxFileName fn_logfile = wxStandardPaths::Get().GetExecutablePath();
fn_logfile.SetExt("log");
wxString datetime = wxDateTime::Now().FormatISOCombined();
wxString buf = "";
buf << datetime << " - " << logstring;
wxTextFile file(fn_logfile.GetFullPath());
if (!fn_logfile.FileExists())
{
file.Create();
}
if (file.Open())
{
while (file.GetLineCount()>500)
{
file.RemoveLine(0);
}
file.AddLine(buf);
/*Datei speichern*/
file.Write();
file.Close();
}
}
}
};
Code: Select all
MyApp::OnInit
Code: Select all
Log::SetWriteLogFile(true);
To use them, I loaded the files to my project source files and #include "log.h" in myApp.h, instead of declaring the namespace there. But if I do that, I run into several linker errors, complaining about unresolved symbols.
As a matter of fact, I already have another namespace outsourced to h/cpp just like this one, but it isn't used in the OnInit of the main class.
Is this the problem I might be facing here? (Is the #include resolved to late?) Or am I just color blind atm?
Thank you!
Natu