passing iostream through functions

I am trying to create a logging function that accepts loggin info (strings, variables, etc) and an options label ('mask' in this case).

To do this I think I need to pass an IO buffer through to my function so I am trying to figure out how to do this. I can see why it doesn't work below, but I am trying to figure out how to make it work. I am trying keep the code in the following "main()" function to a minimum as it needs to be useful for others.

This is my first post and I am still fairly new at C++. Please be kind!

~Stew

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;

void logger(streambuf input, short mask)
{
	ofstream fout("Log.txt");

	if (mask & 0xf0) fout << input;
	if (mask & 0x0f) cout << input;
	
	fout.close();
}

void main ()
{
	int i = 3;
	logger("Problems found with int i = " << i << ". Investigate this", 0xff);
	logger("Other parameters are lookin' good."                       , 0xf0);
	logger(endl<<endl<<endl<<endl<<"I like spaces!"<<endl             , 0x0f);

	cin.get();
}
Last edited on
closed account (zb0S216C)
If you want to pass std::cout (or whatever) to a function, it's best to pass is by reference. std::cout is of type std::ostream. So, to pass std::cout, you would write something like this:

1
2
3
4
5
6
7
8
9
10
void Function( std::ostream & );

int main( )
{
}

void Function( std::ostream &Stream )
{
    Stream << "..." << std::endl;
}


Wazzak
Last edited on
Topic archived. No new replies allowed.