Last Updated: October 18, 2020
· dmcrodrigues

a simplified logging system using macros

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


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


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);


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);


// 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...


You can check this project on Github.

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 ·