]> err.no Git - sope/blob - sope-core/NGExtensions/NGLogging.subproj/NGLogEventDetailedFormatter.m
e5531a8b94260d62dad971613d497a2cf5203ad3
[sope] / sope-core / NGExtensions / NGLogging.subproj / NGLogEventDetailedFormatter.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OGo
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 // $Id$
22
23
24 #include "NGLogEventFormatter.h"
25 #include "NGLogLevel.h"
26
27 @class NSString;
28
29 @interface NGLogEventDetailedFormatter : NGLogEventFormatter
30 {
31 }
32
33 @end
34
35 #include "NGLogEvent.h"
36 #include "common.h"
37 #include "NSProcessInfo+misc.h"
38
39 @implementation NGLogEventDetailedFormatter
40
41 static unsigned char *processName = NULL;
42 static NSProcessInfo *processInfo = nil;
43
44 static char *monthNames[14] = {
45   "Dec", 
46   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
47   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
48   "Jan"
49 };
50
51 + (void)initialize {
52   static BOOL didInit = NO;
53   unsigned    len;
54   NSString    *pn;
55
56   if (didInit) return;
57
58   didInit     = YES;
59   processInfo = [[NSProcessInfo processInfo] retain];
60   pn          = [processInfo processName];
61   len         = [pn cStringLength];
62   processName = malloc(len + 4);
63   [pn getCString:processName];
64 }
65
66 static __inline__ unsigned char * levelPrefixForEvent(NGLogEvent *_event) {
67   switch ([_event level]) {
68     case NGLogLevelWarn:  return "[WARN] ";
69     case NGLogLevelError: return "[ERROR] ";
70     case NGLogLevelFatal: return "[FATAL] ";
71     default:              return "";
72   }
73 }
74
75 - (NSString *)formattedEvent:(NGLogEvent *)_event {
76   NSMutableString *fe;
77   NSCalendarDate  *date;
78
79   fe = [NSMutableString stringWithCapacity:160];
80   /* timestamp, process name, process id, level prefix */
81   date = [_event date];
82   [fe appendFormat:@"%s %02i %02i:%02i:%02i %s [%d]: %s",
83     monthNames[[date monthOfYear]],
84     [date dayOfMonth],
85     [date hourOfDay], [date minuteOfHour], [date secondOfMinute],
86     processName,
87     /* Note: pid can change after a fork() */
88 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
89     [processInfo processIdentifier],
90 #else
91     [[processInfo processId] intValue],
92 #endif
93     levelPrefixForEvent(_event)];
94     
95   /* message */
96   [fe appendString:[_event message]];
97   return fe;
98 }
99
100 @end /* NGLogEventDetailedFormatter */