Skip to content

Instantly share code, notes, and snippets.

@pxpgraphics
Created November 21, 2014 18:54
Show Gist options
  • Save pxpgraphics/be3350fd219ee16c549c to your computer and use it in GitHub Desktop.
Save pxpgraphics/be3350fd219ee16c549c to your computer and use it in GitHub Desktop.
NSLog Override
#import <Foundation/Foundation.h>
#ifdef DEBUG
#define NSLog(args...) PXPLog(__FILE__,__LINE__,__PRETTY_FUNCTION__,args);
#else
#define NSLog(...)
#endif
void PXPLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...);
#import "PXPLog.h"
#import <stdio.h>
#import <stdlib.h>
#import <sys/time.h>
#import <time.h>
#import <pthread.h>
void PXPLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...)
{
// Arguments.
va_list args;
va_start(args, format);
if (![format hasSuffix:@"\n"]) {
format = [format stringByAppendingString:@"\n"];
}
// Message.
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
// System.
int processID = [NSProcessInfo processInfo].processIdentifier;
mach_port_t threadID = pthread_mach_thread_np(pthread_self());
// Names.
NSString *appName = [NSProcessInfo processInfo].processName;
NSString *fileName = [NSString stringWithUTF8String:file].lastPathComponent;
// Timestamp.
char timestamp[24];
time_t ltime;
struct tm *tm;
static struct timeval tv;
static struct timezone tz;
time(&ltime);
tm = (struct tm *)localtime(&ltime);
gettimeofday(&tv, &tz);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm);
snprintf(timestamp, sizeof(timestamp), "%s.%03d", timestamp, tv.tv_usec);
// Log.
fprintf(stderr, "%s %s[%d:%d] <%s:%d> %s", timestamp, appName.UTF8String, processID, threadID, fileName.UTF8String, lineNumber, message.UTF8String);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment