Skip to content
jessicahunter24 edited this page Apr 16, 2013 · 23 revisions

PINSPEC provides a set of utility functions to print level-based log messages to the screen, as well as an output log file generated from each simulation. Using this logging capability is highly recommended, as it gives code output readability and keeps track of previous runs via a log file created as part of the output.

There are different levels of log messages, each with a particular tag:

  • Level 0: DEBUG
  • Level 1: INFO
  • Level 2: NORMAL
  • Level 3: SEPARATOR
  • Level 4: HEADER
  • Level 5: TITLE
  • Level 6: WARNING
  • Level 7: CRITICAL
  • Level 8: RESULT
  • Level 9: UNITTEST
  • Level 10: ERROR

To print out information about the progression of the code to the screen, one might use the 'INFO' keyword. To format the logging level, or a section in the screen printout, the keywords 'NORMAL', 'SEPARATOR', 'HEADER', 'TITLE' can be used. An example of output format is shown below for messages with several tags:

!

##Setting the Log Level In order to view log messages, the log level should be set near the beginning of the file, using the constructor 'py_setlevel()'. The argument of py_setlevel is the keyword for the level, given above, surrounded by single quotes. Selecting a specific level ensures that any logging message tagged with that keyword will be printed along with any other messages with a higher logging level. For example, the following command,

>>> py_setlevel('CRITICAL')

would cause all messages tagged with the keywords CRITICAL, RESULT, and ERROR to be printed to the screen and the log file.

>>> py_setlevel('INFO')

would cause all messages tagged with the tags greater than equal to NORMAL will be printed to the screen and the log file. The default log level for PINSPEC is NORMAL.

##Printing Messages to the Screen Now that the logging module has been imported, you can write formatted messages to the screen. For example, the following will print 'Hello World!' to the console:

>>> py_printf('NORMAL', 'Hello World!')
[  NORMAL ]  HELLO WORLD!

PINSPEC's logging utilities allow you to include numbers and strings in your messages using the C/C++ styles for formatted I/O. For example, you can create variables in python for integers, floating point numbers and strings and insert them into your formatted messsage as follows:

>>> my_name = 'Will Boyd'
>>> my_age = 25
>>> py_printf('NORMAL', 'My name is %s and I am %d years old', my_name, my_age)
[  NORMAL ]  My name is Will Boyd and I am 25 years old.

PINSPEC's logging module will chop messages that are greater than 80 characters into individual lines of 80 characters or less to provide for nicely formatted output which can fit in a default size console. An example for how this appears is as follows:

>>> my_favorite_number = 42.0
>>> py_printf('NORMAL', 'The answer to life the universe and everything happens to be my favorite number, %f', my_favorite_number)
[  NORMAL ]  The answer to life the universe and everything happens to be my
[  NORMAL ]  ... favorite number, 42.000000

##Output Log File As mentioned earlier, all log messages are printed to a log file during a any given instantiation of the pinspec package. PINSPEC will create a 'log' folder within the output folder where each log file will reside. This folder contains a log file for each run, allowing users to examine the details of different runs. Log files have unique filenames with the date and time at which the PINSPEC package was first imported into Python with the following format: 'pinspec-mm-dd-yyyy--hh:mm:ss.log'.

The preceding Python commands for print log messages are compiled into the following Python script:

from pinspec.log import *

py_printf('NORMAL', 'Hello World!')

my_name = 'Will Boyd'
my_age = 25
py_printf('NORMAL', 'My name is %s and I am %d years old', my_name, my_age)

my_favorite_number = 42.0
py_printf('NORMAL', 'The answer to life the universe and everything happens to be my favorite number, %f', my_favorite_number)

Next: Numpy

Tutorials

Home