2 Copyright (C) 2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NGLogConsoleAppender.h"
23 #include "NGLogEvent.h"
25 #include <sys/types.h>
28 @implementation NGLogConsoleAppender
30 static Class NSCalendarDateClass = Nil;
31 static Class NSProcessInfoClass = Nil;
32 static unsigned char *processName = NULL;
34 static char *monthNames[14] = {
36 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
37 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
42 if (NSCalendarDateClass == Nil)
43 NSCalendarDateClass = [NSCalendarDate class];
44 if (NSProcessInfoClass == Nil)
45 NSProcessInfoClass = [NSProcessInfo class];
47 if (processName == NULL) {
48 /* process name can't change, right? */
52 pn = [[NSProcessInfoClass processInfo] processName];
53 len = [pn cStringLength];
55 processName = malloc(len + 4);
56 [pn getCString:processName];
60 static __inline__ unsigned char * levelPrefixForEvent(NGLogEvent *_event) {
61 switch ([_event level]) {
62 case NGLogLevelWarn: return "[WARN]";
63 case NGLogLevelError: return "[ERROR]";
64 case NGLogLevelFatal: return "[FATAL]";
69 - (void)appendLogEvent:(NGLogEvent *)_event {
70 /* Note: pid can change after a fork() */
73 // TODO: get time using libc function, cheaper
75 date = [[NSCalendarDateClass alloc] init];
77 // TODO: cString for message is expensive
79 "%s %02i %02i:%02i:%02i %s [%d]: %s\n",
80 monthNames[[date monthOfYear]],
82 [date hourOfDay], [date minuteOfHour], [date secondOfMinute],
85 [[_event message] cString]);
90 @end /* NGLogConsoleAppender */