Casablanca - prevent console output
-
Saturday, September 01, 2012 7:30 PM
Hi,
I am developing a console application using Casablanca and sometimes, when an error occurs, the library prints on my console window. Is there any way to prevent this, or a way to disable console outputs?
All Replies
-
Wednesday, September 05, 2012 3:28 PMOwner
This is clearly an irritating "feature," one we're addressing in the upcoming refresh.
For now, it should be fixable by setting the default log object to 'nullptr,' using actors::log::set_default(nullptr); do this at the beginning of your application (in main() on the client, or on_initialize() on the server).
Caveat: I can't say that we have tested this a lot, but all the code does seem to be checking for NULL properly.
Niklas
-
Monday, September 10, 2012 5:35 PM
I tried to disable the outputs by setting the default log to 'nullptr', but it didn't work for me. When I try to access an invalid url, for example, it prints on the console window "Error 12007: Error in: WinHttpSendRequest: The DNS server could not by Resolved"
The same thing occurs when I start the TrivialServer example. It prints: "Registering the default server API Windows HTTP Server", and then, "Registering to listen for HTTP requests at http://localhost:4711/trivial..."
Here is the code:
int main(int argc, char* argv[])
{
actors::log::set_default(nullptr); // Try to prevent console outputs
http_listener::create("http://localhost:4711/trivial", [](http::http_request message)
{
message.reply(http::status_codes::OK, "Hello World!");
//std::cout << "Served a request..." << std::endl;
})
/* Prevent Listen() from returning until user hits 'Enter' */
.listen([]() { fgetc(stdin); }).wait();
return 0;
}Am I doing something wrong?
Marcelo
- Edited by Marcelo-Mattos Monday, September 10, 2012 5:38 PM
-
Monday, September 10, 2012 5:59 PMOwner
Nope, you're not doing anything wrong. This is a bug. I've stepped through the code and it fails.
The only workaround for the bug that I can think of is to implement a log class (derived from actors::_Log, overriding the two post() functions) that does nothing when given a string to post. Then, pass a pointer (or shared pointer) to an instance of this class to the set_default() function.
Good thing you found this and reported it before we refresh (we're hoping that will happen this week)!
Niklas
- Proposed As Answer by Niklas GustafssonOwner Monday, September 10, 2012 5:59 PM
- Marked As Answer by Marcelo-Mattos Monday, September 10, 2012 6:57 PM
-
Monday, September 10, 2012 8:13 PM
The proposed workaround worked nicely.
The console outputs stopped and now I can store the messages into a log file.
Perfect.class Log : public actors::_Log
{
private:
virtual void post(actors::log_level level, const std::string &message);
virtual void post(actors::log_level level, int code, const std::string &message);
virtual void post(actors::log_level level, const std::wstring &message);
virtual void post(actors::log_level level, int code, const std::wstring &message);};
void Log::post(actors::log_level level, const std::string &message) { /* Here you can store the message into a log file */ }
void Log::post(actors::log_level level, int code, const std::string &message) { /* Here you can store the message into a log file */ }
void Log::post(actors::log_level level, const std::wstring &message) { /* Here you can store the message into a log file */ }
void Log::post(actors::log_level level, int code, const std::wstring &message) { /* Here you can store the message into a log file */ }Thanks for the help.
Marcelo
- Edited by Marcelo-Mattos Monday, September 10, 2012 8:14 PM
-
Monday, September 10, 2012 9:51 PMOwner
BTW, we have an internal file log implementation that maybe we should make public...
I've checked in afix to the nullptr problem, so it will definitely be in the refresh.
Niklas

