# Wrapper around cronolog using pipes

**URL:** <https://www.ruby-forum.com/t/wrapper-around-cronolog-using-pipes/81680>\
**Category:** Ruby\
**Created:** [January 11, 2007, 7:24am UTC](https://www.ruby-forum.com/t/wrapper-around-cronolog-using-pipes/81680 "2007-01-11T07:24:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnufied](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ea666f/32.png) [@gnufied](https://www.ruby-forum.com/u/gnufied)\
**Post date:** [January 11, 2007, 7:24am UTC](https://www.ruby-forum.com/t/wrapper-around-cronolog-using-pipes/81680/1 "2007-01-11T07:24:44Z")

</div>

require “thread”  
require “open4”

path\_to\_cron = `which cronolog`  
path\_to\_cron = path\_to\_cron.chomp

DELAY\_TEMPLATES = { :daily =\> {:cmd\_string =\> "#{path\_to\_cron}  
",:format\_string =\> “%Y/%m/%d”},  
:hourly =\> {:cmd\_string =\> "#{path\_to\_cron} --delay ‘1 hour’  
",:format\_string =\> nil},  
:midnight =\> {:cmd\_string =\> "#{path\_to\_cron} --delay ‘12 hours’  
",:format\_string =\> nil}}

class Log4u

def initialize(\_file\_name,delay\_interval = :daily)

```
raise(ArgumentError.new,"Invalid arguments") unless

```

[:daily,:hourly,:midnight].include? delay\_interval

```
@mutex = Mutex.new

filename_string = File.basename(_file_name)
dirname_string = File.dirname(_file_name)

if (format_string = DELAY_TEMPLATES[delay_interval][:format_string])
  option_string =

```

“#{dirname\_string}/#{format\_string}/#{filename\_string}”  
else  
option\_string = “#{dirname\_string}/#{filename\_string}”  
end

```
check_for_prgrm dirname_string

cmd_string = DELAY_TEMPLATES[delay_interval][:cmd_string]
pid,@log_pipe,stdout,stderr = Open4.popen4("#{cmd_string}

```

#{option\_string}")  
end

def check\_for\_prgrm dirname\_string  
path\_to\_cron = `which cronolog`  
raise “Cronolog doesn’t exist in path” if path\_to\_cron.empty?  
raise “Log Directory isn’t writable” unless File.writable?  
dirname\_string  
end

def time\_string  
time\_now = Time.now  
time\_now.strftime("%I:%M%p")  
end

def error msg  
@mutex.synchronize{ @log\_pipe.puts “ERROR: #{time\_string}: #{msg}” }  
end

def info msg  
@mutex.synchronize{ @log\_pipe.puts “INFO: #{time\_string}: #{msg}” }  
end

def debug msg  
@mutex.synchronize { @log\_pipe.puts “DEBUG: #{time\_string}: #{msg}”}  
end

def close\_log  
@mutex.synchronize { @log\_pipe.close }  
end  
end

I am facing following problems with above code:

1. Doesn’t detect the pipe errors and propagate it to parent ,  
and when used from threads would invariably lead to buggy code.  
because threads won’t  
detect the exception until abort\_on\_exception is true or a join is  
performed.  
May be a problem of the author who is going to use it, but still if  
i can simpify things a bit.
2. Not sure, how it behaves when multiple processes/threads attempt to  
get cronolog on  
the same file. In my tests cronolog seems to handle it, but not  
entirely sure.
3. Gives bloody zombie processes sometimes.

Any ideas folks?
