GLOG multithread log - c++

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.

Related

readdir hangs up if libpocofoundation is linked with my tiny app

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.

Using DLLs wrapping Windows PackageManager in MinGW

In an attempt to incorporate a windows platform feature into an otherwise crossplatform application, I've made a one-function VC++ DLL in visual studio that uses Windows.Management.Deployment.PackageManager to get some details on all installed windows store apps.
The function works fine as a standalone application, and I can successfully build the DLL with MSVC that links properly with my MinGW main application (I can happily return primitive data from the dll, etc) - but any attempt to execute a function from the dll containing code relating to PackageManager crashes my application in runtime with the unhelpful code -529697949.
Here's some minimal code blocks that replicate:
main.cpp in the main application:
#include <QCoreApplication>
#include "mylib/WindowsAppsLib.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto hi = (Helpers::sayHi());
qDebug() << (hi);
return a.exec();
}
dll header:
#pragma once
#define WINDOWSAPPSLIB_API __declspec(dllexport)
namespace Helpers
{
extern "C" WINDOWSAPPSLIB_API const char* sayHi();
}
dll source:
#include "stdafx.h"
#include <sddl.h>
#include <collection.h>
#include "WindowsAppsLib.h"
#include <windows.h>
#using <Windows.winmd>
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace std;
const char* Helpers::sayHi()
{
auto packageManager = ref new Windows::Management::Deployment::PackageManager();
auto packages = packageManager->FindPackages();
return "Hi!";
}
Without the two lines relating to packagemanger, the program runs fine and prints "Hi!". When included, the program crashes with code -529697949 as soon as sayHi() is called. The two lines in themselves have their dependencies available and don't cause exceptions.
Any clues on how I might proceed to investigate this? Nothing I've been able to get out of this system is getting me closer to identifying the problem. Is this the sensible way to access Windows.Management.Deployment.PackageManager from within a plain C++ MinGW application to begin with?

log4cxx: try/catch wont work on DOMConfigurator::configure

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

LD_PRELOAD with boost fileystem library

I am trying to intercept open system call in Linux. It works fine with other libraries but doesn't wotk with boost libboost_fileystem. Here is my code (stripped down for readability).
#include <boost/filesystem/fstream.hpp>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <stdio.h>
using namespace std;
using namespace boost::filesystem;
typedef int (*open_func_type)(const char * pathname, int flags, ...);
int open(const char *path, int flags, ...)
{
va_list arg;
mode_t mode = 0;
if (flags & O_CREAT)
{
va_start(arg, flags);
mode = va_arg(arg, mode_t);
va_end(arg);
}
//some stuff here
open_func_type open_func = (open_func_type) dlsym(RTLD_NEXT, "open");
return open_func(path, flags, mode);
}
int main()
{
boost::filesystem::fstream build_path;
build_path.open("/tmp/test.txt", ios::in);
//other stuff
return 0;
}
I stepped though the code using gdb, my open implementation doesn't get called. But doing strace shows the open system call being called. If I call other library functions that call open, I see my implementation getting called. Anything that I am doing wrong here? I am dynamically linking with boost libraries.
I gave sometime to figure it out and found that internally boost calls std::basic_filebuf open which in turn calls fopen. fopen seems to call open system call in the kernel without calling open library call (it would be nice if somebody could point it out why). I intercepted fopen call and this works fine now. If large file support is used, fopen64 also needs to be implemented.

I am getting error in c++ programm while using log4cxx

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