]> err.no Git - scalable-opengroupware.org/blob - WebUI/NGExtensions/NGLogging/NGLogSyslogAppender.m
syslog appender, notes/readme updated
[scalable-opengroupware.org] / 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)syslogPriorityForLogLevel:(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   int level;
81     
82   level = [self syslogPriorityForLogLevel:[_event level]];
83   syslog(level, [[_event message] cString]);
84 }
85
86 - (int)syslogLevelForLogLevel:(NGLogLevel)_level {
87     int level;
88     
89     switch (_level) {
90         case NGLogLevelDebug:
91             level = LOG_DEBUG;
92             break;
93         case NGLogLevelInfo:
94             level = LOG_INFO;
95             break;
96         case NGLogLevelWarn:
97             level = LOG_WARNING;
98             break;
99         case NGLogLevelError:
100             level = LOG_ERR;
101             break;
102         case NGLogLevelFatal:
103             level = LOG_ALERT; // LOG_EMERG is broadcast to all users...
104             break;
105         default:
106             level = LOG_NOTICE;
107             break;
108     }
109     return level;
110 }
111
112 @end