Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread 'John Clements' via Racket Users
Oog… I don’t really think dynamic-require is the right tool here at all. 
Setting the current-output-port seems vastly more reasonable.

John

> On Apr 1, 2019, at 12:19, Chansey  wrote:
> 
> More elegant way is combining with-output-to-file with dynamic-require
> 
> (with-output-to-file FILE
>   (lambda () (dynamic-require "main.rkt" #f))
>   )
> 
> On Tuesday, April 2, 2019 at 1:53:47 AM UTC+8, Chansey wrote:
> Dear George,
> 
> Your method is also very well.
> 
> Because using with-output-to-file to wrapper main.rkt is enough, not must in 
> every module files.
> 
> Thanks.
> 
> On Tuesday, April 2, 2019 at 1:30:09 AM UTC+8, gneuner2 wrote:
> 
> On 4/1/2019 1:09 PM, Siyuan Chen wrote: 
> > Dear George, 
> > 
> > This means we must modify these module code? 
> > 
> > For example: 
> > 
> > suppose in foo.rkt 
> > 
> > #lang racket 
> > (provide ...) 
> > (require ...) 
> > 
> > (define (my-func-1 x) 
> > ...printf..) 
> > 
> > (define (my-func-2 x) 
> >   ...printf..) 
> > 
> > It must be modified to this? 
> > 
> > #lang racket 
> > (provide ...) 
> > (require ...) 
> > 
> > (with-output-to-file  
> >(lambda () 
> >   (define (my-func-1 x) 
> > ...printf..) 
> > 
> > (define (my-func-2 x) 
> >   ...printf..) 
> >)) 
> > 
> > It seems not very convenient... 
> > 
> > Is it possible to temporarily change global output port? 
> > 
> > Thanks. 
> 
> 
> Not exactly.  You don't need to encapsulate the function definitions - 
> just the calls.  IT would be more like the following: 
> 
> #lang racket 
> (provide ...) 
> (require ...) 
> 
>(define (my-func-1 x) 
> ...printf..) 
> 
> (define (my-func-2 x) 
>...printf..) 
> 
> 
> (with-output-to-file  
> (lambda () 
>(my-func-1 ...) 
>(my-func-2 ...) 
> ... 
> )) 
> 
> 
> But if that still is too heavyweight, the best solution is to run the 
> program from the command line and redirect or tee the output into a 
> file:  e.g., 
> 
> racket [flags] program > file 
> or 
> racket [flags] program | tee file 
> 
> The exact flags to use depend on the specifics of your program (e.g., is 
> it a script, is it a module, etc.): 
> https://docs.racket-lang.org/reference/running-sa.html?q=command%20line#%28part._mz-cmdline%29
>  
> 
> Note: Windows doesn't have "tee", but you can do something similar with 
> racket [flags] program > file 2>&1 
> 
> George 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread Chansey
More elegant way is combining with-output-to-file with dynamic-require

(with-output-to-file FILE
  (lambda () (dynamic-require "main.rkt" #f))
  )

On Tuesday, April 2, 2019 at 1:53:47 AM UTC+8, Chansey wrote:
>
> Dear George,
>
> Your method is also very well.
>
> Because using with-output-to-file to wrapper main.rkt is enough, not must 
> in every module files.
>
> Thanks.
>
> On Tuesday, April 2, 2019 at 1:30:09 AM UTC+8, gneuner2 wrote:
>>
>>
>> On 4/1/2019 1:09 PM, Siyuan Chen wrote: 
>> > Dear George, 
>> > 
>> > This means we must modify these module code? 
>> > 
>> > For example: 
>> > 
>> > suppose in foo.rkt 
>> > 
>> > #lang racket 
>> > (provide ...) 
>> > (require ...) 
>> > 
>> > (define (my-func-1 x) 
>> > ...printf..) 
>> > 
>> > (define (my-func-2 x) 
>> >   ...printf..) 
>> > 
>> > It must be modified to this? 
>> > 
>> > #lang racket 
>> > (provide ...) 
>> > (require ...) 
>> > 
>> > (with-output-to-file  
>> >(lambda () 
>> >   (define (my-func-1 x) 
>> > ...printf..) 
>> > 
>> > (define (my-func-2 x) 
>> >   ...printf..) 
>> >)) 
>> > 
>> > It seems not very convenient... 
>> > 
>> > Is it possible to temporarily change global output port? 
>> > 
>> > Thanks. 
>>
>>
>> Not exactly.  You don't need to encapsulate the function definitions - 
>> just the calls.  IT would be more like the following: 
>>
>> #lang racket 
>> (provide ...) 
>> (require ...) 
>>
>>(define (my-func-1 x) 
>> ...printf..) 
>>
>> (define (my-func-2 x) 
>>...printf..) 
>>
>>
>> (with-output-to-file  
>> (lambda () 
>>(my-func-1 ...) 
>>(my-func-2 ...) 
>> ... 
>> )) 
>>
>>
>> But if that still is too heavyweight, the best solution is to run the 
>> program from the command line and redirect or tee the output into a 
>> file:  e.g., 
>>
>> racket [flags] program > file 
>> or 
>> racket [flags] program | tee file 
>>
>> The exact flags to use depend on the specifics of your program (e.g., is 
>> it a script, is it a module, etc.): 
>>
>> https://docs.racket-lang.org/reference/running-sa.html?q=command%20line#%28part._mz-cmdline%29
>>  
>>
>> Note: Windows doesn't have "tee", but you can do something similar with 
>> racket [flags] program > file 2>&1 
>>
>> George 
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread Chansey
Dear George,

Your method is also very well.

Because using with-output-to-file to wrapper main.rkt is enough, not must 
in every module files.

Thanks.

On Tuesday, April 2, 2019 at 1:30:09 AM UTC+8, gneuner2 wrote:
>
>
> On 4/1/2019 1:09 PM, Siyuan Chen wrote: 
> > Dear George, 
> > 
> > This means we must modify these module code? 
> > 
> > For example: 
> > 
> > suppose in foo.rkt 
> > 
> > #lang racket 
> > (provide ...) 
> > (require ...) 
> > 
> > (define (my-func-1 x) 
> > ...printf..) 
> > 
> > (define (my-func-2 x) 
> >   ...printf..) 
> > 
> > It must be modified to this? 
> > 
> > #lang racket 
> > (provide ...) 
> > (require ...) 
> > 
> > (with-output-to-file  
> >(lambda () 
> >   (define (my-func-1 x) 
> > ...printf..) 
> > 
> > (define (my-func-2 x) 
> >   ...printf..) 
> >)) 
> > 
> > It seems not very convenient... 
> > 
> > Is it possible to temporarily change global output port? 
> > 
> > Thanks. 
>
>
> Not exactly.  You don't need to encapsulate the function definitions - 
> just the calls.  IT would be more like the following: 
>
> #lang racket 
> (provide ...) 
> (require ...) 
>
>(define (my-func-1 x) 
> ...printf..) 
>
> (define (my-func-2 x) 
>...printf..) 
>
>
> (with-output-to-file  
> (lambda () 
>(my-func-1 ...) 
>(my-func-2 ...) 
> ... 
> )) 
>
>
> But if that still is too heavyweight, the best solution is to run the 
> program from the command line and redirect or tee the output into a 
> file:  e.g., 
>
> racket [flags] program > file 
> or 
> racket [flags] program | tee file 
>
> The exact flags to use depend on the specifics of your program (e.g., is 
> it a script, is it a module, etc.): 
>
> https://docs.racket-lang.org/reference/running-sa.html?q=command%20line#%28part._mz-cmdline%29
>  
>
> Note: Windows doesn't have "tee", but you can do something similar with 
> racket [flags] program > file 2>&1 
>
> George 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread Siyuan Chen
Dear George,

I have just found a way to solve this problem.

Use (current-output-port out).

But still very thank you.


On Tue, Apr 2, 2019 at 1:30 AM George Neuner  wrote:

>
> On 4/1/2019 1:09 PM, Siyuan Chen wrote:
> > Dear George,
> >
> > This means we must modify these module code?
> >
> > For example:
> >
> > suppose in foo.rkt
> >
> > #lang racket
> > (provide ...)
> > (require ...)
> >
> > (define (my-func-1 x)
> > ...printf..)
> >
> > (define (my-func-2 x)
> >   ...printf..)
> >
> > It must be modified to this?
> >
> > #lang racket
> > (provide ...)
> > (require ...)
> >
> > (with-output-to-file 
> >(lambda ()
> >   (define (my-func-1 x)
> > ...printf..)
> >
> > (define (my-func-2 x)
> >   ...printf..)
> >))
> >
> > It seems not very convenient...
> >
> > Is it possible to temporarily change global output port?
> >
> > Thanks.
>
>
> Not exactly.  You don't need to encapsulate the function definitions -
> just the calls.  IT would be more like the following:
>
> #lang racket
> (provide ...)
> (require ...)
>
>(define (my-func-1 x)
> ...printf..)
>
> (define (my-func-2 x)
>...printf..)
>
>
> (with-output-to-file 
> (lambda ()
>(my-func-1 ...)
>(my-func-2 ...)
> ...
> ))
>
>
> But if that still is too heavyweight, the best solution is to run the
> program from the command line and redirect or tee the output into a
> file:  e.g.,
>
> racket [flags] program > file
> or
> racket [flags] program | tee file
>
> The exact flags to use depend on the specifics of your program (e.g., is
> it a script, is it a module, etc.):
>
> https://docs.racket-lang.org/reference/running-sa.html?q=command%20line#%28part._mz-cmdline%29
>
> Note: Windows doesn't have "tee", but you can do something similar with
> racket [flags] program > file 2>&1
>
> George
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread George Neuner



On 4/1/2019 1:27 PM, Chansey wrote:

I've found a way.

Use (current-output-port out).


That will work, but it's not a good idea except as a quick hack to test 
something.  Working in DrRacket, you may find that the output remains 
redirected after your program has ended (or crashed).


If you want to capture output in a file, it is best to either use OS 
redirection, or open/close the file explicitly in your code.  There are 
ways to specify program parameters on the command line if necessary.

https://docs.racket-lang.org/reference/Command-Line_Parsing.html?q=command%20line%20param

George

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread George Neuner



On 4/1/2019 1:09 PM, Siyuan Chen wrote:

Dear George,

This means we must modify these module code?

For example:

suppose in foo.rkt

#lang racket
(provide ...)
(require ...)

(define (my-func-1 x)
...printf..)

(define (my-func-2 x)
  ...printf..)

It must be modified to this?

#lang racket
(provide ...)
(require ...)

(with-output-to-file 
   (lambda ()
  (define (my-func-1 x)
...printf..)

(define (my-func-2 x)
  ...printf..)
   ))

It seems not very convenient...

Is it possible to temporarily change global output port?

Thanks.



Not exactly.  You don't need to encapsulate the function definitions - 
just the calls.  IT would be more like the following:


#lang racket
(provide ...)
(require ...)

  (define (my-func-1 x)
...printf..)

(define (my-func-2 x)
  ...printf..)


(with-output-to-file 
   (lambda ()
  (my-func-1 ...)
      (my-func-2 ...)
   ...
   ))


But if that still is too heavyweight, the best solution is to run the 
program from the command line and redirect or tee the output into a 
file:  e.g.,


racket [flags] program > file
or
racket [flags] program | tee file

The exact flags to use depend on the specifics of your program (e.g., is 
it a script, is it a module, etc.):

https://docs.racket-lang.org/reference/running-sa.html?q=command%20line#%28part._mz-cmdline%29

Note: Windows doesn't have "tee", but you can do something similar with
racket [flags] program > file 2>&1

George

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread Chansey
I've found a way.

Use (current-output-port out).

On Tuesday, April 2, 2019 at 1:02:05 AM UTC+8, gneuner2 wrote:
>
>
> On 4/1/2019 12:54 PM, Chansey wrote: 
> > My racket project consists of several module files (e.g.: "foo.rkt", 
> > "bar.rkt", ...). 
> > 
> > The entry module file is "main.rkt" 
> > 
> > These files call "printf" in different places of the code (But 
> > "printf" only can print messages to REPL). 
> > 
> > My question is: 
> > 
> > Is there any way to redefine "printf" in main.rkt in order to print 
> > messages to a file? 
>
> "printf"  doesn't write to the REPL per se - it writes to the current 
> output port (which may lead to the REPL).  Depending on how your code is 
> structured, it may be as easy as: 
>
> (with-output-to-file  
>(lambda () 
>... ;; your code here 
>)) 
>
> Another choice is to use fprintf which allows to choose the output 
> destination. 
>
> George 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Is there any way to redefine printf in main.rkt ?

2019-04-01 Thread George Neuner



On 4/1/2019 12:54 PM, Chansey wrote:
My racket project consists of several module files (e.g.: "foo.rkt", 
"bar.rkt", ...).


The entry module file is "main.rkt"

These files call "printf" in different places of the code (But 
"printf" only can print messages to REPL).


My question is:

Is there any way to redefine "printf" in main.rkt in order to print 
messages to a file?


"printf"  doesn't write to the REPL per se - it writes to the current 
output port (which may lead to the REPL).  Depending on how your code is 
structured, it may be as easy as:


(with-output-to-file 
  (lambda ()
  ... ;; your code here
  ))

Another choice is to use fprintf which allows to choose the output 
destination.


George

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.