Skip to content

Examples TypicalUsage

Eduard Mishkurov edited this page May 14, 2026 · 5 revisions

TypicalUsage examples

This directory shows the three common ways to use logme in an application. The examples are intentionally separate because these scenarios should not be mixed in one startup path.

1. Out-of-box usage

OutOfBox.cpp uses logme without explicit configuration. It writes to the default channel and is suitable for small tools, quick diagnostics, and first integration tests.

LogmeI("application started with default logme configuration");
LogmeW("request %d took longer than expected", requestId);

Run:

./TypicalUsageOutOfBox

2. Code configuration

CodeConfiguration.cpp configures logging from C++ code. It creates an application channel, attaches console and file backends, sets the filter level, and then logs through that channel.

Use this style when the logging topology is part of the application logic or when the application does not use JSON configuration.

Run:

./TypicalUsageCodeConfiguration

3. Configuration file

ConfigFile.cpp loads logging setup from typical-usage.json or from a path passed on the command line. It does not create channels or backends in code after loading the file.

Use this style for production applications where channels, backends, levels, and runtime behavior should be adjustable without recompiling the application.

Run:

./TypicalUsageConfigFile typical-usage.json

Loading a JSON configuration file requires logme to be built with JsonCpp support.

Build

cmake -S . -B build -DLOGME_BUILD_EXAMPLES=ON
cmake --build build

The binaries are placed in the examples output directory used by the main build.

Console output

Console output was not captured: CMake configure failed.

Command: C:\Program Files\CMake\bin\cmake.EXE -S C:\Users\TRITON~1\AppData\Local\Temp\logme_examples_zioj5xao\logme -B C:\Users\TRITON~1\AppData\Local\Temp\logme_examples_build_y4c5x8k3\build -DLOGME_BUILD_EXAMPLES=ON -DLOGME_BUILD_TESTS=OFF -DLOGME_BUILD_TOOLS=OFF -DLOGME_BUILD_DYNAMIC=OFF -DLOGME_BUILD_STATIC=ON -DLOGME_ENABLE_INSTALL=OFF
Exit code: 1

-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.26200.
-- The C compiler identification is MSVC 19.44.35223.0
-- The CXX compiler identification is MSVC 19.44.35223.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test LOGME_HAS_STD_FORMAT
-- Performing Test LOGME_HAS_STD_FORMAT - Success
-- LOGME_FMT_FORMAT = AUTO
-- LOGME_STD_FORMAT = AUTO
-- Logme format backend: std::vformat
-- USE_JSONCPP = AUTO
-- Logme JsonCpp support: disabled
-- Configuring done (13.6s)
-- Generating done (0.3s)
CMake Error at logme/CMakeLists.txt:21 (target_link_libraries):
  Target "logme" links to:
... output truncated, 12 lines omitted ...

Source code

Sources

Repository folder: examples/TypicalUsage

Files:

CodeConfiguration.cpp

Open in repository

#include <memory>

#include <Logme/Backend/ConsoleBackend.h>
#include <Logme/Backend/FileBackend.h>
#include <Logme/Logme.h>

static Logme::ChannelPtr CreateApplicationChannel()
{
  Logme::ID id{"app"};

  auto ch = Logme::Instance->CreateChannel(id, Logme::OutputFlags(), Logme::LEVEL_DEBUG);

  auto console = std::make_shared<Logme::ConsoleBackend>(ch);
  console->SetAsync(true);
  ch->AddBackend(console);

  auto file = std::make_shared<Logme::FileBackend>(ch);
  file->SetAppend(true);
  file->SetMaxSize(1024ULL * 1024);
  
  if (file->CreateLog("typical-usage.log") == false)
  {
    LogmeE("cannot create log file 'typical-usage.log'");
    exit(1);
  }

  ch->AddBackend(file);

  return ch;
}

static void ProcessRequest(
  const Logme::ChannelPtr& ch
  , int requestId
)
{
  Logme::SID network = Logme::SID::Build("network");
  Logme::SID storage = Logme::SID::Build("storage");

  LogmeI(ch, network, "request %d accepted", requestId);
  LogmeD(ch, storage, "request %d loaded cached profile", requestId);
  LogmeW(ch, network, "request %d took longer than expected", requestId);
}

int main()
{
  auto app = CreateApplicationChannel();

  LogmeI(app, "application started with code configuration");

  ProcessRequest(app, 1001);
  LogmeE(app, "example error record");

  return 0;
}

ConfigFile.cpp

Open in repository

#include <string>

#include <Logme/Logme.h>

static const char* TYPICAL_USAGE_CONFIGURATION = R"json(
{
  "channels": [
    {
      "name": "",
      "level": "debug",
      "backends": [
        {
          "type": "ConsoleBackend",
          "async": false
        },
        {
          "type": "FileBackend",
          "append": true,
          "max-size": "1Mb",
          "file": "typical-usage-config.log"
        }
      ]
    }
  ]
}
)json";

static bool LoadLogmeConfiguration()
{
  std::string error;
  if (Logme::Instance->LoadConfiguration(TYPICAL_USAGE_CONFIGURATION, std::string(), &error))
  {
    return true;
  }

  LogmeE("cannot load logme configuration: %s", error.c_str());
  return false;
}

static void ProcessRequest(int requestId)
{
  LogmeI("request %d accepted", requestId);
  LogmeW("request %d took longer than expected", requestId);
}

int main()
{
  if (!LoadLogmeConfiguration())
  {
    return 1;
  }

  LogmeI("application started with embedded configuration");

  ProcessRequest(1001);
  LogmeE("example error record");

  return 0;
}

OutOfBox.cpp

Open in repository

#include <Logme/Logme.h>

static void ProcessRequest(int requestId)
{
  LogmeI("request %d accepted", requestId);
  LogmeW("request %d took longer than expected", requestId);
}

int main()
{
  LogmeI("application started with default logme configuration");

  ProcessRequest(1001);
  LogmeE("example error record");

  return 0;
}

Previous: ThreadContext

Clone this wiki locally