# Debug

## DebugThread object

### thread.log(options)

```typescript
import { DebugThread } from '@empathize/framework';

function exampleFunction()
{
    const debugThread = new DebugThread('exampleFunction', 'Initialized');
    
    // do some stuff
    
    debugThread.log({
        message: [
            'list',
            'of',
            'lines'
        ]
    });
}
```

## DebugOptions

| Name      | Type                          | Description                                                                                                  |
| --------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------ |
| message   | string \| string\[] \| object | Message to log. If string - single line; list of strings - multiple lines; object - in format `[key]: value` |
| function? | string                        | Name of function log should be written as                                                                    |
| thread?   | number                        | Id of a thread (random number)                                                                               |

## Debug class

### Debug.log(options)

```typescript
import { Debug } from '@empathize/framework';

function exampleFunction(a, b)
{
    Debug.log({
        function: 'exampleFunction',
        message: {
            'a value': a,
            'b value': b
        }
    });
}
```

### Debug.merge(records)

Merges log records with currently stored. Windows in neutralino work as separate processes, so `Debug` class in the main window will be different from `Debug` class in the about window. So, to have a complete log from all the windows, you should send them by, for example, `IPC.write()` method to your main window, and then merge it by this method

![How Debug.merge() should be used](/files/6ajsxA1cVZveTuoRm6Uo)

#### About window

```typescript
import { Debug, IPC } from '@empathize/framework';

declare const Neutralino;

// This event will be called before window closing
// Be careful because this event works only when
// exitProcessOnClose = false
Neutralino.events.on('windowClose', async () => {
    // Wait before it will be stored
    await IPC.write({
        type: 'log',
        log: Debug.getRecords()
    });
    
    // And then we need to manually close this process
    Neutralino.app.exit();
});
```

#### Main window

```typescript
import { Debug, IPC } from '@empathize/framework';

IPC.read().then((records) => {
    records.forEach((record) => {
        if (record.data.type === 'log')
        {
            // And don't forget to call .pop() method
            // to remove this record from the IPC.read() pool
            Debug.merge(record.pop().data.log);
        }
    });
});
```

### Debug.getRecords()

System class that used primarily with `Debug.merge()`

### Debug.get()

Returns list of strings

```typescript
import { Debug, fs, path, dir } from '@empathize/framework';

const pathToFile = path.join(dir.app, 'logs', `${Debug.startedAt}.log`);
const debugLog = Debug.get().join('\r\n');

fs.write(pathToFile, debugLog);
```

### Debug.handler(callback)

```typescript
import { Debug } from '@empathize/framework';

Debug.handler((record) => {
    console.log(`Made new log record:\r\n${record.log.join('\r\n')}`);
});
```

### Debug.loggable(func)

This method will return a new function that will log some user-specified strings at the start and at the end of its work

![How Debug.loggable() works](/files/qnpNenY7Vqk8RZzvCCGj)

#### Sync functions

```typescript
import { Debug } from '@empathize/framework';

// sum(1, 2) -> 3
let sum = (a: number, b: number): number => a + b;

// Replace this sum function with a new one, generated by Debug.loggable(()
// This new function will generate a log at the start and finish of its work
// Also <number> can be replaced with <ReturnType<typeof sum>>
// which is the same, but generates automatically by the TypeScript
sum = Debug.loggable<number>(sum, {
    // Function name to log
    function: 'sum',
    
    // DebugOptions to log at start
    start: (thread, ...args) => `Calculating ${args[0]} + ${args[1]}`,
    
    // DebugOptions to log at finish
    finish: (thread, output) => `Calculated ${output}`
});

console.log(`1 + 2 = ${sum(1, 2)}`);
```

#### Async functions

```typescript
import { Debug } from '@empathize/framework';

// await sum(1, 2) -> 3
let sum = (a: number, b: number): Promise<number> => {
    return new Promise((resolve) => {
        setTimeout(() => resolve(a + b), 3000);
    });
};

// Here I use <ReturnType<typeof sum>>, but it also
// acceptable to use <Promise<number>>
sum = Debug.loggable<ReturnType<typeof sum>>(sum, {
    // Function name to log
    function: 'sum',
    
    // DebugOptions to log at start
    start: (thread, ...args) => `Calculating ${args[0]} + ${args[1]}`,
    
    // DebugOptions to log at finish
    finish: (thread, output) => `Calculated ${output}`
});

sum(1, 2).then((result) => console.log(`1 + 2 = ${result}`));
```

This function is highly useful to generate log generation in your already existing code without modifying it. For example:

```typescript
import { Debug, Configs } from '@empathize/framework';

// We're rewriting Configs.get() function and then
// it will automatically generate log output
Configs.get = Debug.loggable<ReturnType<typeof Configs.get>>(Configs.get, {
    function: 'Configs.get',
    context: Configs,
    start: (thread, ...args) => `Getting ${args[0]} config value`,
    finish: (thread, output) => `Resolved value: ${output}`
});
```

Property context means an object or class at which name this function should be called. Because we're making a copy of the original function - this copy will have no access to `this`, and to manually give this function `this` value - you should specify its *context* - parent class or object this function belongs to&#x20;


---

# 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://krypt0nn.gitbook.io/empathize/api/meta-classes/debug.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.
