printf functionality from cout?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
LordMage
Member
Member
Posts: 115
Joined: Sat Sep 22, 2007 7:26 am
Contact:

printf functionality from cout?

Post by LordMage »

Okay, I like cout and I like printf but I would rather use C++ versions of functions for my OS. I have jimmy rigged a cout function using custom typedefs.

I have right now 3 overloaded versions of the operator<< ostream function.

cout << "TEST"; // prints TEST to the screen.
cout << (int)0x20; // prints 32 to the screen using an i to a function.
cout << "0x" << (hex)0x20; // prints 0x20 using the same i to a function modified for base 16.

What I want to know is if there is a better way to incorporate the printf % options in a cout style statement?

Thanks for any suggestions
Getting back in the game.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: printf functionality from cout?

Post by CodeCat »

I've opted to have both in my kernel. Not just for the sake of convenience, but also because of the issue of locking. If several threads are using cout, you might get a mixup of the output. A solution would be to require that every use of cout uses endl to release the lock, but it's easy to forget that (and will result in blocking a thread if you do!). So I decided to do it a little differently from the standard C++ way:

- There is a single 'console' object, which handles raw output.
- You can do Kout::printf, which locks the console, prints a string, then releases it.
- You can also acquire a 'console handle', which I usually call kout, which is a local object that allows you to do C++-style formatting. But the object gets destructed at the end of scope, so the lock is never held indefinitely.

An issue with this approach I haven't fixed yet is that a function that holds the lock with a console handle can't call functions which also require a lock. If you do, the second function will block indefinitely because its caller already holds the lock. I realise I might be making this way too complicated, but it's the best solution I've been able to think of.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: printf functionality from cout?

Post by jal »

LordMage wrote:What I want to know is if there is a better way to incorporate the printf % options in a cout style statement?
I assume you mean "formatting options". I use an enum type that is one of the types for the << overloading:

Code: Select all

enum CtrlChar_t { newline, hex };
...
	CVideo& operator<<(CtrlChar_t CtrlChar);
Then in the handling code, I have something like this:

Code: Select all

CVideo& CVideo::operator<<(CtrlChar_t CtrlChar)
{
	switch (CtrlChar)
	{
		case newline:
			NewLine();
			break;

		case hex:
			OutputHex = true;
			break;
	}

	return *this;
}
This allows me to do things like this:

Code: Select all

*pVideo << "  addr: " << hex << pSection->sh_addr << newline;

JAL
LordMage
Member
Member
Posts: 115
Joined: Sat Sep 22, 2007 7:26 am
Contact:

Re: printf functionality from cout?

Post by LordMage »

thank you,

That is precisly the type of thing I wanted, that allows me to specify what I want without having to define the variable to be specifically that type.
Getting back in the game.
Post Reply