KjellKod / g3log

G3log is an asynchronous, "crash safe", logger that is easy to use with default logging sinks or you can add your own. G3log is made with plain C++14 (C++11 support up to release 1.3.2) with no external libraries (except gtest used for unit tests). G3log is made to be cross-platform, currently running on OSX, Windows and several Linux distros. See Readme below for details of usage.
http://github.com/KjellKod/g3log
The Unlicense
908 stars 271 forks source link

[How to use g3log in multiple classes or intra class] #494

Closed alex0598 closed 1 year ago

alex0598 commented 1 year ago

Uncertain what's going on? Do you need advice or help troubleshooting?

I have the following class. Unfortunately after I have called InitLogging() the log file is created and "InitCalled" is logged, but nothing is logged when I call Log(). The class is as an export class of a .DLL.

std::unique_ptr<g3::LogWorker> LogWorker = {g3::LogWorker::createLogWorker()};
std::unique_ptr<g3::FileSinkHandle> FileSink;

ExampleClass::ExampleClass(const string InstanceName)
{
  LOGF(DEBUG, "ExcampleClass instance %s created", InstanceName.c_str ());
}

ExampleClass::~ExampleClass()
{
  // not really needed, just to show the intention of usage, would like to shutdown filesink here
  g3::internal::shutDownLoggingForActiveOnly(LogWorker.get());
}

ExampleClass::InitLogging()
{
  LogWorker->addSink(std2::make_unique<g3::FileSink>("ExampleClass", "log/", InstanceName), g3::FileSink::fileWrite);
  g3::initializeLogging(LogWorker.get());

  LOG(INFO) << "Init called";
}

ExampleClass::Log()
{
  LOG(INFO) << "Test";
}
KjellKod commented 1 year ago

Maybe you can provide a suggestion on how you easier would have understood how to properly use g3log?