Inherits from NSObject
Declared in ECLogChannel.h

Overview

This isn’t a class you typically interact with directly. The methods in the class are generally used by the logging system itself.

About

A channel is a place to send log messages to.

You get to define as many channels as you like, organised in whatever way makes sense.

This allows you to turn most logging off most of the time, and just enable the bits that you happen to be interested in right now.

Log and Debug

Channels come in two flavours, log and debug.

Log channels are always available.

Debug channels are only available in debug targets (ones where EC_DEBUG is defined). In non-debug builds, debug channels don’t exist. Anything inside ECDebug() messages won’t get compiled or executed at all. This allows you to put potentially time-consuming logging code into these calls, safe in the knowledge that it won’t affect the final performance of your app.

Defining Channels

Channels must be defined before use. This is done once for each channel, in a .m file. For example:

ECDefineLogChannel(MyLogChannel);
ECDefineDebugChannel(MyDebugChannel);

If you want to share a channel between multiple files, you can also declare it in a .h file:

ECDeclareLogChannel(MyLogChannel);
ECDeclareDebugChannel(MyDebugChannel);

Usage

To use a channel, you send stuff to it with ECLog or ECDebug:

 ECLog(MyLogChannel, @"this is a test %@ %d", @"blah", 123);

 ECDebug(MyDebugChannel, @"doodah");

As mentioned above, ECLog statements will always be compiled, so you need to use them with channels defined by ECDefineLogChannel.

You can use ECDebug with channels that were defined with either ECDefineLogChannel or ECDefineDebugChannel. Any ECDebug statements will be compiled in debug builds, but not in release builds.

Logging Objects

As well as the more usual text logging, you can also send arbitrary objects to a log channel.

 NSImage* image = [NSImage imageNamed:@"blah.png"];
 ECDebug(MyLogChannel, image);

What the log handlers do with objects that you log is up to them. The default behaviour for simple text-based log handlers is just to call [object description] on the object and log that.

However, custom log handlers can do anything that they want. For example, you might have a log handler which takes any images that you log and displays them in a scrolling window.

Tasks

Properties

context

Context flags that apply to any message sent to the channel.

@property (assign, nonatomic) ECLogContextFlags context

Declared In

ECLogChannel.h

enabled

Is the channel enabled?

@property (assign, nonatomic) BOOL enabled

Declared In

ECLogChannel.h

handlers

Handlers that the channel’s output will be sent to.

@property (strong, nonatomic) NSMutableSet *handlers

Declared In

ECLogChannel.h

level

Log level of the channel.

@property (strong, nonatomic) NSNumber *level

Declared In

ECLogChannel.h

name

Name of the channel.

@property (strong, nonatomic) NSString *name

Declared In

ECLogChannel.h

setup

Has the channel been set up?

@property (assign, nonatomic) BOOL setup

Declared In

ECLogChannel.h

Class Methods

cleanName:

Return a cleaned up version of a raw channel name.

+ (NSString *)cleanName:(const char *)name

Parameters

name

Raw c-style name string.

Return Value

Cleaned up name.

Discussion

Removes “Channel” from the end, and splits up MixedCaps words by inserting spaces.

Declared In

ECLogChannel.h

Instance Methods

caseInsensitiveCompare:

Comparison function to sort channels by name.

- (NSComparisonResult)caseInsensitiveCompare:(ECLogChannel *)other

Parameters

other

The other channel to compare against.

Return Value

Comparison result.

Declared In

ECLogChannel.h

disable

Disable the channel. Any output sent to the channel will be ignored whilst it is disabled.

- (void)disable

Declared In

ECLogChannel.h

disableHandler:

Disable a handler for this channel.

- (void)disableHandler:(ECLogHandler *)handler

Parameters

handler

The handler to disable. Any output sent to this channel will no longer get passed to the disabled handler.

Declared In

ECLogChannel.h

enable

Enable the channel.

- (void)enable

Declared In

ECLogChannel.h

enableHandler:

Enable a handler for this channel.

- (void)enableHandler:(ECLogHandler *)handler

Parameters

handler

The handler to enable. Any output sent to this channel will get passed to the enabled handler.

Declared In

ECLogChannel.h

fileFromContext:

Return a formatted string giving the file name and line number from a context structure.

- (NSString *)fileFromContext:(ECLogContext *)context

Parameters

context

The context information

Return Value

String with the file name and line number.

Declared In

ECLogChannel.h

initWithName:

Set up a channel with a given name.

- (id)initWithName:(NSString *)name

Parameters

name

The name of the channel.

Declared In

ECLogChannel.h

isHandlerEnabled:

Will this channel pass output to a given handler?

- (BOOL)isHandlerEnabled:(ECLogHandler *)handler

Parameters

handler

The handler to check for.

Return Value

YES if the handler is enabled for this channel.

Declared In

ECLogChannel.h

selectFlagWithIndex:

UI helper – respond to a context flag being selected.

- (void)selectFlagWithIndex:(NSUInteger)index

Parameters

index

The index of the flag.

Discussion

We respond by toggling the flag on/off.

Declared In

ECLogChannel.h

selectHandlerWithIndex:

UI helper – respond to a handler being selected.

- (void)selectHandlerWithIndex:(NSUInteger)index

Parameters

index

The index of the handler.

Discussion

We respond by enabling/disabling the handler for this channel.

Declared In

ECLogChannel.h

showContext:

Should any log output for this channel include context information for the given context flags?

- (BOOL)showContext:(ECLogContextFlags)flags

Parameters

flags

Flags we’re checking.

Return Value

YES if context information should be shown.

Declared In

ECLogChannel.h

stringFromContext:

Return a formatted string describing a context structure, based on our context flags.

- (NSString *)stringFromContext:(ECLogContext *)context

Parameters

context

The context.

Return Value

String describing the context.

Declared In

ECLogChannel.h

tickFlagWithIndex:

UI helper – should we tick a menu item for a given flag index?

- (BOOL)tickFlagWithIndex:(NSUInteger)index

Parameters

index

The index of the flag.

Return Value

YES if it should be ticked.

Declared In

ECLogChannel.h

tickHandlerWithIndex:

UI helper – should we tick a menu item for a given handler index?

- (BOOL)tickHandlerWithIndex:(NSUInteger)index

Parameters

index

The index of the handler.

Return Value

YES if it should be ticked.

Declared In

ECLogChannel.h