]> err.no Git - scalable-opengroupware.org/blob - Misc/WebUI/NGExtensions/NGLogging/NGLogSyslogAppender.m
856b47548afe7936ac01decfbc17ff5e4d93dc7a
[scalable-opengroupware.org] / Misc / WebUI / NGExtensions / NGLogging / NGLogSyslogAppender.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 #import "NGLogSyslogAppender.h"
25 #import "NGLogEvent.h"
26 #include <syslog.h>
27 #include <stdarg.h>
28
29
30 @interface NGLogSyslogAppender (PrivateAPI)
31 - (int)syslogLevelForLogLevel:(NGLogLevel)_level;
32 @end
33
34 @implementation NGLogSyslogAppender
35
36
37 static NSString *defaultSyslogIdentifier = nil;
38
39
40 + (void)initialize {
41   NSUserDefaults *ud;
42   static BOOL isInitialized = NO;
43
44   if(isInitialized)
45     return;
46
47   ud = [NSUserDefaults standardUserDefaults];
48   defaultSyslogIdentifier =
49     [[ud stringForKey:@"NGLogSyslogIdentifier"] retain];
50
51   isInitialized = YES;
52 }
53
54 + (id)sharedAppender {
55   static id sharedAppender = nil;
56   if(sharedAppender == nil) {
57     sharedAppender = [[self alloc] init];
58   }
59   return sharedAppender;
60 }
61
62 - (id)init {
63     return [self initWithIdentifier:defaultSyslogIdentifier];
64 }
65
66 - (id)initWithIdentifier:(NSString *)_ident {
67   if((self = [super init])) {
68     #warning ** default flags?
69     openlog([_ident cString], LOG_PID | LOG_NDELAY, LOG_USER);
70   }
71   return self;
72 }
73
74 - (void)dealloc {
75   closelog();
76   [super dealloc];
77 }
78
79 - (void)appendLogEvent:(NGLogEvent *)_event {
80   NSString *formattedMsg;
81   int level;
82
83   formattedMsg = [self formattedEvent:_event];
84   level = [self syslogLevelForLogLevel:[_event level]];
85   syslog(level, [formattedMsg cString]);
86 }
87
88 - (int)syslogLevelForLogLevel:(NGLogLevel)_level {
89     int level;
90     
91     switch (_level) {
92         case NGLogLevelDebug:
93             level = LOG_DEBUG;
94             break;
95         case NGLogLevelInfo:
96             level = LOG_INFO;
97             break;
98         case NGLogLevelWarn:
99             level = LOG_WARNING;
100             break;
101         case NGLogLevelError:
102             level = LOG_ERR;
103             break;
104         case NGLogLevelFatal:
105             level = LOG_ALERT; // LOG_EMERG is broadcast to all users...
106             break;
107         default:
108             level = LOG_NOTICE;
109             break;
110     }
111     return level;
112 }
113
114 @end