Re: ASL & Unicode in Xcode's Console
Re: ASL & Unicode in Xcode's Console
- Subject: Re: ASL & Unicode in Xcode's Console
- From: Karl Moskowski <email@hidden>
- Date: Wed, 29 Oct 2008 11:34:28 -0400
On Oct 29, 2008, at 09:41 , Jason Coco wrote:
On Oct 29, 2008, at 09:06 , Yvan BARTHÉLEMY wrote:
> A solution for this is to not use directly asl_log, but wrap it to a
> function that will then use asl_log for console, and fprintf for
> logging to stderr (instead of asl_open(stderr)), of course you will
> have to regenerate yourself date, host name, process name, process
> id information if needed when using fprintf.
Yeah, that's basically the solution I used, but didn't really bother
with re-generating the log information. I also used an undocumented
call that I found on the asl source code that let me specify the
encoding that I wanted instead of defaulting to that stupid "visual"
encoding for the console. But I wrapped the ASL stuff in an Objective-
C class so I just added code that if the level was ASL_LEVEL_DEBUG and
NDEBUG wasn't defined, the data went to stderr as well as the logging
system.
I too put the logging in a wrapper class, with a method that does the
heavy lifting and a few macros that simplify its use. Now, instead
using ASL_OPT_STDERR, I just fprintf nicely formatted messages to
stderr.
To make sure that there's no duplication of messages in the Console, I
wrapped the fprintf section in a conditional to ensure it's a tty.
Now, when Xcode runs the app, logging is echoed to Xcode's console. It
also has the advantage that Debug-level messages show up in the
console, but syslog's filters prevent them from making it to the
system log. (I got the idea from <http://www.cocoabuilder.com/archive/message/cocoa/2008/2/11/198548
>.)
// ASLogger.h
#define ASLog(c, s, l,...) [ASLogger logTo:(c) level:l
sourceFile:__FILE__ lineNumber:__LINE__ format:(s), ##__VA_ARGS__]
#define ASLogError(c, s, ...) ASLog((c), (s), ASL_LEVEL_ERR,
##__VA_ARGS__)
#define ASLogNotice(c, s, ...) ASLog((c), (s), ASL_LEVEL_NOTICE,
##__VA_ARGS__)
#define ASLogDebug(c, s, ...) ASLog((c), (s), ASL_LEVEL_DEBUG,
##__VA_ARGS__)
// ASLogger.m
+ (void) logTo:(aslclient)client level:(NSInteger)level sourceFile:
(char *)sourceFile lineNumber:(NSUInteger)lineNumber format:(NSString
*)format, ... {
va_list ap;
va_start(ap,format);
NSMutableString *message = [[NSMutableString alloc]
initWithFormat:format arguments:ap];
va_end(ap);
[message appendFormat:@" [%u]", lineNumber];
asl_log(client, NULL, level, [message UTF8String]);
if (isatty(fileno(stderr))) {
NSString *now = [[NSCalendarDate calendarDate]
descriptionWithCalendarFormat:@"%a %b %d %H:%M:%S"];
fprintf(stderr, "%s %s:%u <%s> %s\r\n",
[now UTF8String], [[[NSString stringWithUTF8String:sourceFile]
lastPathComponent] UTF8String],
lineNumber, [self aslLevelToString:level], [message UTF8String]);
fflush(stderr);
}
}
+ (const char *)aslLevelToString:(int) level {
switch (level) {
case ASL_LEVEL_EMERG: return ASL_STRING_EMERG;
case ASL_LEVEL_ALERT: return ASL_STRING_ALERT;
case ASL_LEVEL_CRIT: return ASL_STRING_CRIT;
case ASL_LEVEL_ERR: return ASL_STRING_ERR;
case ASL_LEVEL_WARNING: return ASL_STRING_WARNING;
case ASL_LEVEL_NOTICE: return ASL_STRING_NOTICE;
case ASL_LEVEL_INFO: return ASL_STRING_INFO;
default: return ASL_STRING_DEBUG;
}
}
----
Karl Moskowski <email@hidden>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden