//
//  NXLog.h
//  NXKit
//
//  Created by 徐家骏 on 2017/6/15.
//  Copyright © 2017年 徐家骏. All rights reserved.
//

#import <Foundation/Foundation.h>

extern BOOL gEnableConsoleLog;

static NSString * const sNotificationNameLogCacheFull = @"NXlogCacheFull";

typedef NS_ENUM(int, NXLogLevel) {
    NXLogLevel_Error = 0,
    NXLogLevel_Warning,
    NXLogLevel_Info,
    NXLogLevel_Debug,
    NXLogLevel_Verbose,
};

//---------------------------------------------------------------------------------

#ifdef __cplusplus
extern "C"
{
#endif
    
    void NXPrintLog(NXLogLevel level, const char *func, int line, NSString *logEntry);
    
    NSString * NXGetAllLogs(void);
    NSString * NXGetAllLogsAndClean(void);

    NSArray * NXGetAllLogsList(void);
    
    void NXClearAllLogs(void);
    
#ifdef __cplusplus
}
#endif


//---------------------------------------------------------------------------------

#if defined(DEBUG) || defined(FORCE_LOG)
#define ASSERT(...) do { @try { NSAssert(__VA_ARGS__); } @catch (NSException *exception){ NSLog([@"handle exception reason: " stringByAppendingString:exception.reason], nil); } } while(0)
#else
#define ASSERT(...)
#endif

#define LOGTemplate(level, ...) \
do { \
NXPrintLog(level, __PRETTY_FUNCTION__, __LINE__, \
[NSString stringWithFormat:@__VA_ARGS__]); \
} while(0)

#define LOGE(...)   LOGTemplate(NXLogLevel_Error    , __VA_ARGS__)
#define LOGW(...)   LOGTemplate(NXLogLevel_Warning  , __VA_ARGS__)
#define LOGI(...)   LOGTemplate(NXLogLevel_Info     , __VA_ARGS__)
#define LOGD(...)   LOGTemplate(NXLogLevel_Debug    , __VA_ARGS__)

#ifdef DEBUG
#define LOGV(...)   LOGTemplate(NXLogLevel_Verbose  , __VA_ARGS__)
#else
#define LOGV(...)   do { } while(0)
#endif

#define NXTRACECMD(...) \
do { \
NXPrintLog(NXLogLevel_Debug, __PRETTY_FUNCTION__, __LINE__, \
[NSString stringWithFormat:@"%@|%@", NSStringFromSelector(_cmd), \
[NSString stringWithFormat:@__VA_ARGS__]]); \
} while(0)
