DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Making a log file global (http://www.daniweb.com/forums/thread44031.html)

DotNetUser Apr 24th, 2006 7:24 pm
Making a log file global
 
I'm coding in VC++.NET 2003. I have a class that needs a log file to log info and errors for debugging. The problem I have is that FileStream and StreamWriter can not be global. I get compiler error C3145 : cannot declare a global or static managed type object or a __gc pointer. A workaround was to open, write, and then close the log file in every function/event. During testing, I discovered that this won't work since other functions/events may be executed before the previous function/event has closed the file. Any suggestions?

  FileStream* fs = new FileStream(S"c:\\Variables.txt", FileMode::Append, FileAccess::Write, FileShare::Write);
  fs->Close();
  StreamWriter* sw = new StreamWriter(S"c:\\Variables.txt", true, Encoding::ASCII);
  String* NextLine=S"This is the appended line.";
  sw->Write(NextLine);
  sw->Close();

DotNetUser Apr 26th, 2006 12:39 pm
Re: Making a log file global
 
I got it. Create a function that opens the file/stream, writes to it, and closes the file/stream. Pass in the string to be logged. So, instead of writing directly to the file, call the function.

Ancient Dragon Apr 26th, 2006 2:28 pm
Re: Making a log file global
 
I do similar, but I put that log function in another thread so that it doesn't slow down other processing. you can put the log requests into a queue, then the log function can take its own sweet time processing them. Also, I always close the file when the last item in the queue has been logged.


All times are GMT -4. The time now is 2:42 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC