Skip to content

SIALogger Objective C_v110

Ivlev Alexander edited this page Jun 7, 2016 · 5 revisions

SIALogger Objective-C

library for simplify log and assertion

Contents

  1. Config
  2. Logs
  3. Assertion
  4. Check
  5. Contracts requires
  6. Not implemented
  7. Log format
  8. Extensions
  9. Colorful

Description

Config

For changes settings get SIALogConfig class.
List settings:

  • Max log level - Writing only if log level less the variable.
  • Outputs - Classes implementation protocol SIALogOutputProtocol for show log messages. For example: Console, File.
  • Format time - String by generate current time. for more information see: NSDateFormatter

Logs

Supported five log level:

  • Fatal
  • Error
  • Warning
  • Info
  • Trace

Fatal log always abort a program.

For log use macroses SIALog{LEVEL}(MSG, ...). For example:
SIALogTrace(@"Trace message with value:%@", value);

If you needs log by condition use macroses SIALog{LEVEL}If(CONDITION, MSG, ...). For example:
SIALogWarningIf(nil == model, @"Model is nil");

If you needs log by condition and return from function (or other action) use macroses SIALog{LEVEL}If(CONDITION, MSG, ...). For example:
if (SIALogInfoIf(nil == model, @"Model is nil")) { return; }

Assertion

Assertion abort program only debug mode, but always logs. For assert use Macros SIALogAssertMsg(CONDITION, MSG, ...) or SIALogAssert(CONDITION). For example:
SIALogAssert(nil != model);

Check

Check always log and abort program. For check use macros SIALogCheck(CONDITION) For example:
SIALogCheck(nil == model)

Contracts requires

It's not real contracts because these works only runtime. These contracts not support code analysis. It's specifics assertion. Macros List:

  • SIARequires(CONDITION)
  • SIARequiresNotNil(VALUE) For example: SIARequiresNotNil(model)
  • SIARequiresType(VALUE, TYPE) For example: SIARequiresType(model, Model)
  • SIARequiresProtocol(VALUE, PROTOCOL) For example: SIARequiresProtocol(model, ModelProtocol)
  • SIARequiresSelector(VALUE, SELECTOR) For example: SIARequiresSelector(model, @selector(method))
  • SIARequiresArrayInterval(BEGIN, VALUE, END) For example: SIARequiresArrayInterval(0, model.value, 100)

Not implemented

Not implemented abort program only debug mode, but always logs. Use macros: SIANotImplemented()

Log format

Supported seven tokens:

  • %t - log time. see more: Config/Format time.
  • %L - log level, with normal name. For example: 'Fatal', 'Error', 'Warning', 'Info', 'Trace'.
  • %3 - log level, with short name. For example: 'FTL', 'ERR', 'WRN', 'INF', 'TRC'.
  • %U - log level, with upper case normal name. For example: 'FATAL', 'ERROR', 'WARNING', 'INFO', 'TRACE'.
  • %f - log file.
  • %l - log line.
  • %m - log message.

Log format supported into SIALogConsoleOutput and SIALogDocumentsFileOutput. Your can init there classes with format string. For example:
[[SIALogConsoleOutput alloc] initWithFormat:@"%t [%U] {%f:%l}: %m"]

Also your can using SIALogFormatter class. For example:

SIALogMessage* msg = ...;
SIALogFormatter* formatter = [[SIALogFormatter alloc] initWithFormat:@"%t [%L] {%f:%l}: %m"];
NSString* fullLogText = [formatter toString:msg];

Extensions

Your can wrote SIALogOutputProtocol implementation. For example, your want to wrote logs into BD. Your can wrote:

#import "SIALogOutputProtocol.h"

@interface YourLogBDOutput : NSObject <SIALogOutputProtocol>
@end

@implementation YourLogBDOutput
- (void)log:(SIALogMessage*)msg {
  writeToBD (...);
}
@end

...
//initialized code
[SIALogConfig setOutputs:@[ [YourLogBDOutput new] ]];

###Colorful Your can plug color module. See more Install paragraph: Colorful

For usage your must rewrite outputs on SIALogColoredConsoleOutput. For example:

[SIALogConfig setOutputs:@[ [SIALogColoredConsoleOutput new] ]];

The output supported log format (see above), but contains additional token: %c. This token beginning/ending paint text, use colors for current message level. For example:

[SIALogConfig setOutputs:@[ [[SIALogColoredConsoleOutput alloc] initWithFormat:@"%t %c[%3]%c {%f:%l}: %m"] ]];

SIALogTrace(@"message 1");
SIALogInfo(@"message 2");
SIALogWarning(@"message 3");
SIALogError(@"message 4");
SIALogFatal(@"message 5");

And your can see similar log:
Log View

As well your can change level color. For example:

SIALogColoredConsoleOutput* output = [[SIALogColoredConsoleOutput alloc] initWithFormat:@"%t %c[%U] {%f:%l}:%c %m"];

[output setForegroundColor:[[SIALogColor alloc] initByRed:255 Green:0 Blue:0] OnLevel:[SIALogLevels Error]];
[output setBackgroundColor:[[SIALogColor alloc] initByRed:0 Green:255 Blue:0] OnLevel:[SIALogLevels Error]];

[SIALogConfig setOutputs:@[ output ]];
You can’t perform that action at this time.