📄 ch21.htm
字号:
<BR>
//<BR>
// This program creates a log file similar to the server log files,
just<BR>
// with richer information.<BR>
//<BR>
// By Shuman Ghosemajumder, Anadas Software Development<BR>
//<BR>
// GENERAL ALGORITHM<BR>
//<BR>
// 1. Get the desired environment variables<BR>
//<BR>
// 2. Write them to a file!<BR>
<BR>
// IncLUDES ***********************************************************
<BR>
<BR>
#include <stdio.h><BR>
#include <string.h><BR>
#include <stdlib.h><BR>
#include <time.h><BR>
<BR>
// DEFINES AND STRUCTURES *********************************************
<BR>
<BR>
#define MAX_STRING 256<BR>
#define DATE_STRING 32<BR>
#define HOUR_STRING 5<BR>
<BR>
#define LOG_FILE "./pseudo-log"<BR>
<BR>
// FUncTION PROTOTYPES ************************************************
<BR>
<BR>
int main(int argc, char *argv[], char *env[]);<BR>
void SafeGetEnv( char * env_name, char * * ptr, char * null_string
);<BR>
<BR>
// FUncTIONS **********************************************************
<BR>
<BR>
int main(int argc, char *argv[], char *env[])<BR>
{<BR>
char * browser,<BR>
* hostname,
<BR>
* refer_url;
<BR>
char date[32];<BR>
char empty_string[1];<BR>
time_t bintime;<BR>
<BR>
time(&bintime);<BR>
sprintf( date,"%s\0", ctime(&bintime)
);<BR>
date[24] = '\0'; //
exactly 24 chars in length<BR>
<BR>
empty_string[0] = '\0';<BR>
<BR>
SafeGetEnv( "REMOTE_HOST", &hostname,
empty_string );<BR>
SafeGetEnv( "HTTP_REFERER",
&refer_url, empty_string );<BR>
SafeGetEnv( "HTTP_USER_AGENT",
&browser, empty_string );<BR>
<BR>
FILE * fp;<BR>
<BR>
fp = fopen( LOG_FILE, "a" );
<BR>
<BR>
fprintf( fp, "%s %s %s %s\n",
date, hostname, refer_url, browser );<BR>
<BR>
fclose( fp );<BR>
<BR>
return (0); // exit gracefully<BR>
}<BR>
<BR>
void SafeGetEnv( char * env_name, char * * ptr, char * null_string
)<BR>
{<BR>
// Normally a NULL pointer is returned
if a certain environment variable<BR>
// doesn't exist and you try to retrieve
it. This function set the value<BR>
// of the pointer to point at a NULL string
instead.<BR>
<BR>
char * tmp;<BR>
<BR>
tmp = getenv( env_name );<BR>
<BR>
if( ! tmp ) *ptr = null_string;
<BR>
else *ptr
= tmp;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<H2><FONT SIZE=5 COLOR=#FF0000><A NAME="LoggingAccesses">Logging Accesses </A></FONT></H2>
<P>
Now that we have a program to extract environment variable information,
we're in much the same situation we were in when we simply had
access to the access log file. We can create a huge log file of
the various environment variable information we wish to keep track
of, but the raw information isn't very useful unless we summarize
it and have the output visible through the Web.
<P>
Listing 21.6 is a program that parses the pseudo access log created
by the program in Listing 21.5. This program reads in the pseudo
access log file generated by makelogg.cpp and generates an HTML
as output. The document summarizes all of the raw information
presented in that access log into useful categories. Figure 21.2
shows some sample output from it.
<P>
<A HREF="f21-2.gif" ><B>Figure 21.2:</B> <I>A sample shot of the output from the Pseudo Access Log Summary program</I></A>
<HR>
<BLOCKQUOTE>
<B>Listing 21.6. Source code listing for the Pseudo Access Log
Summary program.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">// parselog.cpp -- AccESS LOG SUMMARY
PROGRAM for "MAKE LOG"<BR>
// Available on-line at http://www.anadas.com/cgiunleashed/trackuser/
<BR>
//<BR>
// This program reads in the pseudo access log file generated
by parselog.cpp<BR>
// and generates an HTML document as output. The document
summarizes all of<BR>
// the raw information presented in that access log into useful
categories.<BR>
//<BR>
// By Shuman Ghosemajumder, Anadas Software Development<BR>
//<BR>
// The categories it summarizes information for:<BR>
//<BR>
// * # of hits by domain<BR>
// * # of hits by referrer<BR>
// * # of hits by date<BR>
// * # of hits by browser<BR>
//<BR>
// GENERAL ALGORITHM<BR>
//<BR>
// 1. For each domain and file path, dynamically create a linked
list<BR>
// for each value, and add 1 to the hit
count each time.<BR>
//<BR>
// 2. Create a linked list for each date, as well as each hour
also.<BR>
//<BR>
// 3. Send the output to stdout.<BR>
<BR>
<BR>
// IncLUDES ***********************************************************
<BR>
<BR>
#include <stdio.h><BR>
#include <string.h><BR>
#include <stdlib.h><BR>
<BR>
#include "linklist.h"
// Linked List Header File<BR>
<BR>
#include "linklist.cpp" //
Linked List Functions<BR>
<BR>
// DEFINES AND STRUCTURES *********************************************
<BR>
<BR>
#define MAX_STRING 256<BR>
#define DATE_STRING 32<BR>
#define HOUR_STRING 5<BR>
<BR>
#define LOG_FILE "./pseudo-log"<BR>
<BR>
typedef struct<BR>
{ char refer[MAX_STRING];<BR>
int num_access;<BR>
} sREFER;<BR>
<BR>
typedef struct<BR>
{ char browser[MAX_STRING];<BR>
int num_access;<BR>
} sBROWSER;<BR>
<BR>
typedef struct<BR>
{ char hostname[MAX_STRING];<BR>
int num_access;<BR>
} sHOSTNAME;<BR>
<BR>
typedef struct<BR>
{ char date[DATE_STRING];<BR>
int num_access;<BR>
} sDATE;<BR>
<BR>
<BR>
// FUncTION PROTOTYPES ************************************************
<BR>
<BR>
int main(int argc, char *argv[], char *env[]);<BR>
void ProcessLine( char * line );<BR>
void PrintOutput( void );<BR>
void InitAll(void);<BR>
void DestroyAll(void);<BR>
<BR>
// GLOBAL VARIABLES ***************************************************
<BR>
<BR>
sLINK * link_hostname;<BR>
sLINK * link_date;<BR>
sLINK * link_refer;<BR>
sLINK * link_browser;<BR>
<BR>
<BR>
// FUncTIONS **********************************************************
<BR>
<BR>
int main(int argc, char *argv[], char *env[])<BR>
{<BR>
printf("Content-type: text/html\n\n");
<BR>
printf("<HTML><TITLE>Pseudo
Access Log Summary</TITLE><BODY>\n");<BR>
printf("<H1>Pseudo Access Log
Summary</H1>\n");<BR>
<BR>
FILE * fp;<BR>
<BR>
fp = fopen( LOG_FILE, "r" ); //
open the access log file<BR>
<BR>
if( ! fp )<BR>
{ printf("ERROR:
Couldn't load log file!"); // abort
painlessly<BR>
}<BR>
else
//
if able to load file...<BR>
{ char line[512];<BR>
<BR>
InitAll();<BR>
<BR>
for(;;)<BR>
{ //
fetch lines until EOF encountered<BR>
<BR>
if(
fgets( line, 511, fp ) == NULL ) break;<BR>
<BR>
ProcessLine(
line ); // extract
the important information<BR>
}<BR>
<BR>
PrintOutput(); //
send the output to stdout<BR>
}<BR>
<BR>
DestroyAll();<BR>
<BR>
printf("</ul></BODY></HTML>\n"); //
end the HTML file<BR>
<BR>
return(0); // exist gracefully<BR>
}<BR>
void InitAll(void)<BR>
{<BR>
// Initialize the head pointers<BR>
<BR>
InitHead( &link_hostname );<BR>
InitHead( &link_refer );<BR>
InitHead( &link_browser );<BR>
InitHead( &link_date );<BR>
}<BR>
<BR>
void DestroyAll(void)<BR>
{<BR>
// Destroy the linked lists and free memory
<BR>
<BR>
DestroyList( &link_hostname );<BR>
DestroyList( &link_refer );<BR>
DestroyList( &link_browser );<BR>
DestroyList( &link_date );<BR>
}<BR>
<BR>
<BR>
void ProcessLine( char * line )<BR>
{<BR>
// Process a single line of the pseudo
access log file<BR>
<BR>
sHOSTNAME hn;<BR>
sREFER rf;<BR>
sBROWSER bs;<BR>
sDATE dt;<BR>
<BR>
char * left, * right;<BR>
sLINK * l;<BR>
<BR>
// Line Structure:<BR>
//<BR>
&nbs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -