Cincom

Web Log Stats Lesson 3
Accessing an External File


| Table of Contents | Lesson 2 | Lesson 4 |


All of the information regarding the activity on our web site is contained in a log file. The web server creates a new file every day in which to record this information. We want to read just one of these files and gather "hits" by collecting all unique IP addresses (other computers from the Internet) coming into our web site.

This lesson explains how to locate this log file, open it, pull its contents into the VisualWorks environment and close it.

Remember, this tutorial requires some additional files for the exercises and workshops. You will want to download and install them on your machine before you begin.

1. If VisualWorks is not already running, please start running it now.

2. From the main VisualWorks window, either click the last button on the Toolbar or, select the Tools >> Workspace menu option.

3. In the Workspace, enter the following:

'ws000101.log'

4. Highlight this text, <Operate-Click> and select Inspect It.

A new window will appear (called appropriately enough the Inspector window) with multiple tabs. Since this is a a ByteString (as identified in the window caption), the Text tab is displayed as default. We will discuss the other tabs in a later lesson.


Figure 3-1. Our ByteString in the VisualWorks Inspector Window

Note that the string of characters looks exactly like what you highlighted. This should come as no surprise. You told Smalltalk to inspect a sequence of characters between single quotes and it displayed it as a ByteString. Typical computer; it did exactly what you told it to. Smalltalk has no idea that the string of characters within the quotes represents a computer file. If that was your meaning (which it was), you'll have to tell it so.

5. Close the Inspector window and modify your line as follows:

'ws000101.log' asFilename

6. Highlight this text, then <Operate-Click> and select Inspect It.

A new (Inspector) window will appear with 2 panes. Note that this time, the caption reads differently. For example, under WindowsNT and WindowsXP, it displays as a NTFSFilename. Under Linux/UNIX, it displays as a UnixFilename. In Windows 9X, it displays as a FatFilename. Now we have proof that Smalltalk "thinks" this string of characters is a Filename. Now we're getting somewhere!


Figure 3-2. Our Filename in the VisualWorks Inspector Window

If you did not see the Inspector window, make sure you typed the above exactly as stated. Smalltalk is case sensitive and the message asFilename cannot appear as any of the following:

·  "as File name"

·  "as Filename"

·  "as file name"

·  "asfilename"

·  "asFileName"

The message asFilename is an instance-side method of the String class that "converts" a string to a Filename object. Those other choices are close but the exact method is asFilename. You can dismiss the first three choices because method names cannot contain spaces. The last 2 are close but close doesn't work - you must use the precisely correct method name.

Important

In an attempt to make this tutorial "operating system neutral", and to make it easy for you to copy and paste the sample code from your browser directly into your workspace, it is important to place the six log files in the VisualWorks "default directory".

You may be used to putting files like these in a temporary directory. Therefore, to access the first log file in Windows, you would use something like c:\temp\ws000101.log and in Linux/UNIX, you would use something like /usr/tmp/ws000101.log. But to remain "operating system neutral", the sample code cannot use these fully qualified paths because they differ so much from one operating system to another. Therefore, from this point forward, when you are asked to reference this file or any other file, the example code will use only the file name (ws000101.log) but it will be up to you to use the fully qualified path to that file if you do not choose to place them in the VisualWorks "default directory".

The easiest way to determine the VisualWorks "default directory" is by returning to the main VisualWorks Launcher window and select the menu option File >> File Browser. A dialog box will appear like the one below:


Figure 3-3. The VisualWorks "default directory" appears highlighted

This is called the File Browser window. You will learn how to use this in a future lesson but what's important to note now is the directory highlighted in the leftmost pane. It will identify the name of the "default directory" of VisualWorks. In this example, a Windows-based system is used and the "default directory" is c:\vw7.4. This is where the log files resided while this tutorial was being developed.

The VisualWorks "default directory" is different from and should not be confused with the VisualWorks "home directory". The "default directory" is where VisualWorks is started from (and looks there first for files) and the "home directory" is the root directory where VisualWorks was installed. The "home directory" must be set correctly for VisualWorks to function properly. To avoid any confusion, this author decided to make the VisualWorks "home directory" and the VisualWorks "default directory" one in the same.

Since we are on the subject, let's determine how to set the VisualWorks "home directory".

Close the File Browser window.

To find out what your VisualWorks "home directory" is, from the main VisualWorks Launcher window, select the menu option File >> Set VisualWorks Home.


Figure 3-4. From the File menu, select Set VisualWorks Home


Figure 3-5. You can view/set the VisualWorks "home directory" here.

In this example, on a Windows-based system, the VisualWorks "home directory" is c:\vw7.4. This is the same location as the "default directory" therefore since the log files are placed in this directory, the use of a "fully qualified path" for specifying the location of the log files throughout this tutorial is NOT needed.

7. Close the Inspector window.

We now know how to tell Smalltalk that our string of characters is a file. Now let's find out more about it.

8. Modify your workspace so that it appears as follows:

| myFile |
myFile := 'ws000101.log' asFilename.
myFile fileSize.

9. Highlight all of the following text, <Operate-Click> and select Inspect It.

Note that an Inspector window will appear and the value of self is 69072. This number may vary depending on your operating system because this file has "carriage return-line feeds" at the end of each line and some operating systems will treat "carriage return-line feeds" as 1 character whereas other operating systems will treat it as 2 characters. The important item to note here is that VisualWorks returns a number. This will verify 2 things. One, you are indeed pointing to an existing file. Two, you are pointing to the correct file for this exercise. If you did not see the Inspector window or 69072 (plus or minus 100) in the Inspector window, then make sure you are pointing to where the file ws000101.log really does exist.


Figure 3-6. The filesize displayed in the VisualWorks Inspector Window

What if I get the following dialog box?


Figure 3-7. The infamous Notifier dialog box

This could mean a couple of things - none of which are good. The goal here is to make sure VisualWorks can access the log files, in this case just one of them. As you can probably deduce from the message ERROR_FILE_NOT_FOUND, this dialog box basically says that VisualWorks could not find this file. The reason for this could be twofold:

1) The log file is not in the VisualWorks "image directory".
2) Case sensitivity.

To solve the first situation, verify that all the other "log" files are located in the VisualWorks "image directory". The easiest way to determine the VisualWorks "image directory" is by returning to the main VisualWorks Launcher window and select the menu option File >> File Browser. Try to locate the log files in the directory that the File Browser goes to first.


Figure 3-8. The log files shown in the VisualWorks "image directory" (note: lower case)

Now return to your workspace and try it again. If you have moved the log files to the VisualWorks "default directory" and you still get this error, then the problem may be case sensitivity. The names of the log files should all be in lowercase (you might want to double-check that). The sample code above shows the file name in lowercase (ws000101.log). If you are using a "case-sensitive" operating system, such as Linux or one of the various flavors of UNIX, then this might be the problem. If you're not sure if your operating system is "case-sensitive", enter the following in a Workspace, highlight all of the text, <Operate-Click> and select Print It.

Filename isCaseSensitive

This will return either True or False. If it is True, then make sure that the string containing the file name (ws000101.log) matches exactly with the log file name as it is stored on your system.

Now return to your workspace and try it again. This should take care of any problems you might have in accessing the log files.

A lot is going on here so let's dissect this step by step.

| myFile |

On our first line, we are declaring a temporary variable. This is done by delimiting the variable name with the vertical bar character. A variable name cannot contain spaces. Variable names typically are meaningful words that describe what it holds and are never abbreviated (i.e. the words are always completely spelled out). Also, the first word in a multi-word variable is always lower case with all subsequent words beginning with a captial letter. Enough of that for now - more on naming conventions in a later lesson.

myFile := 'ws000101.log' asFilename.

On the second line, we are assigning to our temporary variable the filename reference to our web server log file. In future exercises, we will be referring to this file quite often and it's easier to do this with a temporary variable. In this manner, we won't have to keep typing 'ws000101.log' asFilename over and over again throughout our program.

myFile fileSize.

On the third line, there should be no surprises; a typical Smalltalk expression. The object myFile has been created (from the second line) and is sent the message fileSize which calculates how large the file is in bytes. Because we selected Inspect It, this value is then displayed in an Inspector window.

We still haven't read our file yet, although we are very close.

10. Modify the text as follows, highlight all of it, <Operate-Click> and select Do It.

| myFile |
myFile := 'ws000101.log' asFilename.
myFile edit.

Note that a new window will appear and the entire contents of this file has been brought into a file editor. In case you were wondering, as the name suggests, this is a fully functioning text editor. If you are not that thrilled with Notepad (Windows) or vi (Linux/UNIX) or SimpleEdit (Mac), then you might like this one.


Figure 3-9. The ws000101.log file in the VisualWorks File Editor

11. Close the VisualWorks File Editor. This will automatically close the file.

If you are an experienced programmer, you may have noticed some similarities between a language you have used before and Smalltalk. For example, the assignment character (:=) is similar to the convention used by Pascal. And the period at the end of the statement (sentence) is very much like COBOL. We're not here to argue who used what first or who borrowed what from whom, but rather to identify some key points of the Smalltalk language.

If you aren't an experienced programmer, all you need to remember is this: Sequential lines of code in Smalltalk are separated by periods. Assignment statements use the "colon equal sign" (:=) together. If you are curious as to why the equal sign is not used for assignment, it is because the equal sign alone is used to test for equality. The statement x = 3 in Smalltalk will return either True or False depending on the value of x.

So in describing our "3-line program" above that displays the contents of a file in the Smalltalk file editor, we used an instance of the class called Filename and sent it a message called edit.

Summary

If you have never seen a log file from a web server before, this is just one type of format that can be generated. This particular file is a comma-delimited ASCII text file with 15 separate fields between the commas. Some of these fields are self-explanatory while others are not. The field that interests us the most right now is the first one, an IP address of a visitor to our web site. In the next exercise, we will want to count them.

You now should know how to:

Determine VisualWork's default directory

Set the VisualWorks Home directory

Read in a file

Determine the size of a file

Bring a file into the internal Smalltalk editor

Convert a String to a Filename

Declare temporary variables

Assign values to variables using "colon equal" (:=)

Terminate multiple Smalltalk expressions (with a period)

 


| Table of Contents | Lesson 2 | Lesson 4 |