# runtime\_logger

## runtime\_logger サンプル

### 概要

Diarkis Module では様々なタイプのロガーがプリセットで用意されています。 本サンプルではプリセットロガーとカスタムロガーの実装と設定方法について紹介します。

### ローカル環境でサーバーを起動する

サンプルで使用するサーバーを起動するためのチュートリアルを実施して、ローカル環境で Diarkis サーバーを起動します。

[Diarkis サーバをローカルで起動する](/getting-started/tutorial/setup-local-server.md)

### サンプルの引数

サンプルの起動時には以下の３つのパラメータを指定してください。

| 引数         | 説明                            |
| ---------- | ----------------------------- |
| serverAddr | Diarkis サーバのエンドポイントを指定してください  |
| UID        | 接続するユーザーの ID を任意の文字列で指定してください |
| clientKey  | クライアントキーを任意の文字列で指定してください      |

#### 起動例：

`runtime_logger.exe 192.168.1.123:7000 1111 AAAA`

### 起動方法

本サンプルは単体で実行することができ、起動すると指定された Diarkis サーバに接続し切断します。

### サンプルコード説明

カスタムアロケータの実装方法と設定方法に分けて紹介します。 また、本サンプルでは基礎的な Diarkis サーバへの接続方法等は説明していません。必要に応じて room\_broadcast 等の基礎的なサンプルを参照してください。

#### プリセットロガー

Diarkis Module では様々な種類のプリセットロガーを提供しています。 本サンプルではそれらのロガーを切り替えて実行することで、それぞれのロガーがどのような動きをするか紹介しています。\
以下が DEBUG\_OUT ロガーのサンプル実装例となります。

```
// Logging with DEBUG_OUT
// The runtime will use DebugLoggerBackend if you specify DEBUG_OUT.
// On Windows, the log messages will be output to the Debug Output window.
// On other platforms, the log messages will be output to the console.
DiarkisUtils::Print("************************ Logging with DEBUG_OUT ****************************");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
{
    DiarkisInterface::DiarkisInit(uid, LogOutType::DEBUG_OUT);

    if (RunDiarkis(host, uid, clientKey) != 0)
    {
        DiarkisInterface::DiarkisDestroy();
        return 1;
    }

    DiarkisInterface::DiarkisDestroy();
}
```

#### カスタムロガーの実装方法

カスタムロガーの実装方法についてはこちらのページを参照してください。本サンプルではこの方法に則ってカスタムロガーを実装しています。\
以下がカスタムロガーのサンプル実装となります。

```
/**
* This is a sample implementation of the custom logger.
*/
class AppCustomLoggerBackend : public ILoggerBackend
{
public:
    ...
```

Diarkis が出力するログにアプリ側で任意の文字列を追加して出力しています。

```
Result Log(const Diarkis::StdString& message, bool includeNewLine) override
{
    // Custom log output implementation
    // A log message is passed into this method, so you can implement your app-specific log output here.
    // This method could be called from multiple threads, so make sure your implementation is thread-safe.
    DiarkisUtils::Print("Add log to the app logger: %s", message.c_str());

    return Diarkis::Results::SUCCESS;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.diarkis.io/diarkis-client/samples/cpp/runtime_logger.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
