Skip to content

Commit

Permalink
Adjust configuration and debug section. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonystone authored Jul 6, 2018
1 parent 2a4a2c3 commit bb03499
Showing 1 changed file with 59 additions and 57 deletions.
116 changes: 59 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,62 @@ You can of course also pass a tag by using the CLog version of the call.
## Configuration
TraceLog is configured via variables that are either set in the runtime environment or passed on startup of the application.
TraceLog can be configured via the environment either manually using `export` or via Xcode.
For TraceLog to read the environment on startup, you must call its configure method at the beginning of your application.
```swift
TraceLog.configure()
```

### Log Writers

TraceLog can be configured with multiple custom log writers who do the job of outputting the log statements to the desired location. By default, it configures itself with a `ConsoleWriter`
which outputs to `stdout`. You can change the writers at any time and chain multiple of them to output to different locations such as to a remote logging servers, syslog, etc.

Writers must implement the `Writer` protocol. To install them, simply call configure with an array of `Writers`.

```swift
let networkWriter = NetworkWriter(url: URL("http://mydomain.com/log"))

TraceLog.configure(writers: [ConsoleWriter(), networkWriter])
```

Since writers are instantiated before the call, you are free to initialize them with whatever makes sense for the writer type to be created. For instance in the case of the network writer the URL of
the logging endpoint.

### Concurrency Modes

TraceLog can be configured to run in 3 main concurrency modes, `.direct`, `.sync`, or `.async`. These modes determine how TraceLog will invoke each writer as it logs your logging statements.

* `.direct` - Direct, as the name implies, will directly call the writer from the calling thread with no indirection. It will block until the writer(s) in this mode have completed the write to the endpoint. Useful for scripting applications and other applications where it is required for the call not to return until the message is printed.

* `.sync` - Synchronous blocking mode is similar to direct in that it blocks but this mode also uses a queue for all writes. The benefits of that is that all threads writing to the log will be serialized through before calling the writer (one call to the writer at a time).

* `.async` - Asynchronous non-blocking mode. A general mode used for most application which queues all messages before being evaluated or logged. This ensures minimal delays in application execution due to logging.

Modes can be configured globally for all writers including the default writer by simply setting the mode in the configuration step as in the example below.
```swift
TraceLog.configure(mode: .direct)
```
This will set TraceLog's default writer to `.direct` mode.

You can also configure each writer individually by wrapping the writer in a mode as in the example below.

```swift
TraceLog.configure(writers: [.direct(ConsoleWriter()), .async(FileWriter())])
```
This will set the ConsoleWriter to write directly (synchronously) when you log but will queue the FileWriter calls to write in the background asynchronously.

You can also set all writers to the same mode by setting the default mode and passing the writer as normal as in the following statement.

```swift
TraceLog.configure(mode: .direct, writers: [ConsoleWriter(), FileWriter()])
```
This sets both the ConsoleWriter and the FileWriter to `.direct` mode.

## Setting up your debug environment

TraceLog's current logging output is controlled by variables that are either set in the runtime environment or passed on startup of the application (in the `TraceLog.Configure(environment:)` method).

These variables consist of the following:

Expand All @@ -195,7 +250,7 @@ These variables consist of the following:
LOG_TAG_<TAGNAME>=<LEVEL>
LOG_PREFIX_<TAGPREFIX>=<LEVEL>
```
Log output can be configured globally using the `LOG_ALL` variable, by TAG name
Log output can be set globally using the `LOG_ALL` variable, by TAG name
using the `LOG_TAG_<TAGNAME>` variable pattern, and/or by a TAG prefix by using
the `LOG_PREFIX_<TAGPREFIX>` variable pattern.

Expand Down Expand Up @@ -234,20 +289,13 @@ For instance, in the example above, if we decided for one tag in the Networking
```
This outputs the same as the previous example except for the `NTSerializer` tag which is set to `TRACE4` instead of using the less specific `TRACE1` setting in `LOG_PREFIX`.

#### Configuration (Environment)

TraceLog can be configured via the environment either manually using `export` or via Xcode.
For TraceLog to read the environment on startup, you must call its configure method at the beginning of your application.

```swift
TraceLog.configure()
```
#### Environment Setup (Xcode)

To set up the environment using Xcode, select "Edit Scheme" from the "Set the active scheme" menu at the top left. That brings up the menu below.

<img src=Docs/Xcode-environment-setup-screenshot.png width=597 height=361 />

#### Configuration (In code)
#### Environment Setup (Statically in code)

TraceLog.configure() accepts an optional parameter called environment which you can pass the environment. This allows you to set the configuration options at startup time (note: this ignores any variables passed in the environment).

Expand All @@ -261,52 +309,6 @@ Here is an example of setting the configuration via `TraceLog.configure()`.
Note: Although the environment is typically set once at the beginning of the application, `TraceLog.configure`
can be called anywhere in your code as often as required to reconfigured the logging levels.

## Configuring Log Writers

TraceLog can be configured with multiple custom log writers who do the job of outputting the log statements to the desired location. By default, it configures itself with a `ConsoleWriter`
which outputs to `stdout`. You can change the writers at any time and chain multiple of them to output to different locations such as to a remote logging servers, syslog, etc.

Writers must implement the `Writer` protocol. To install them, simply call configure with an array of `Writers`.

```swift
let networkWriter = NetworkWriter(url: URL("http://mydomain.com/log"))

TraceLog.configure(writers: [ConsoleWriter(), networkWriter])
```

Since writers are instantiated before the call, you are free to initialize them with whatever makes sense for the writer type to be created. For instance in the case of the network writer the URL of
the logging endpoint.

## Concurrency Modes

TraceLog can be configured to run in 3 main concurrency modes, `.direct`, `.sync`, or `.async`. These modes determine how TraceLog will invoke each writer as it logs your logging statements.

* `.direct` - Direct, as the name implies, will directly call the writer from the calling thread with no indirection. It will block until the writer(s) in this mode have completed the write to the endpoint. Useful for scripting applications and other applications where it is required for the call not to return until the message is printed.

* `.sync` - Synchronous blocking mode is simaler to direct in that it blocks but this mode also uses a queue for all writes. The benifits of that is that all threads writing to the log will be serialized through before calling the writer (one call to the writer at a time).

* `.async` - Asynchronous non-blocking mode. A general mode used for most application which queues all messages before being evaluated or logged. This ensures minimal delays in application execution due to logging.

Modes can be configured globally for all writers including the default writer by simply setting the mode in the configuration step as in the example below.
```swift
TraceLog.configure(mode: .direct)
```
This will set TraceLog's default writer to `.direct` mode.

You can also configure each writer individually by wrapping the writer in a mode as in the example below.

```swift
TraceLog.configure(writers: [.direct(ConsoleWriter()), .async(FileWriter())])
```
This will set the ConsoleWriter to write directly (synchronously) when you log but will queue the FileWriter calls to write in the background asynchronously.

You can also set all writers to the same mode by setting the default mode and passing the writer as normal as in the following statement.

```swift
TraceLog.configure(mode: .direct, writers: [ConsoleWriter(), FileWriter()])
```
This sets both the ConsoleWriter and the FileWriter to `.direct` mode.

## Runtime Overhead

The **Swift** implementation was designed to take advantage of swift compiler optimizations and will incur **no overhead** when compiled with optimization on (`-O`) and `TRACELOG_DISABLED` is defined.
Expand Down

0 comments on commit bb03499

Please sign in to comment.