RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1784 | Replies: 2
Reply
Join Date: Jun 2005
Posts: 69
Reputation: DotNetUser is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
DotNetUser DotNetUser is offline Offline
Junior Poster in Training

Making a log file global

  #1  
Apr 24th, 2006
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();
Last edited by Dave Sinkula : Apr 25th, 2006 at 10:50 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 69
Reputation: DotNetUser is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
DotNetUser DotNetUser is offline Offline
Junior Poster in Training

Re: Making a log file global

  #2  
Apr 26th, 2006
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,639
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 986
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Making a log file global

  #3  
Apr 26th, 2006
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:52 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC