Last Updated: October 18, 2020
·
20.06K
· dmcrodrigues

a simplified logging system using macros

I developed a logging system built based on macros, <a href="https://github.com/dmcrodrigues/macro-logger">macro-logger</a>. It can be used with C/C++ code but also with Objective-C.

Levels

There are three different logging levels. It's possible also disable all logs.

  • [0] NO_LOGS
  • [1] ERROR
  • [2] INFO
  • [3] DEBUG

The log level can be specified on compilation time using the macro

LOG_LEVEL=level_number

If not specified is used the debug level.

Technical aspects

Each log below the log level defined will not be included on binary. This is relevant because allows save space and resources.

Logs are printed using fprintf function in conjunction with standard error stream (stderr) in C/C++ and Objective-C.

The log functions can handle arguments like printf or just a message. The message can be a C/C++ string or an Objective-C string.

int x = 5;
LOG_INFO("x * x = %d", x * x);
LOG_DEBUG(@"Objective-C also supported");

The log functions can receive Objective-C objects as arguments.

NSString *baseStation = @"Houston";
LOG_ERROR(@"%@ we have a problem!", baseStation);

Usage

The only file needed to use the macrologger is the header file, macrologger.h.

Functions available

LOG_DEBUG(message, arguments);

LOG_INFO(message, arguments);

LOG_ERROR(message, arguments);

LOG_IF_ERROR(condition, message, arguments);

Examples

// C/C++ strings
LOG_DEBUG("Maybe i can touch this button...");

// Objective-C strings
LOG_INFO("@Pressure is dropping...");

// printing Objective-C objects    
NSString *baseStation = @"Houston";
LOG_ERROR(@"%@ we have a problem!", baseStation);

int going_down = 1;
LOG_IF_ERROR(going_down, "i'm going down... if only i had used macro-logger...");

Sample output

2012-09-04 01:05:16 | DEBUG   | sample.m        | main:29 | Maybe i can touch this button...
2012-09-04 01:05:16 | INFO    | sample.m        | main:31 | Pressure is dropping...
2012-09-04 01:05:16 | ERROR   | sample.m        | main:34 | Houston we have a problem!
2012-09-04 01:05:16 | ERROR   | sample.m        | main:37 | i'm going down... if only i had used macro-logger...

Project

You can check this project on Github.

https://github.com/dmcrodrigues/macro-logger

1 Response
Add your response

Hi David Rodrigues,

Thanks for this simplified solution for logging in multiple programming languages, recently I tried to use it with iOS version 9.3.5 but it's crashing because of "clockgettime(CLOCKREALTIME, &curtime);", for iOS version greater than 10 it's working like a charm, could you please provide alternative for getting current time in this code with iOS version 9.3.5(Objective - C).

Thanks in advance.

over 1 year ago ·