OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [lib/] [syslog.c] - Blame information for rev 507

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * RTEMS version of syslog and associated routines
3
 *
4
 * $Id: syslog.c,v 1.2 2001-09-27 12:01:52 chris Exp $
5
 */
6
 
7
#include <rtems.h>
8
#include <stdio.h>
9
#include <stdarg.h>
10
#include <errno.h>
11
#include <syslog.h>
12
#include <sys/types.h>
13
#include <sys/socket.h>
14
#include <netinet/in.h>
15
 
16
#include <unistd.h>
17
 
18
static int LogStatus = LOG_CONS;
19
static const char *LogTag = "syslog";
20
static int LogFacility = LOG_USER;
21
static int LogMask = 0xff;
22
 
23
static int LogFd = -1;
24
static rtems_id LogSemaphore;
25
extern struct in_addr rtems_bsdnet_log_host_address;
26
 
27
#define SYSLOG_PORT     514
28
 
29
void
30
syslog (int pri, const char *fmt, ...)
31
{
32
        va_list ap;
33
 
34
        va_start (ap, fmt);
35
        vsyslog (pri, fmt, ap);
36
        va_end (ap);
37
}
38
 
39
/*
40
 * FIXME: Should cbuf be static?  It could be if we put the mutex
41
 *        around the entire body of this routine.  Then we wouldn't
42
 *        have to worry about blowing stacks with a local variable
43
 *        that large.  Could make cbuf bigger, too.
44
 */
45
void
46
vsyslog (int pri, const char *fmt, va_list ap)
47
{
48
        int cnt;
49
        char *cp;
50
        char *msgp, cbuf[200];
51
        int sent;
52
 
53
        if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
54
                syslog (LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID,
55
                                                                "syslog: unknown facility/priority: %#x", pri);
56
                pri &= LOG_PRIMASK|LOG_FACMASK;
57
        }
58
 
59
        if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
60
                return;
61
 
62
        if ((pri & LOG_FACMASK) == 0)
63
                pri |= LogFacility;
64
 
65
        cnt = sprintf (cbuf, "<%d>", pri);
66
        cp = msgp = cbuf + cnt;
67
        if (LogTag) {
68
                const char *lp = LogTag;
69
                while ((*cp = *lp++) != '\0')
70
                        cp++;
71
        }
72
        if (LogStatus & LOG_PID) {
73
                rtems_id tid;
74
                rtems_task_ident (RTEMS_SELF, 0, &tid);
75
                cnt = sprintf (cp, "[%#lx]", (unsigned long)tid);
76
                cp += cnt;
77
        }
78
        if (LogTag) {
79
                *cp++ = ':';
80
                *cp++ = ' ';
81
        }
82
        cnt = vsprintf (cp, fmt, ap);
83
        cnt += cp - cbuf;
84
        if (cbuf[cnt-1] == '\n')
85
                cbuf[--cnt] = '\0';
86
 
87
        if (LogStatus & LOG_PERROR)
88
                printf ("%s\n", cbuf);
89
 
90
        /*
91
         * Grab the mutex
92
         */
93
        sent = 0;
94
        if ((rtems_bsdnet_log_host_address.s_addr != INADDR_ANY)
95
         && (LogFd >= 0)
96
         && (rtems_semaphore_obtain (LogSemaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT) == RTEMS_SUCCESSFUL)) {
97
                /*
98
                 * Set the destination address/port
99
                 */
100
                struct sockaddr_in farAddress;
101
                farAddress.sin_family = AF_INET;
102
                farAddress.sin_port = htons (SYSLOG_PORT);
103
                farAddress.sin_addr = rtems_bsdnet_log_host_address;
104
                memset (farAddress.sin_zero, '\0', sizeof farAddress.sin_zero);
105
 
106
                /*
107
                 * Send the message
108
                 */
109
                if (sendto (LogFd, cbuf, cnt, 0, (struct sockaddr *)&farAddress, sizeof farAddress) >= 0)
110
                        sent = 1;
111
                rtems_semaphore_release (LogSemaphore);
112
        }
113
        if (!sent && (LogStatus & LOG_CONS) && !(LogStatus & LOG_PERROR))
114
                printf ("%s\n", msgp);
115
}
116
 
117
void
118
openlog (const char *ident, int logstat, int logfac)
119
{
120
        rtems_status_code sc;
121
        struct sockaddr_in myAddress;
122
 
123
        if (ident != NULL)
124
                LogTag = ident;
125
        LogStatus = logstat;
126
        if (logfac != 0 && (logfac & ~LOG_FACMASK) == 0)
127
                LogFacility = logfac;
128
 
129
        /*
130
         * Create the socket
131
         */
132
        if ((LogFd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
133
                printf ("Can't create syslog socket: %d\n", errno);
134
                return;
135
        }
136
 
137
        /*
138
         * Bind socket to name
139
         */
140
        myAddress.sin_family = AF_INET;
141
        myAddress.sin_addr.s_addr = INADDR_ANY;
142
        myAddress.sin_port = 0;
143
        memset (myAddress.sin_zero, '\0', sizeof myAddress.sin_zero);
144
        if (bind (LogFd, (struct sockaddr *)&myAddress, sizeof (myAddress)) < 0) {
145
                close (LogFd);
146
                LogFd = -1;
147
                printf ("Can't bind syslog socket: %d\n", errno);
148
                return;
149
        }
150
 
151
        /*
152
         * Create the mutex
153
         */
154
        sc = rtems_semaphore_create (rtems_build_name('s', 'L', 'o', 'g'),
155
                                        1,
156
                                        RTEMS_PRIORITY |
157
                                                RTEMS_BINARY_SEMAPHORE |
158
                                                RTEMS_INHERIT_PRIORITY |
159
                                                RTEMS_NO_PRIORITY_CEILING |
160
                                                RTEMS_LOCAL,
161
                                        0,
162
                                        &LogSemaphore);
163
        if (sc != RTEMS_SUCCESSFUL) {
164
                printf ("Can't create syslog seamphore: %d\n", sc);
165
                close (LogFd);
166
                LogFd = -1;
167
        }
168
}
169
 
170
void
171
closelog(void)
172
{
173
        if (LogFd >= 0) {
174
                close (LogFd);
175
                LogFd = -1;
176
                rtems_semaphore_delete (LogSemaphore);
177
        }
178
}
179
 
180
int
181
setlogmask (int pmask)
182
{
183
        int omask;
184
 
185
        omask = LogMask;
186
        if (pmask != 0)
187
                LogMask = pmask;
188
        return (omask);
189
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.