Well, I just wrote a simple logger program with log4cxx lib, It is working fine so far, but then I realized the exception handling doesnt work as I expected:
#include <iostream>
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/helpers/exception.h>
using namespace std;
using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::helpers;
int main(int argc, char *argv[])
{
/* some code here ... */
try
{
DOMConfigurator::configure("/path/to/logcfg.xml");
}
catch (Exception&)
{
cout << "error: problem in reading log config file" << endl;
/* here I want to free up some objects! */
exit(EXIT_FAILURE);
}
}
So, now lets say the logcfg.xml does not exist, the program will print out this message and exit:
log4cxx: Could not open file [/wrong/path/to/logcfg.xml].
Which seems to me, it never reached my exception handler, but raised and handled in library itself. Could you please tell me what is proper way to handle such case ?
Thanks
Related
I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.
I want to use Glog lib to log my application logs. My application is multithreaded. As Suggested in glog i have supposed to use RAW_LOG for thread safety. Here is my example code.
#include "stdafx.h"
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include <windows.h>
#include <glog/logging.h>
#include <glog/raw_logging.h>
using namespace std;
int main(int /*argc*/, char** argv)
{
FLAGS_alsologtostderr = 1;
google::SetLogDestination(google::GLOG_INFO, "E:/mylog.log");
google::InitGoogleLogging(argv[0]);
//LOG(INFO) << "Infomration";
RAW_LOG_INFO("Test");
RAW_LOG(INFO,"This is INFO");
RAW_LOG(WARNING,"This is WARNING");
RAW_LOG(ERROR, "This is Error");
return 0;
}
But log files are not generating if I use RAW_LOG but if I use LOG() function then log file is generating.
Please let me know how do I use RAW_LOG function or is LOG() function is threadsafe or not.
this is from the raw_logging.h
// * it logs straight and ONLY to STDERR w/o buffering
It can only log to stderr and is only for debugging purposes in low level code
so RAW_LOG(INFO,"...") will never generate any log files.
When I started to use VS2013, I created just very basic application like this.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello!";
return 0;
}
It crashed and when I commented out the #include <iostream> its no longer crash. I did several research on this error but nothing is suitable for my situation. This is the error :
Thanks for all your helps.
Once you create a new project, if you create it as an empty project I don't think you will face this issue. Then, you start it from scratch and you use int main() instead of that _tmain(...) and DO NOT EVER use using namespace std;
start a new EMPTY project and use something like this:
#include <iostream>
int main()
{
std::cout << "Hello World";
return 0;
}
Does anyone know how to change the settings for the automatic creation of the main method in Netbeans (for C++) at least?
The problem is that the main method that is automatically created forgets to
#include <iostream>
So when I do a simple "helloworld" program, even though it does "using namespace std", I get an error on the line with the cout and the endl.
# include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
cout << "Hello!" << endl;
return 0;
}
I believe you can change it in the menu:
tools->models, choose the template that you want to change and click in "open in editor"
I never try it, but you can try and see if it works
I am getting error in c++ program while using log4cxx. The error message is:
error:Please initialize the log4cxx system properly
Please help to resolve,
Thanks in advance.
AFAIR you have to configure the log4cxx system at the beginning of your program, e.g. using a BasicConfigurator as shown in this Short introduction to Apache log4cxx:
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr logger(Logger::getLogger("MyApp"));
int main(int argc, char **argv)
{
// Set up a simple configuration that logs on the console.
BasicConfigurator::configure();
LOG4CXX_INFO(logger, "Entering application.");
// ...
return 0;
}
HTH
Martin