Logging Method Formats

I am creating a logging namespace for a game that I am creating. The logging includes a file, and it would contain the following methods:
1
2
3
4
OUT::log(?)
OUT::warn(?)
OUT::critical(?)
OUT::debug(?)


I will want to call things like
OUT::log("Something happened");
and
OUT::log("Code process " + int proc + " occurred.");
but this might have to be changed to something like
OUT::log("Code process ", int proc, " occurred.");
What would I put in place of "?" to accomplish this?

I don't want to create 30 different methods for this, ie, not
1
2
3
4
5
OUT::log(string, int, string);
OUT::log(string, long, string);
OUT::log(string, double, string);
OUT::log(string, int);
OUT::log(string, int, string, int, string);

...I do NOT want to do this...too much chaos.
I understand in C++11, I could do something with int like to_string, and that would make the "string" + int + "string" to work.

So I need some help. Any suggestions?
Last edited on
Just make all those functions take a single std::string: the content you want to log. Then you should be able to just do
OUT::log("text " + std::to_string(stuff) + " other text");
or anything that creates a string.
closed account (3hM2Nwbp)
Stream operators on a wrapper construct to an underlying output stream? Seems like all of the functionality is already there without any extra tedious coding on your part.
Okay that should work. I was wanting to not install gcc-4.8, but I figured the benefits outweigh the disadvantages.
I believe Luc Lieber described a solution that didn't require C++11: http://www.cplusplus.com/reference/ostream/ostream/

However, there's various other libraries (3rd party) that do similar syntax as well which might be more specific to your needs if you really want to avoid C++11.
Last edited on
Having another problem then. I install g++-4.8 and I am still getting
Main_Main.cpp:29:48: error: ‘to_string’ is not a member of ‘std’

Seems like others have had this problem. Any suggestions?

Yes, I am using the right g++ now, still having the problem.

g++ --version
g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Last edited on
The <string> header is required. There might also be a requirement for "-std=c++11" on the command line if you wish to use C++11.
Thanks, the -std=c++11 worked. I remember reading that somewhere, just forgot to implement it.
Topic archived. No new replies allowed.