2

I'm trying to use the g2log logging library inside my DLL project using VS2012.

The problem is that looking at the documentation LINK」 it needs to be initialized at the beginning and stay alive for the lifespan of the program. Since I don't have a main, and using DllMain doesn't help, where could I do the initialization? (I started this project recently so I also accept suggestions about other options for a logging library)

Ralf
  • 9,405
  • 2
  • 28
  • 46
キキジキ
  • 1,443
  • 1
  • 25
  • 44

2 Answers2

4

As you are using VS2012 you can use C++11 which means you can have thread-safe statics so you can add a global function:

Logger& get_logger ()
{
   static Logger logger;
   return logger;
}

And then use that get_logg() to get hold of the object (if you need more elaborate construction/initialization you can also do that inside this function using std::atomic<> from C++11). Here is a version using std::atomic_flag:

Logger& get_logger ()
{
   static std::atomic_flag is_initialized;
   if (! is_initialized.test_and_set()) {
     /* do lots of initialization */
   }

   return some_object;
}

I do not use or know the g2log library so I cannot give more explicit examples.

mauve
  • 1,976
  • 12
  • 18
1

I'm planning on doing something similar. Somewhere, you are using that dll. In your user code you might call the initialization procedure, where you could also configure g2log.

Dmitry Ledentsov
  • 3,620
  • 18
  • 28
  • Yes, I only hoped to find a way so that it would be transparent for the projects the library. In the dll I basically export only static factories, maybe I can use that. – キキジキ Oct 24 '12 at 10:10
  • if you separate g2log into a dll, then maybe you could do that there: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx – Dmitry Ledentsov Oct 25 '12 at 10:35
  • I see, putting the logger in a separate library also could be useful for using it throughout the solution with different projects, I didn't think of that! – キキジキ Oct 25 '12 at 13:48