Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Creating automatic log file when I start Stata in MAC


From   Donald Spady <[email protected]>
To   [email protected]
Subject   Re: st: Creating automatic log file when I start Stata in MAC
Date   Fri, 3 Dec 2010 18:27:39 -0700

Eric
   Works like a charm.  Many thanks.

Don

On 2010-12-03, at 8:08 AM, Eric Booth wrote:

> <>
> 
> There are two separate issues here:  1) the local time displaying an error and 2) the logfile starts, but the version of setlogfile.ado I wrote does not prompt for a logfile name when run from your profile.do (it should still prompt for a log file name if you put this code in the file "setlogfile.ado" and put it in your adofile path).
> 
> For (1)  the issue is that somehow the spacing got removed when you copy/pasted my program to your do-file editor.  
> When I copy/paste your line:
> 
>> local time:display %td date("$S_DATE","DMY")"_"substr("$S_TIME",1,2)"-"substr("$S_TIME",4,2)
> 
> I get the same error message you did, but when I copy/paste my line:
> 
>>> local time:display  %td   date("$S_DATE", "DMY")  "_"  substr("$S_TIME",1,2)  "-"  substr("$S_TIME",4,2)
> 
> with spaces between the "-" , "_", and the substr() functions, then it works for me.  Try running just this line from the command window and re-inserting the spacing.
> 
> 
> For (2), the issue is my misunderstanding of the way profile.do handles the -display- command called from .ado files & that I did not test running this from my profile.do, I only tested it by running it from the command window as an .ado file.    
> -setlogfile- does run from profile.do without error, but, as you mention, doesn't prompt even if you insert a "noi" in front of all the "display" commands.  
> There is probably a way to write setlogfile.ado so that it can be called in the profile and prompt users as I intended, but I couldn't get it to display the prompt.  
> So, rather than just saving setlogfile.ado in my path and calling the program from the profile.do, if I copy/pasted the program into the the profile.do and removed the program commands ("program define ...", "version 10",  and "end") then it works as I had intended (on Stata 11 MP w/ Mac OSX 10.6.5).  You can still keep setlogfile.ado in your path and invoke it any time if you want to start a new dofile. 
> 
> Others may have some tips about setting up an .ado file that is called in profile.do so that the -display- commands are shown -- I've only gotten them to work if the (noisily) -display- command is actually in the profile.do.
> 
> 
> - Eric
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> [email protected]
> Office: +979.845.6754
> 
> 
> 
> On Dec 2, 2010, at 10:19 PM, Donald Spady wrote:
> 
>> Eric
>> I rewrote my program using the commands you entered below.   Immediately below this sentence is my program, annotated as to where it didn't work.  As you can see, it does work a bit, but
>> the date function does not work, and the whole program does not work, but part of it does.
>> 
>> macro drop DB*
>> capture program drop setlogfile
>> program define setlogfile
>> version 10.0
>> 
>> **define timestamp**
>> local time:display %td date("$S_DATE","DMY")"_"substr("$S_TIME",1,2)"-"substr("$S_TIME",4,2)
>> //This returns an unknown function () message//
>> 
>> 
>> **request logfilename"
>> noi di "ENTER LOGFILE NAME:" _request(answer)
>> if "${answer}" == "" {
>>  global answer log //Default prefix to your logfiles if you just hit Return
>>  }
>> **define statalogs location:
>> global ldirectory "/users/DonSpady/documents/statalogs//"
>> //This returns ENTER LOGFILE NAME:.
>> //which doesn't work because the '.' is there and when I put in a file name in the command box 
>> // so I enter the logfile name test
>> // and it echoes: ENTER LOGFILE NAME:. test
>> 
> 
>> cap mkdir "${ldirectory}"
>> *global lfile "${ldirectory}/${answer}_'time'.log"
>> //I commented this command out because of the unknown function() message with the date stamp definition.
>> global lfile "${ldirectory}/${answer}.log"
>> 
>> ** start logfile **
>> cap log close _all
>> log using "${lfile}", append
>> di as log "Log File {bf:${answer}} saved in folder: {browse $ldirectory}"
>> end
>> // IF I run just from noi di ... on to end I get the prompt and I enter a file name and then I get an 
>> //'invalid syntax' r(198)
>> // return, BUT the file is opened in the directory and it starts to log it all.
>> 
> 
> Don't run the "end" command if you aren't running the "program define" command above.  If this doesn't stop this error, try turning on your trace and see where it hits the error (-set trace on-).
> 
> 
>> // IF I save the whole file and put it in my profile.do command, all I get is the other profile commands I have 
>> //entered, like set mem 10 m and so on.  NO PROMPTS, NO LOG FILE.
>> // SOMETHING is working, but not all of it.
>> 
>> Where am I going wrong.
>> Many thanks for all your help so far.
>> 
>> Don
>> 
>> On 2010-12-02, at 3:41 PM, Eric Booth wrote:
>> 
>>> <>
>>> 
>>> I'd start by changing your filepath from "/documents/statalogs/" to "/users/<youruser>/documents/statalogs/" (unless you've moved "documents") and confirming that the directory exists.  You can use `c(username)' instead of your username if you are putting this in the profile of multiple machines.
>>> 
>>> At one point in time, I tried to get -window control- to allow me to prompt for information, but never got it to work.  Instead, try using the "request()" option of -display- to prompt you.
>>> 
>>> 
>>> Using the "request()" option plus a couple of other changes, you could simplify all this code to something like this (unless you're stuck on working with -window control-):
>>> ***********!
>>> cap program drop setlogfile
>>> program define setlogfile
>>> 
>>> version 10
>>> 
>>> **define timestamp**
>>> local time:display  %td  date("$S_DATE", "DMY") "_" substr("$S_TIME",1,2) "-" substr("$S_TIME",4,2)
>>> 
>>> **request logfilename**
>>> noi di " ENTER LOGFILE NAME --> "  _request(answer)
>>> if "${answer}" == ""  {
>>> 	global answer  log   // Default prefix to your logfiles if you just hit Return
>>> }
>>> 
>>> 
>>> **define statalogs location:
>>> global ldirectory "/users/`c(username)'/documents/statalogs//"
>>> 
>>> cap mkdir "${ldirectory}"
>>> global lfile "${ldirectory}/${answer}_`time'.log"
>>> 
>>> **start log file**
>>> cap log close _all
>>> log using "${lfile}", append
>>> 	di as smcl "Log File {bf:${answer}} saved in folder: {browse $ldirectory}"
>>> end
>>> 
>>> 
>>> ****************************************
>>> **Run or include in your profile.do -->
>>> **setlogfile
>>> ***********!
>>> You can make the $ldirectory OS specific if you are running this on various machines with different platforms; something like:
>>> 
>>> if "`c(os)'" == "MacOSX"  global ldirectory "/users/`c(username)'/documents/statalogs//"
>>> if "`c(os)'" == "Windows" global ldirectory  "C:/statalogs//"
>>> **note: I used forward slashes in the Windows path based on advice here:  http://www.stata.com/statalist/archive/2009-05/msg00262.html
>>> 
>>> 
>>> - Eric
>>> 
>>> __
>>> Eric A. Booth
>>> Public Policy Research Institute
>>> Texas A&M University
>>> [email protected]
>>> Office: +979.845.6754
>>> 
>>> 
>>> 
>>> On Dec 2, 2010, at 3:44 PM, Donald Spady wrote:
>>> 
>>>> I am trying to modify a program called set_log_file.ado to put into my profile.do so as to prompt me to enter a filename for a logfile.  I am doing this for Stata 11 in MAC OS (snow leopard) 
>>>> When I try what is written below, it does not prompt me for a file name and just goes on to the end and then says "file/documents/statlogs/.log could not be opened.
>>>> It is not my program, but I got it from somewhere many years ago and it worked OK on a PC.  Now I have a MAC and am having some teething problems.
>>>> 
>>>> The program is as follows
>>>> 
>>>> macro drop DB*
>>>> capture program drop set_log_file
>>>> program define set_log_file
>>>> version 7.0
>>>> 
>>>> local x : display %dNDY date("$S_DATE","dmy") substr("$S_TIME",1,2) substr("$S_TIME",4,2)
>>>> global DB_filename "Enter log file name:"
>>>> *window control static DB_filename 10 10 100 10
>>>> *window control edit 10 20 175 8 DB_fname
>>>> *window control button "OK" 10 40 50 10 DB_ok default
>>>> *window control button "Cancel" 70 40 50 10 DB_can escape
>>>> global DB_ok "exit 3001"
>>>> global DB_can "exit 3000"
>>>> 
>>>> * change the 400 400 co-ordinates below to centre the dialog box on your own screen
>>>> * if you want smcl output, just drop the +."log" part below
>>>> * and change "basic.log" to "basic" (or any other name that suits)
>>>> * capture noisily window dialog "Log file details"  400 400 200 70
>>>> if _rc==3001 {
>>>> global DB_fname="$DB_fname.log"
>>>> }
>>>> else
>>>> if _rc==3000 {
>>>> global DB_fname="basic.log"
>>>> }
>>>> *add your own path statements on the following line
>>>> global lfile="/documents/statalogs/"+"$DB_fname"
>>>> log using $lfile, append
>>>> *log using $DB_fname
>>>> end
>>>> 
>>>> The *window lines are commented out because the use of the word 'control' for some reason doesn't work
>>>> 
>>>> Where am I going wrong.
>>>> 
>>>> Many thanks
>>>> 
>>>> Don Spady
>>>> 
>>>> Nature bats last.
>>>> 
> 
> 
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
> 

Don Spady

Ring the bells that still can ring


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index