//// BXLogger.m// BaseTen//// Copyright (C) 2006-2008 Marko Karppinen & Co. LLC.//// Before using this software, please review the available licensing options// by visiting http://basetenframework.org/licensing/ or by contacting// us at sales@karppinen.fi. Without an additional license, this software// may be distributed only in compliance with the GNU General Public License.////// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License, version 2.0,// as published by the Free Software Foundation.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA//// $Id$//#import "BXLogger.h"#import <dlfcn.h>#import <unistd.h>enumBXLogLevelBXLogLevel=kBXLogLevelWarning;voidBXSetLogLevel(enumBXLogLevellevel){BXLogLevel=level;}staticinlineconstchar*LogLevel(enumBXLogLevellevel){char*retval=NULL;switch(level){casekBXLogLevelDebug:retval="DEBUG:";break;casekBXLogLevelInfo:retval="INFO:";break;casekBXLogLevelWarning:retval="WARNING:";break;casekBXLogLevelError:retval="ERROR:";break;casekBXLogLevelOff:casekBXLogLevelFatal:default:retval="FATAL:";break;}returnretval;}staticinlineconstchar*LastPathComponent(constchar*path){constchar*retval=((strrchr(path,'/')?:path-1)+1);returnretval;}staticchar*LibraryName_a(constvoid*addr){Dl_infoinfo={};char*retval=NULL;if(dladdr(addr,&info))retval=strdup(LastPathComponent(info.dli_fname));returnretval;}staticchar*ExecutableName_a(){uint32_tpathLength=0;_NSGetExecutablePath(NULL,&pathLength);char*path=malloc(pathLength);char*retval=NULL;if(path){if(0==_NSGetExecutablePath(path,&pathLength))retval=strdup(LastPathComponent(path));free(path);}returnretval;}voidBXAssertionDebug(){BXLogInfo(@"Break on BXAssertionDebug to inspect.");}voidBXLog(constchar*fileName,constchar*functionName,void*functionAddress,intline,enumBXLogLevellevel,idmessageFmt,...){va_listargs;va_start(args,messageFmt);BXLog_v(fileName,functionName,functionAddress,line,level,messageFmt,args);va_end(args);}voidBXLog_v(constchar*fileName,constchar*functionName,void*functionAddress,intline,enumBXLogLevellevel,idmessageFmt,va_listargs){NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];char*executable=ExecutableName_a();char*library=LibraryName_a(functionAddress);constchar*file=LastPathComponent(fileName);constchar*date=[[[NSDatedate]descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S.%F"timeZone:nillocale:nil]UTF8String];constchar*message=[[[[NSStringalloc]initWithFormat:messageFmtarguments:args]autorelease]UTF8String];charisMain=([NSThreadisMainThread]?'m':'s');fprintf(stderr,"%23s %s (%s) [%d] %s:%d %s [%p%c] \t%8s %s\n",date,executable,library?:"???",getpid(),file,line,functionName,[NSThreadcurrentThread],isMain,LogLevel(level),message);if(executable)free(executable);if(library)free(library);[pooldrain];}