source: rtems/cpukit/shttpd/log.c @ ba3e987e

4.104.115
Last change on this file since ba3e987e was 086442b7, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/02/09 at 11:00:38

Whitespace removal.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3 * All rights reserved
4 *
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file.  As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
9 */
10
11#include "defs.h"
12
13/*
14 * Log function
15 */
16void
17elog(int flags, struct conn *c, const char *fmt, ...)
18{
19        char    date[64], buf[URI_MAX];
20        int     len;
21        FILE    *fp = c == NULL ? NULL : c->ctx->error_log;
22        va_list ap;
23
24        /* Print to stderr */
25        if (c == NULL || c->ctx->inetd_mode == 0) {
26                va_start(ap, fmt);
27                (void) vfprintf(stderr, fmt, ap);
28                (void) fputc('\n', stderr);
29                va_end(ap);
30        }
31
32        strftime(date, sizeof(date), "%a %b %d %H:%M:%S %Y",
33            localtime(&current_time));
34
35        len = my_snprintf(buf, sizeof(buf),
36            "[%s] [error] [client %s] \"%s\" ",
37            date, c ? inet_ntoa(c->sa.u.sin.sin_addr) : "-",
38            c && c->request ? c->request : "-");
39
40        va_start(ap, fmt);
41        (void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
42        va_end(ap);
43
44        buf[sizeof(buf) - 1] = '\0';
45
46        if (fp != NULL && (flags & (E_FATAL | E_LOG))) {
47                (void) fprintf(fp, "%s\n", buf);
48                (void) fflush(fp);
49        }
50
51#if defined(_WIN32) && !defined(NO_GUI)
52        {
53                extern HWND     hLog;
54
55                if (hLog != NULL)
56                        SendMessage(hLog, WM_APP, 0, (LPARAM) buf);
57        }
58#endif /* _WIN32 */
59
60        if (flags & E_FATAL)
61                exit(EXIT_FAILURE);
62}
63
64/*
65 * HACK: m68k-gcc <= 4.2.1 ICEs on the snprintf below for some
66 * coldfire variants for yet unknown reasons.
67 * C.f.: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32307
68
69 * T.S, 2008/3/25 -- 4.2.3 still has this problem
70 */
71#if defined(__GNUC__) && \
72   ( (__GNUC__ < 4 ) || \
73     ( (__GNUC__ == 4 ) && (__GNUC_MINOR__ < 2 ) ) || \
74       ( (__GNUC__ == 4 ) && (__GNUC_MINOR__ == 2 ) && (__GNUC_PATCHLEVEL__ <= 3 ) ) )
75#if defined(__mcoldfire__)
76#define SPLIT_SNPRINTF 1
77#endif /* __mcoldfire__ */
78#endif /* __GNUC__ */
79
80void
81log_access(FILE *fp, const struct conn *c)
82{
83        static const struct vec dash = {"-", 1};
84
85        const struct vec        *user = &c->ch.user.v_vec;
86        const struct vec        *referer = &c->ch.referer.v_vec;
87        const struct vec        *user_agent = &c->ch.useragent.v_vec;
88        char                    date[64], buf[URI_MAX], *q1 = "\"", *q2 = "\"";
89
90        if (user->len == 0)
91                user = &dash;
92
93        if (referer->len == 0) {
94                referer = &dash;
95                q1 = "";
96        }
97
98        if (user_agent->len == 0) {
99                user_agent = &dash;
100                q2 = "";
101        }
102
103        (void) strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S",
104                        localtime(&current_time));
105
106#if SPLIT_SNPRINTF
107        {
108          int num;
109          num = my_snprintf(buf, sizeof(buf),
110            "%s - %.*s [%s %+05d] \"%s\" %d %lu",
111            inet_ntoa(c->sa.u.sin.sin_addr), user->len, user->ptr,
112            date, tz_offset, c->request ? c->request : "-",
113            c->status, c->loc.io.total );
114          my_snprintf(&buf[num], sizeof(buf)-num, " %s%.*s%s" " %s%.*s%s",
115            q1, referer->len, referer->ptr, q1,
116            q2, user_agent->len, user_agent->ptr, q2);
117        }
118#else
119        (void) my_snprintf(buf, sizeof(buf),
120            "%s - %.*s [%s %+05d] \"%s\" %d %lu %s%.*s%s %s%.*s%s",
121            inet_ntoa(c->sa.u.sin.sin_addr), user->len, user->ptr,
122            date, tz_offset, c->request ? c->request : "-",
123            c->status, (unsigned long) c->loc.io.total,
124            q1, referer->len, referer->ptr, q1,
125            q2, user_agent->len, user_agent->ptr, q2);
126#endif
127
128        if (fp != NULL) {
129                (void) fprintf(fp, "%s\n", buf);
130                (void) fflush(fp);
131        }
132
133#if defined(_WIN32) && !defined(NO_GUI)
134        {
135                extern HWND     hLog;
136
137                if (hLog != NULL)
138                        SendMessage(hLog, WM_APP, 0, (LPARAM) buf);
139        }
140#endif /* _WIN32 */
141}
Note: See TracBrowser for help on using the repository browser.