473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

make logfile out of cout

8
hello all!

i have to make a project for school , making a newsserver and a client.
the thing is, i send messages between them to let the user know if everything is running ok. just some simple http messages as 201, etc.

as a extention id like to build in a log file maker.
what i want is: everything that is put on the output stream will also appear in a logfile.

i already found this:

Expand|Select|Wrap|Line Numbers
  1.  ofstream out("logFile.txt");
  2.   if(out) {
  3.     streambuf *pStreambuf = cout.rdbuf();   // save cout streambuf
  4.     cout.rdbuf(out.rdbuf());                // give cout file's streambuf
  5.  
  6.     // The following text will be written to LogFile.txt.  You might
  7.     // want to do something like this to give a user the option of
  8.     // seeing output on-screen, or getting it off-line in a file.
  9.  
  10.     cout << "\n  This output text will be displayed on cout, "
  11.          << "\n  then redirected to a log file using streambufs."
  12.          << "\n  Note that all output is written to cout."
  13.          << "\n  Redirection occurs by giving cout the streambuf"
  14.          << "\n  of the log file\n\n";
  15.  
  16.  
  17.  
  18.     cout.rdbuf(pStreambuf);                 // restore cout streambuf
  19.     out.close();
  20.  
  21.  
this is what i want, except for the fact that i dont see anything in the terminal then. everything is outputted in the file.
I want it to appear on the user's screen as well as written in the file.

is there a way so i can simply specify that whats on cout should be coppied into this logfile? and so i dont have to add to each std::cout i have now:
ofstream file("logfile");
file << INPUTTEXT;

Thank you all for reading!
Jan 5 '08 #1
2 2852
Laharl
849 Expert 512MB
You can associate cout with a file if you want, but then it doesn't write to the monitor. The easiest way to do this is to write a function that takes the desired output as a parameter, most likely in string form, then outputs to the file and stdout.
Jan 6 '08 #2
pompom
8
Thank you, that did the trick like i wanted :-)
Jan 6 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: guf | last post by:
Hi All, I have SQL Server 2000 sp3, Windows 2000 Server. I have a database which is used by an application server. I do several important jobs a day in the application server and never access my...
2
by: ge_orgy | last post by:
Accidently, I(we) deleted the Logfile.LDF in sysfiles folder on the mssql server and now can't enter the database, which status now is set SUSPECT. Before deleting the logfile I did a...
4
by: robinsand | last post by:
Header File: car.h #if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car();
2
by: Andi Clemens | last post by:
Hi, we had some problems in the last weeks with our mailserver. Some messages were not delivered and we wanted to know why. But looking through the logfile is a time consuming process. So I...
6
by: roger.lagrilliere | last post by:
Hello, is anyone willing to help me out to setup a logfile. I have a program running in Access, but I want to know the progress of it. It is rather easy I think, but don't know how to handle...
0
by: indiarocks | last post by:
Is there a way in which the all the logging done using pexpect can be sent to stdout as well as a file. eg. filea = file('output.log',"w") ssh = pexpect.spawn('ssh root@1.1.1.1') ssh.logfile...
0
by: Adam | last post by:
Hello, I have a small app I am creating to crawl a directory and check that if it is moved to another a location it's path will not break a character limit. Usually the Windows path limit. ...
5
by: clairelee0322 | last post by:
I am a C++ beginner and I am working on a C++ project that calucates the phone bill. I got stuck on how to make every 60 minutes into one hour.. Like if the user enter 1000 (10:00) and it asks for...
0
by: Frank Birbacher | last post by:
Hi! sphenxes@gmail.com schrieb: Why is the referenceNumber a string? Are there always *exactly* six alternatives? The alternatives could be a std::deque which can resize as needed.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.