Module: LogInFile

Defined in:
lib/log_in_file.rb,
lib/log_in_file/main.rb,
lib/log_in_file/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

LOG_TIME_FORMAT =
'%Y %m %d %H:%M:%S'
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.closeObject


51
52
53
54
55
# File 'lib/log_in_file/main.rb', line 51

def close
  @rf ||= nil
  rf.close unless @rf.nil?
  @rf = nil
end

.find_temporary_folderObject


83
84
85
86
87
88
89
90
# File 'lib/log_in_file/main.rb', line 83

def find_temporary_folder
  curdir = File.expand_path('.')
  ['tmp','temp','temporary','.tmp'].each do |cname|
    pth = File.join(curdir, cname)
    return pth if File.exist?(pth)
  end
  return nil
end

.formated_message(message, **options) ⇒ Object


33
34
35
36
37
38
# File 'lib/log_in_file/main.rb', line 33

def formated_message(message, **options)
  s = message.dup
  s = s % options[:data] if options.key?(:data)
  s = "--- #{Time.now.strftime(LOG_TIME_FORMAT)} #{s}"
  return s
end

.logfile_folderObject


68
69
70
71
72
73
74
75
76
# File 'lib/log_in_file/main.rb', line 68

def logfile_folder
  @logfile_folder ||= begin
    if tmp_folder.nil?
      File.expand_path('.')
    else
      tmp_folder
    end
  end
end

.logfile_pathObject

Log file path


64
65
66
# File 'lib/log_in_file/main.rb', line 64

def logfile_path
  @logfile_path ||= File.join(logfile_folder, 'loginfile.log')
end

.open_logObject


40
41
42
43
# File 'lib/log_in_file/main.rb', line 40

def open_log
  close
  `open "#{LogInFile.logfile_path}"`    
end

.remove_logObject


57
58
59
60
# File 'lib/log_in_file/main.rb', line 57

def remove_log
  close
  File.delete(logfile_path) if File.exist?(logfile_path)
end

.rfObject


45
46
47
48
49
# File 'lib/log_in_file/main.rb', line 45

def rf
  @rf ||= begin
    File.open(logfile_path, File::APPEND|File::WRONLY|File::CREAT)
  end
end

.tmp_folderObject

Maybe a temp folder


79
80
81
# File 'lib/log_in_file/main.rb', line 79

def tmp_folder
  @tmp_folder ||= find_temporary_folder
end

.write(message, **options) ⇒ Object


28
29
30
31
# File 'lib/log_in_file/main.rb', line 28

def write(message, **options)
  rf.puts(formated_message(message, **options))
  return true
end