]> err.no Git - sope/blob - sope-core/NGExtensions/NGLogging.subproj/NGLogConsoleAppender.m
fixed bugs in logger, improved performance
[sope] / sope-core / NGExtensions / NGLogging.subproj / NGLogConsoleAppender.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3  
4   This file is part of OpenGroupware.org.
5   
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
9   later version.
10   
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.
15   
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
19   02111-1307, USA.
20 */
21
22 #include "NGLogConsoleAppender.h"
23 #include "NGLogEvent.h"
24 #include "common.h"
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 @implementation NGLogConsoleAppender
29
30 static Class NSCalendarDateClass = Nil;
31 static Class NSProcessInfoClass  = Nil;
32 static unsigned char *processName = NULL;
33
34 static char *monthNames[14] = {
35   "Dec", 
36   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
37   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
38   "Jan"
39 };
40
41 + (void)initialize {
42   if (NSCalendarDateClass == Nil)
43     NSCalendarDateClass = [NSCalendarDate class];
44   if (NSProcessInfoClass == Nil)
45     NSProcessInfoClass = [NSProcessInfo class];
46
47   if (processName == NULL) {
48     /* process name can't change, right? */
49     unsigned len;
50     NSString *pn;
51     
52     pn  = [[NSProcessInfoClass processInfo] processName];
53     len = [pn cStringLength];
54     
55     processName = malloc(len + 4);
56     [pn getCString:processName];
57   }
58 }
59
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]";
65   default:              return "";
66   }
67 }
68
69 - (void)appendLogEvent:(NGLogEvent *)_event {
70   /* Note: pid can change after a fork() */
71   NSCalendarDate *date;
72   
73   // TODO: get time using libc function, cheaper
74   // TODO: set to GMT?
75   date = [[NSCalendarDateClass alloc] init];
76   
77   // TODO: cString for message is expensive
78   fprintf(stderr,
79           "%s %02i %02i:%02i:%02i %s [%d]: %s\n",
80           monthNames[[date monthOfYear]],
81           [date dayOfMonth],
82           [date hourOfDay], [date minuteOfHour], [date secondOfMinute],
83           processName,
84           getpid(),
85           [[_event message] cString]);
86   
87   [date release];
88 }
89
90 @end /* NGLogConsoleAppender */