Class: HQ::Logger::AnsiLogger

Inherits:
IoLogger
  • Object
show all
Defined in:
lib/hq/logger/ansi-logger.rb

Constant Summary collapse

ANSI_CODES =
{
	:normal => "\e[0m",
	:bold => "\e[1m",
	:black => "\e[30m",
	:red => "\e[31m",
	:green => "\e[32m",
	:yellow => "\e[33m",
	:blue => "\e[34m",
	:magenta => "\e[35m",
	:cyan => "\e[36m",
	:white => "\e[37m",
}
MESSAGE_COLOURS =
{

	:normal => :normal,

	:hostname => :blue,

	:timing => :blue,
	:trace => :magenta,
	:debug => :cyan,
	:detail => :white,
	:notice => :green,
	:warning => :yellow,
	:error => :red,

	:command_output => :white,
}
DIFF_COLOURS =
{
	:minus_minus_minus => :magenta,
	:plus_plus_plus => :magenta,
	:at_at => :magenta,
	:minus => :red,
	:plus => :blue,
	:else => :white,
}

Instance Attribute Summary

Attributes inherited from IoLogger

#level, #out

Instance Method Summary collapse

Methods inherited from IoLogger

#fix_stuff, #output

Instance Method Details

#ansi_line(text, stuff, colour, prefix = "") ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hq/logger/ansi-logger.rb', line 51

def ansi_line text, stuff, colour, prefix = ""

	raise "No such colour: #{colour}" \
		unless MESSAGE_COLOURS[colour] || ANSI_CODES[colour]

	out.print \
		ANSI_CODES[:bold],
		ANSI_CODES[MESSAGE_COLOURS[:hostname]],
		stuff[:hostname],
		": ",
		ANSI_CODES[colour] || ANSI_CODES[MESSAGE_COLOURS[colour]],
		stuff[:prefix] + prefix,
		text,
		ANSI_CODES[:normal],
		"\n"

end

#output_real(content, stuff) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hq/logger/ansi-logger.rb', line 69

def output_real content, stuff

	if content.is_a? String
		ansi_line content, stuff, :normal
		return
	end

	case content["type"]

	when "log"

		ansi_line content["text"], stuff, stuff[:level]

		if content["content"]
			content["content"].each do
				|item|
				output item, stuff, "  "
			end
		end

	when "exception"

		ansi_line content["text"], stuff, stuff[:level]
		ansi_line content["message"], stuff, :normal, "  "

		content["backtrace"].each do |frame|
			ansi_line frame, stuff, :normal, "    "
		end

	when "diff"

		ansi_line content["text"], stuff, stuff[:level]

		content["content"].each do
			|line|
			colour = DIFF_COLOURS[line["type"].gsub("-", "_")[5..-1].to_sym]
			ansi_line line["text"], stuff, colour, "  "
		end

	when "command"

		ansi_line content["text"], stuff, stuff[:level]

		if content["output"]

			content["output"].each do
				|line|
				ansi_line line, stuff, :normal, "  "
			end

		end

	when "command-output"

		ansi_line content["text"], stuff, :normal, "  "

	else

		pp content
		raise "Error"

	end

end

#valid_modesObject



47
48
49
# File 'lib/hq/logger/ansi-logger.rb', line 47

def valid_modes
	[ :normal, :partial ]
end