Class: Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki_lyrics/utils/logger.rb

Overview

Copyright © 2006-2008 by Sergio Pistone [email protected]

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Instance Method Summary collapse

Constructor Details

#initialize(file_path, truncate_to_lines = -1 )) ⇒ Logger

Returns a new instance of Logger.



21
22
23
24
25
26
27
28
# File 'lib/wiki_lyrics/utils/logger.rb', line 21

def initialize( file_path, truncate_to_lines=-1 )
	@file_path = file_path
	@tabulation = nil
	@tabulation_base = "   ".freeze()
	@tabulation_level = 0
	@skip_first_line_tabulation = false
	truncate( truncate_to_lines ) if truncate_to_lines >= 0
end

Instance Method Details

#decrease_tabulation_levelObject



119
120
121
# File 'lib/wiki_lyrics/utils/logger.rb', line 119

def decrease_tabulation_level()
	set_tabulation_level( @tabulation_level - 1 )
end

#finalizeObject

TODO revise implementation



30
31
# File 'lib/wiki_lyrics/utils/logger.rb', line 30

def finalize() # TODO revise implementation
end

#get_file_pathObject



33
34
35
# File 'lib/wiki_lyrics/utils/logger.rb', line 33

def get_file_path()
	return @file_path
end

#get_tabulation_baseObject



83
84
85
# File 'lib/wiki_lyrics/utils/logger.rb', line 83

def get_tabulation_base()
	return @tabulation_base
end

#get_tabulation_levelObject



99
100
101
# File 'lib/wiki_lyrics/utils/logger.rb', line 99

def get_tabulation_level()
	return @tabulation_level
end

#increase_tabulation_levelObject



115
116
117
# File 'lib/wiki_lyrics/utils/logger.rb', line 115

def increase_tabulation_level()
	set_tabulation_level( @tabulation_level + 1 )
end

#log(msg, new_lines = 1) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wiki_lyrics/utils/logger.rb', line 70

def log( msg, new_lines=1 )
	output = File.new( @file_path, File::CREAT|File::APPEND|File::WRONLY )
	if @tabulation
		output.write( @tabulation ) if ! @skip_first_line_tabulation
		output.write( msg.gsub( "\n", "\n#{@tabulation}" ) )
		@skip_first_line_tabulation = new_lines <= 0
	else
		output.write( msg )
	end
	new_lines.times() { output.write( "\n" ) }
	output.close()
end

#resetObject



65
66
67
68
# File 'lib/wiki_lyrics/utils/logger.rb', line 65

def reset()
	output = File.new( @file_path, File::CREAT|File::TRUNC )
	output.close()
end

#set_file_path(file_path) ⇒ Object



37
38
39
40
41
42
# File 'lib/wiki_lyrics/utils/logger.rb', line 37

def set_file_path( file_path )
	if @file_path != file_path
		File.delete( @file_path ) if File.exist?( @file_path ) && ! File.directory?( @file_path )
		@file_path = file_path.clone().freeze()
	end
end

#set_tabulation_base(tabulation_base) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wiki_lyrics/utils/logger.rb', line 87

def set_tabulation_base( tabulation_base )
	if @tabulation_base != tabulation_base
		@tabulation_level = tabulation_base.clone().freeze()
		if level <= 0
			@tabulation = nil
		else
			@tabulation = ""
			level.times() { @tabulation << @tabulation_base }
		end
	end
end

#set_tabulation_level(level) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/wiki_lyrics/utils/logger.rb', line 103

def set_tabulation_level( level )
	if @tabulation_level != level
		@tabulation_level = level
		if level <= 0
			@tabulation = nil
		else
			@tabulation = ""
			level.times() { @tabulation << @tabulation_base }
		end
	end
end

#truncate(max_lines) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wiki_lyrics/utils/logger.rb', line 44

def truncate( max_lines )
	begin
		file = File.new( @file_path, File::RDONLY )
	rescue Errno::ENOENT
		file = File.new( @file_path, File::CREAT|File::TRUNC )
	end
	lines = file.read().split( "\n" )
	file.close()
	offset = lines.size() - max_lines
	if offset > 0
		file = File.new( @file_path, File::CREAT|File::TRUNC|File::WRONLY )
		max_lines.times() do |index|
			line = lines[offset + index]
			break if ! line
			file.write( line )
			file.write( "\n" )
		end
		file.close()
	end
end