Annotation of libwww/Library/src/HTLog.c, revision 2.24
2.1 frystyk 1: /* HTLog.c
2: ** LOGGING MODULE
3: **
2.3 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.1 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.24 ! frystyk 6: ** @(#) $Id: HTLog.c,v 2.23 1999/02/19 23:09:35 frystyk Exp $
2.1 frystyk 7: **
8: ** This module contains a simple logging mechanism for requests.
9: ** The user must open and close the log file!!!
10: **
11: ** History:
12: ** 01 May 95 Henrik Frystyk, frystyk@w3.org
2.2 frystyk 13: ** Bugs:
14: ** ANSI file handling is not capable of handling simultanous writing
15: ** from several processes at the same time in a multi-process environment
2.1 frystyk 16: */
17:
18: /* Library include files */
2.5 frystyk 19: #include "WWWLib.h"
2.1 frystyk 20: #include "HTLog.h" /* Implemented here */
21:
2.17 frystyk 22: struct _HTLog {
23: FILE * fp;
24: BOOL localtime;
2.21 frystyk 25: int accesses;
2.17 frystyk 26: };
2.1 frystyk 27:
28: /* ------------------------------------------------------------------------- */
29:
30: /* Open a Logfile
31: ** --------------
32: ** You can use either GMT or local time. If no filename is given,
2.4 frystyk 33: ** no log is kept. New log entries can be either appended to an existing
34: ** file or overwriting an exsisting file.
2.1 frystyk 35: ** Returns YES if OK, NO on error
36: */
2.17 frystyk 37: PUBLIC HTLog * HTLog_open (const char * filename, BOOL local, BOOL append)
2.1 frystyk 38: {
2.17 frystyk 39: HTLog * log;
2.4 frystyk 40: if (!filename || !*filename) {
2.24 ! frystyk 41: HTTRACE(APP_TRACE, "Log......... No log file given\n");
2.17 frystyk 42: return NULL;
2.4 frystyk 43: }
2.17 frystyk 44:
45: if ((log = (HTLog *) HT_CALLOC(1, sizeof(HTLog))) == NULL)
46: HT_OUTOFMEM("HTLog_open");
47:
2.24 ! frystyk 48: HTTRACE(APP_TRACE, "Log......... Open log file `%s\'\n" _ filename);
2.17 frystyk 49: log->fp = fopen(filename, append ? "a" : "w");
50: if (!log->fp) {
2.24 ! frystyk 51: HTTRACE(APP_TRACE, "Log......... Can't open log file `%s\'\n" _ filename);
2.17 frystyk 52: HT_FREE(log);
53: return NULL;
2.1 frystyk 54: }
2.17 frystyk 55: log->localtime = local;
56: return log;
2.1 frystyk 57: }
58:
59:
60: /* Close the log file
61: ** ------------------
62: ** Returns YES if OK, NO on error
63: */
2.17 frystyk 64: PUBLIC BOOL HTLog_close (HTLog * log)
2.1 frystyk 65: {
2.17 frystyk 66: if (log && log->fp) {
67: int status;
2.24 ! frystyk 68: HTTRACE(APP_TRACE, "Log......... Closing log file %p\n" _ log->fp);
2.17 frystyk 69: status = fclose(log->fp);
70: HT_FREE(log);
71: return (status != EOF);
2.1 frystyk 72: }
73: return NO;
74: }
75:
2.21 frystyk 76: PUBLIC int HTLog_accessCount (HTLog * log)
77: {
78: return log ? log->accesses : -1;
79: }
80:
2.10 frystyk 81: /* Add entry to the log file
82: ** -------------------------
2.1 frystyk 83: ** Format: <HOST> - - <DATE> <METHOD> <URI> <RESULT> <CONTENT_LENTGH>
2.2 frystyk 84: ** which is almost equivalent to Common Logformat. Permissions on UNIX
85: ** are modified by umask.
2.1 frystyk 86: **
87: ** Returns YES if OK, NO on error
88: **
89: ** BUG: No result code is produced :-( Should be taken from HTError.c
90: */
2.17 frystyk 91: PUBLIC BOOL HTLog_addCLF (HTLog * log, HTRequest * request, int status)
2.1 frystyk 92: {
2.17 frystyk 93: if (log && log->fp) {
2.1 frystyk 94: time_t now = time(NULL);
2.17 frystyk 95: HTParentAnchor * anchor = HTRequest_anchor(request);
2.5 frystyk 96: char * uri = HTAnchor_address((HTAnchor *) anchor);
2.24 ! frystyk 97: HTTRACE(APP_TRACE, "Log......... Writing CLF log\n");
2.17 frystyk 98: fprintf(log->fp, "localhost - - [%s] %s %s %d %ld\n",
99: HTDateTimeStr(&now, log->localtime),
2.5 frystyk 100: HTMethod_name(HTRequest_method(request)),
2.16 frystyk 101: uri ? uri : "<null>", /* Bill Rizzi */
2.18 frystyk 102: abs(status),
2.11 frystyk 103: HTAnchor_length(anchor));
2.12 frystyk 104: HT_FREE(uri);
2.21 frystyk 105: log->accesses++;
2.17 frystyk 106: return (fflush(log->fp) != EOF); /* Actually update it on disk */
107: }
108: return NO;
109: }
110:
2.18 frystyk 111: /* Add entry to the referer log file
112: ** ---------------------------------
113: **
114: ** which is almost equivalent to Common Logformat. Permissions on UNIX
115: ** are modified by umask.
116: **
117: ** Returns YES if OK, NO on error
118: */
119: PUBLIC BOOL HTLog_addReferer (HTLog * log, HTRequest * request, int status)
120: {
121: if (log && log->fp && request) {
122: HTParentAnchor * parent_anchor = HTRequest_parent(request);
123: if (parent_anchor) {
124: char * me = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
125: char * parent = HTAnchor_address((HTAnchor *) parent_anchor);
2.24 ! frystyk 126: HTTRACE(APP_TRACE, "Log......... Writing Referer log\n");
2.20 frystyk 127: if (me && parent && *parent) {
2.18 frystyk 128: fprintf(log->fp, "%s -> %s\n", parent, me);
129: }
130: HT_FREE(me);
131: HT_FREE(parent);
2.21 frystyk 132: log->accesses++;
2.18 frystyk 133: return (fflush(log->fp) != EOF); /* Actually update it on disk */
134: }
135: }
136: return NO;
137: }
138:
2.17 frystyk 139: /*
140: ** A generic logger - logs whatever you put in as the line.
141: */
142: PUBLIC BOOL HTLog_addLine (HTLog * log, const char * line)
143: {
144: if (log && log->fp && line) {
2.19 frystyk 145: fprintf(log->fp, "%s\n", line);
2.21 frystyk 146: log->accesses++;
2.17 frystyk 147: return (fflush(log->fp) != EOF); /* Actually update it on disk */
2.1 frystyk 148: }
149: return NO;
150: }
2.18 frystyk 151:
2.20 frystyk 152: /*
153: ** A generic logger with variable arguments
154: */
155: PUBLIC BOOL HTLog_addText (HTLog * log, const char * fmt, ...)
156: {
157: if (log && log->fp) {
158: va_list pArgs;
159: va_start(pArgs, fmt);
160: #ifdef HAVE_VPRINTF
2.21 frystyk 161: log->accesses++;
2.20 frystyk 162: (vfprintf(log->fp, fmt, pArgs));
2.22 frystyk 163: va_end(pArgs);
164: #endif
2.20 frystyk 165: return (fflush(log->fp) != EOF); /* Actually update it on disk */
166: }
167: return NO;
168: }
Webmaster