Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
a set of macros for loggings to be used in C and Objective-C programming. since these macros has any dependencies, you can use this with any logging facilities you have to use.
// =================================================================================
// Copyright (c) 2012 Sheutettz Nogami. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
// =================================================================================
#ifndef _NANA_LOG_H_
#define _NANA_LOG_H_
#define NANA_LOG_ASSERT 0x80u
#define NANA_LOG_ERROR 0x10u
#define NANA_LOG_WARN 0x08u
#define NANA_LOG_INFO 0x04u
#define NANA_LOG_DEBUG 0x02u
#define NANA_LOG_TRACE 0x01u
#define NANA_LOG_ALL 0xFFu
#define NANA_LOG_NONE 0x00u
#ifndef NANA_LOG
#ifdef NANA_LOG_DEFAULT
#define NANA_LOG NANA_LOG_DEFAULT
#else
#if defined(DEBUG) || defined(_DEBUG)
#define NANA_LOG NANA_LOG_ALL
#else
#define NANA_LOG NANA_LOG_NONE
#endif
#endif
#endif
#undef _NANA_LOG_MASK
#define _NANA_LOG_MASK (NANA_LOG+0)
#undef _NANA_LOG
#if (_NANA_LOG_MASK & NANA_LOG_ALL)
#define _NANA_LOG
#endif
#undef _NANA_LOG_ASSERT
#if (_NANA_LOG_MASK & NANA_LOG_ASSERT)
#define _NANA_LOG_ASSERT
#endif
#undef _NANA_LOG_ERROR
#if (_NANA_LOG_MASK & NANA_LOG_ERROR)
#define _NANA_LOG_ERROR
#endif
#undef _NANA_LOG_WARN
#if (_NANA_LOG_MASK & NANA_LOG_WARN)
#define _NANA_LOG_WARN
#endif
#undef _NANA_LOG_INFO
#if (_NANA_LOG_MASK & NANA_LOG_INFO)
#define _NANA_LOG_INFO
#endif
#undef _NANA_LOG_DEBUG
#if (_NANA_LOG_MASK & NANA_LOG_DEBUG)
#define _NANA_LOG_DEBUG
#endif
#undef _NANA_LOG_TRACE
#if (_NANA_LOG_MASK & NANA_LOG_TRACE)
#define _NANA_LOG_TRACE
#endif
#undef _nana_log_level_to_label
#undef nana_log
#ifdef _NANA_LOG
#define _nana_log_level_to_label(level) ((level & NANA_LOG_ASSERT) ? "ASSERT" : \
(level & NANA_LOG_ERROR) ? "ERROR" : (level & NANA_LOG_WARN) ? "WARNING" : \
(level & NANA_LOG_INFO) ? "INFO" : (level & NANA_LOG_DEBUG) ? "DEBUG" : \
(level & NANA_LOG_TRACE) ? "TRACE" : "UNEXPECTED")
inline static const char* _nana_last_path(const char* s) {
const char* p_ = strrchr(s,'/');
return p_ ? (p_+1) : (s);
}
#define _NANA_FILENAME _nana_last_path(__FILE__)
#define nana_log(level, fmt, ...) \
_nana_log_printf(level, "%s(%d): %s: [%s] " fmt, \
_NANA_FILENAME, __LINE__, __PRETTY_FUNCTION__, _nana_log_level_to_label(level), ##__VA_ARGS__)
#else
#define nana_log(level, fmt, ...)
#endif
#undef nana_assert
#undef nana_assertf
#ifdef _NANA_LOG_ASSERT
#define nana_assert(expr) \
do{ \
if(!(expr)){nana_log(NANA_LOG_ASSERT, "assertion failed! - `%s' is not true.", #expr);_nana_abort();} \
}while(0)
#define nana_assertf(expr, fmt, ...) \
do{ \
if(!(expr)){nana_log(NANA_LOG_ASSERT, "assertion failed! - `%s' is not true: " fmt, #expr, ##__VA_ARGS__); _nana_abort();} \
}while(0)
#else
#define nana_assert(expr)
#define nana_assertf(expr, fmt, ...)
#endif
#undef nana_error
#ifdef _NANA_LOG_ERROR
#define nana_error(fmt, ...) nana_log(NANA_LOG_ERROR, fmt, ##__VA_ARGS__)
#else
#define nana_error(fmt, ...)
#endif
#undef nana_warn
#ifdef _NANA_LOG_WARN
#define nana_warn(fmt, ...) nana_log(NANA_LOG_WARN, fmt, ##__VA_ARGS__)
#else
#define nana_warn(fmt, ...)
#endif
#undef nana_info
#ifdef _NANA_LOG_INFO
#define nana_info(fmt, ...) nana_log(NANA_LOG_INFO, fmt, ##__VA_ARGS__)
#else
#define nana_info(fmt, ...)
#endif
#undef nana_debug
#ifdef _NANA_LOG_DEBUG
#define nana_debug(fmt, ...) nana_log(NANA_LOG_DEBUG, fmt, ##__VA_ARGS__)
#else
#define nana_debug(fmt, ...)
#endif
#undef nana_trace
#undef nana_enter
#undef nana_enterf
#undef nana_leave
#undef nana_leavef
#undef nana_pass
#ifdef _NANA_LOG_TRACE
#define nana_trace(fmt, ...) nana_log(NANA_LOG_TRACE, fmt, ##__VA_ARGS__)
#define nana_enter() nana_trace("[ENTER]")
#define nana_enterf(fmt, ...) nana_trace("[ENTER] " fmt, ##__VA_ARGS__)
#define nana_leave() nana_trace("[LEAVE]")
#define nana_leavef(fmt, ...) nana_trace("[LEAVE] " fmt, ##__VA_ARGS__)
#define nana_pass() nana_trace("[PASS]")
#else
#define nana_trace(fmt, ...)
#define nana_enter()
#define nana_enterf(fmt, ...)
#define nana_leave()
#define nana_leavef(fmt, ...)
#define nana_pass()
#endif
#ifndef NANA_LOG_HAVE_ABORT
#undef _nana_abort
#ifdef NDEBUG
#define _nana_abort()
#else
#include <stdlib.h>
#define _nana_abort() abort()
#endif
#endif
#ifndef NANA_LOG_HAVE_PRINTF
#define NANA_LOG_HAVE_PRINTF
#undef _nana_log_printf
#define _nana_log_printf(level, fmt, ...) NSLog(@"" fmt, ##__VA_ARGS__)
#endif
#endif // _NANA_LOG_H_
@sheutettz
Owner

if you want to use it with iConsole for example. you may create a header file MyLog.h like below:

#import <Foundation/Foundation.h>
#import "iConsole.h"

#undef  NANA_LOG_HAVE_PRINTF
#define NANA_LOG_HAVE_PRINTF
#define _nana_log_printf(level, fmt, ...) [iConsole log:@"" fmt, ##__VA_ARGS__]

#include "NANALog.h"

then you import your MyLog.h in your objective-c source files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.